c# 重载WndProc,实现重写“最小化”的实现方法
程序员文章站
2023-11-22 10:39:04
code #1复制代码 代码如下:private void form1_sizechanged(object sender, eventargs e) //最小化隐藏窗体...
code #1
private void form1_sizechanged(object sender, eventargs e) //最小化隐藏窗体
{
if (this.windowstate == formwindowstate.minimized)//窗体状态为最小化
{
stoprecttimer.enabled = false;
this.visible = false;
this.notifyicon1.visible = true; //显示系统托盘图标
this.notifyicon1.text = this.text; //设置图标显示的文本
this.showintaskbar = false; //窗体在任务标中隐藏
reghotkey();
打开otoolstripmenuitem.text = "打开(&o)";
}
}
很显然,如果打开歌词状态话的话,怎样才能最小化而不改变窗体的大小呢?我想到了重载“最小化”,但是怎么重载呢?这里给出一种重载wndproc的方案:
const int wm_syscommand = 0x112;
const int sc_close = 0xf060;
const int sc_minimize = 0xf020;
const int sc_maximize = 0xf030;
protected override void wndproc(ref message m)
{
if (m.msg == wm_syscommand)
{
if (m.wparam.toint32() == sc_minimize)
{
this.visible = false;
return;
}
}
base.wndproc(ref m);
}
复制代码 代码如下:
private void form1_sizechanged(object sender, eventargs e) //最小化隐藏窗体
{
if (this.windowstate == formwindowstate.minimized)//窗体状态为最小化
{
stoprecttimer.enabled = false;
this.visible = false;
this.notifyicon1.visible = true; //显示系统托盘图标
this.notifyicon1.text = this.text; //设置图标显示的文本
this.showintaskbar = false; //窗体在任务标中隐藏
reghotkey();
打开otoolstripmenuitem.text = "打开(&o)";
}
}
很显然,如果打开歌词状态话的话,怎样才能最小化而不改变窗体的大小呢?我想到了重载“最小化”,但是怎么重载呢?这里给出一种重载wndproc的方案:
复制代码 代码如下:
const int wm_syscommand = 0x112;
const int sc_close = 0xf060;
const int sc_minimize = 0xf020;
const int sc_maximize = 0xf030;
protected override void wndproc(ref message m)
{
if (m.msg == wm_syscommand)
{
if (m.wparam.toint32() == sc_minimize)
{
this.visible = false;
return;
}
}
base.wndproc(ref m);
}