如何在C#中调用winapi改变窗口图标

问题描述 投票:0回答:2

我正在尝试使用 WinApi

SendMessage()
更改窗口图标,但没有成功。

知道这里出了什么问题吗?

public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
    if (!browser.IsDisposed && browser.IsPopup)
    {
        Application.Current.Dispatcher?.Invoke(new Action(() =>
            {

                IntPtr handle = browser.GetHost().GetWindowHandle();
                string getFavIcon = chromiumWebBrowser.Address.Split('#')[0] + "assets/images/MyPic.png";
                var image = new BitmapImage(new Uri(getFavIcon));
                var bitMap = Utilities.BitmapImage2Bitmap(image);
                //IntPtr imagePtr = image.ge
                SendMessage(handle, WM_SETICON, 0, bitMap.GetHbitmap());
                SendMessage(handle, WM_SETICON, 1, bitMap.GetHbitmap());
            })
        );

    }

}
wpf winapi c#-3.0
2个回答
0
投票

终于我成功了,我得到了黑色图标,因为图像尚未下载。 以下代码是覆盖 cefsharp chromium 控件弹出窗口的图标。

//ILifeSpanHandler

private const int WM_SETICON = 0x80;
 public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
        {
            if (!browser.IsDisposed && browser.IsPopup)
            {
                Application.Current.Dispatcher?.Invoke(new Action(() =>
                    {
                         _handle = browser.GetHost().GetWindowHandle();
                        var bitMap = Utilities.BitmapImage2Bitmap(Utilities.FavIcon);
                        SendMessage(_handle, WM_SETICON, 0, bitMap.GetHicon());
                    })
                );
            }
        }  

0
投票

首先,我们需要完成所有的ILifeSpanHandler,否则,它会抛出异常“NotImplementedException”,如下所示:

` 内部类 LifeSpanHandler : ILifeSpanHandler { [DllImport(“User32.dll”,EntryPoint =“SendMessage”)] 公共静态 extern int SendMessage( IntPtr hWnd, // 消息接收窗口句柄 int Msg, // 消息ID int wParam, // w 参数 int lParam // l 参数 );

    private const int WM_SETICON = 0x80;
    public LifeSpanHandler() { }

    public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
    {
        //throw new NotImplementedException();
        return false;
    }

    public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
    {
        //throw new NotImplementedException();

        if (!browser.IsDisposed && browser.IsPopup)
        {
            var handle = browser.GetHost().GetWindowHandle();
            Bitmap bmp = Properties.Resources.feed;
            SendMessage(handle, WM_SETICON, 0, (int)bmp.GetHicon());
        }
    }

    public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
    {
        //throw new NotImplementedException();
    }

    public bool OnBeforePopup(IWebBrowser chromiumWebBrowser, 
        IBrowser browser, IFrame frame, 
        string targetUrl, string targetFrameName, 
        WindowOpenDisposition targetDisposition, 
        bool userGesture, 
        IPopupFeatures popupFeatures, IWindowInfo windowInfo, 
        IBrowserSettings browserSettings, 
        ref bool noJavascriptAccess, out IWebBrowser newBrowser)
    {
        //throw new NotImplementedException();

        newBrowser = new ChromiumWebBrowser();
        newBrowser.Load(targetUrl);
        return false;
    }
}

`

然后,使用来自 CefSharp 的实现组件,假设其定义为“yourBrowser”:

yourBrowser.LifeSpanHandler = new LifeSpanHandler();

我们需要注意下面Microsoft Visual Studio提到的一些提示:

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.