C# 捕獲窗體最小化、最大化事件
阿新 • • 發佈:2018-12-29
個人程式碼:
private void HospitalInformation_SizeChanged(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Maximized && btnModify.Top==btnClear.Top)//捕獲窗體最大化事件 { btnModify.Top += 20;//更改按鈕的位置 btnClear.Top += 60; btnAdd.Top += 100; dgvData.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;//更改dgvData的AuotSizeColumnsMode屬性 } if (this.WindowState == FormWindowState.Normal)//捕獲還原(Normal)事件 { btnModify.Top -= 20; btnClear.Top -= 60; btnAdd.Top -= 100; dgvData.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; } }
窗體的Deactivate事件,判斷窗體狀態是否為最小化,其他事件判斷會出現問題:
[csharp] view plaincopyprint?- privatevoid MainForm_Deactivate(object sender, EventArgs e)
- {
- if (this.WindowState == FormWindowState.Minimized)
- {
- this.notifyIcon1.Visible = true;
-
this
- this.ShowInTaskbar = false;
- }
- }
notifyIcon單擊顯示
- privatevoid notifyIcon1_Click(object sender, EventArgs e)
- {
- this.Show();
- this.ShowInTaskbar = true;
-
this.WindowState = FormWindowState.Normal;
- this.notifyIcon1.Visible = false;
- }
{
if(WindowState == FormWindowState.Maximized)
{
//最大化時所需的操作
MessageBox.Show("max");
}
else if(WindowState == FormWindowState.Minimized)
{
//最小化時所需的操作
MessageBox.Show("min");
}
}
*****但是問題出現了,最大化以後,內部的控制元件不能隨anchar發生變化。。。。。,問題出在哪裡呢?
於是又找到了一些程式碼,如下
雖然Form類沒有提供Minimize的事件,但還是可以通過過載Deactive來實現
當Form失去焦點後,測試WindowState取得Form狀態,若為Minimized既是最小化事件。
本例為最小化後隱藏視窗:
{
if (this.WindowState == FormWindowState.Minimized)
this.Visible = false;
}
還有種方法更加直接,過載WndProc:
constint WM_SYSCOMMAND =0x112;
constint SC_CLOSE =0xF060;
constint SC_MINIMIZE =0xF020;
constint SC_MAXIMIZE =0xF030;
protectedoverridevoid WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND)
{
if (m.WParam.ToInt32() == SC_MINIMIZE)
{
this.Visible =false;
return;
}
}
base.WndProc(ref m);
}