没有引用到onload嘛
时间: 2024-04-10 19:33:09 浏览: 19
非常抱歉,我在之前的回答中忘记引用 `OnLoad` 方法。确保在窗体类中重写 `OnLoad` 方法,并在其中调用 `SetFormIcon` 方法,以便在窗体加载时设置大图标。以下是更新后的示例代码:
```csharp
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApp
{
public partial class MainForm : Form
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
private const int WM_SETICON = 0x80;
public MainForm()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
SetFormIcon();
}
private void SetFormIcon()
{
Icon icon = new Icon("path_to_icon_file.ico");
IntPtr hIcon = icon.Handle;
// 设置窗体的大图标
SendMessage(Handle, WM_SETICON, (IntPtr)1, hIcon);
}
}
}
```
请确保将 "path_to_icon_file.ico" 替换为你自己的图标文件路径。这样,在窗体加载时,`OnLoad` 方法会被调用,并且 `SetFormIcon` 方法会被执行,从而设置窗体的大图标。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)