using Microsoft.Web.WebView2.Core; using System.ComponentModel.DataAnnotations; using System.IO; using Yw.Model.Map; using Yw.WebBrower; namespace PBS.WinFrmUI.Hydro { public partial class MapSetPlaceMarkerContainer : UserControl, IMapSetPlaceMarkerContainer { public MapSetPlaceMarkerContainer() { InitializeComponent(); } public event Action LoadCompletedEvent; public event Action LoadFailedEvent; public event Action HandingErrorEvent; public event Action SetMarkerEvent; public event Action SetMapBoundsEvent; public MapSetPlaceMarkerCallBackObj CallBackObj { get { if (_callBackObj == null) { _callBackObj = new MapSetPlaceMarkerCallBackObj(); } return _callBackObj; } } private MapSetPlaceMarkerCallBackObj _callBackObj; public bool IsInitialized { get { return _isInitialized; } } private bool _isInitialized; /// /// 初始化容器 /// public async Task InitialContainer() { var callBackObj = this.CallBackObj; callBackObj.LoadCompletedEvent += CallBackObj_LoadCompletedEvent; callBackObj.LoadFailedEvent += CallBackObj_LoadFailedEvent; callBackObj.HandingErrorEvent += CallBackObj_HandingErrorEvent; callBackObj.SetMarkerEvent += CallBackObj_SetMarkerEvent; callBackObj.SetMapBoundsEvent += CallBackObj_SetMapBoundsEvent; await this.webViewControl1.InitialWebBrower(MapUrlHelper.SetPlaceMarkerUrl, callBackObj); } //加载完成 private void CallBackObj_LoadCompletedEvent() { _isInitialized = true; this.LoadCompletedEvent?.Invoke(); } //加载失败 private void CallBackObj_LoadFailedEvent() { this.LoadFailedEvent?.Invoke(); } //处理错误 private void CallBackObj_HandingErrorEvent(HandingError obj) { this.HandingErrorEvent?.Invoke(obj); } //设置标记 private void CallBackObj_SetMarkerEvent(Yw.Model.Map.Marker obj) { this.SetMarkerEvent?.Invoke(obj); } //设置地图边界 private void CallBackObj_SetMapBoundsEvent(Yw.Model.Map.Point southWest, Yw.Model.Map.Point northEast) { this.SetMapBoundsEvent?.Invoke(southWest, northEast); } /// /// 加载标记 /// public async Task LoadMarker(Marker marker) { if (!this.IsInitialized) { return false; } return await this.webViewControl1.EvaluateScriptAsync("loadMarker", marker); } /// /// 加载地图边界 /// public async Task LoadMapBounds(Yw.Model.Map.Point southWest, Yw.Model.Map.Point northEast) { if (!this.IsInitialized) { return false; } return await this.webViewControl1.EvaluateScriptAsync("loadMapBounds", southWest, northEast); } /// /// 查询地址 /// public async Task SearchAddress() { if (!this.IsInitialized) { return; } await this.webViewControl1.EvaluateScriptAsync("searchAddress()"); } public async Task<(double Width, double Height, double Zoom)?> CaptureImage (string tempFilePath, Yw.Model.Map.Point southWest, Yw.Model.Map.Point northEast) { var webView = this.webViewControl1.Controls[0] as Microsoft.Web.WebView2.WinForms.WebView2; if (webView == null) { TipFormHelper.ShowError(" webView is null"); return null; } return await this.Invoke(async () => { await webView.EnsureCoreWebView2Async(); var coreWebView = webView.CoreWebView2; int pageWidth = webView.ClientSize.Width; int pageHeight = webView.ClientSize.Height; webView.Width = pageWidth; webView.Height = pageHeight; var pWidth = pageWidth - 10; var pHeight = pageHeight - 10; var directory = Path.GetDirectoryName(tempFilePath); if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); using (var fileStream = new FileStream(tempFilePath + "_t.png", FileMode.Create)) { await coreWebView.CapturePreviewAsync(CoreWebView2CapturePreviewImageFormat.Png, fileStream); var bitMap = new Bitmap(fileStream); bitMap.Clone(new Rectangle(10, 10, pWidth, pHeight), System.Drawing.Imaging.PixelFormat.DontCare).Save(tempFilePath); fileStream.Close(); var screen = Screen.PrimaryScreen; var screenBound = screen.Bounds; var bo = (double)screenBound.Height / screenBound.Width; var dpi = Math.Round(bo, 3);//0.56 } var size = AmapBoundsHelper.CalcSize(southWest, northEast); double distanceHeight = size.Height; double distanceWidth = size.Width; var BackgroundWidth = distanceWidth; var BackgroundHeight = distanceHeight; var w = pageWidth / BackgroundWidth; var h = pageHeight / BackgroundHeight; var Zoom = w > h ? w : h; File.Delete(tempFilePath + "_t.png"); return (BackgroundWidth, BackgroundHeight, Zoom); }); } #region 边框 /// /// 显示边框 /// [Display(Name = "显示边框")] [DisplayName("显示边框")] [Browsable(true)] public bool ShowBorder { get { return this.webViewControl1.ShowBorder; } set { this.webViewControl1.ShowBorder = value; } } /// /// 边框颜色 /// [Display(Name = "边框颜色")] [DisplayName("边框颜色")] [Browsable(true)] public Color BorderColor { get { return this.webViewControl1.BorderColor; } set { this.webViewControl1.BorderColor = value; } } /// /// 边框宽度 /// [Display(Name = "边框宽度")] [DisplayName("边框宽度")] [Browsable(true)] public int BorderWidth { get { return this.webViewControl1.BorderWidth; } set { this.webViewControl1.BorderWidth = value; } } #endregion } }