当前位置: 编程技术>.net/c#/asp.net
c# 重载WndProc,实现重写“最小化”的实现方法
来源: 互联网 发布时间:2014-10-13
本文导语: 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; //显示系统托盘图标...
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);
}