WinForm中的幾個適用技能匯總。本站提示廣大學習愛好者:(WinForm中的幾個適用技能匯總)文章只能為提供參考,不一定能成為您想要的結果。以下是WinForm中的幾個適用技能匯總正文
本文匯總了幾個WinForm中罕見的適用技能,關於C#法式開辟有著很好的參考自創價值。詳細剖析以下:
1、屏障窗體右上角封閉按鈕
1.重寫OnClosing
protected override void OnClosing(CancelEventArgs e) { if(this.Visible) { e.Cancel=true; // // WHATE TODO // } }
2.重寫WndProc
protected override void WndProc(ref Message m) { const int WM_SYSCOMMAND = 0x0112; const int SC_CLOSE = 0xF060; if (m.Msg == WM_SYSCOMMAND && (int) m.WParam == SC_CLOSE) { // User clicked close button this.WindowState = FormWindowState.Minimized; return; } base.WndProc(ref m); }
更多辦法可以參考:
http://topic.csdn.net/u/20091220/21/5228d0d6-26aa-48b8-81aa-293f7c7339f8.html?94449
http://topic.csdn.net/u/20090419/18/970d8ad9-ed9a-4bd9-a623-81d23106545b.html
2、屏障CTRL-V
在WinForm中的TextBox控件沒有方法屏障CTRL-V的剪貼板粘貼舉措,假如須要一個輸出框,然則不願望用戶粘貼剪貼板的內容,可以改用RichTextBox控件,而且在KeyDown中屏障失落CTRL-V鍵,例子:
private void richTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.Control && e.KeyCode==Keys.V) e.Handled = true; }
3、運用法式單例運轉
#region 單實例運轉 /// <summary> /// 單實例運轉 /// </summary> /// <param name="frm">所要運轉的主窗體</param> public static void SingleRun(Form frm) { System.Threading.Mutex mutex = new System.Threading.Mutex(false,"SINGLE_INSTANCE_MUTEX"); if (!mutex.WaitOne(0, false)) { mutex.Close(); mutex = null; } if (mutex != null) { Application.Run(frm); } else { MessageBox.Show("運用法式已啟動","信息提醒",MessageBoxButtons.OK,MessageBoxIcon.Information); } } #endregion
4、將控件轉換成圓形
#region 將控件轉換為圓形 [System.Runtime.InteropServices.DllImport("gdi32")] private static extern IntPtr BeginPath(IntPtr hdc); [System.Runtime.InteropServices.DllImport("gdi32")] private static extern int SetBkMode(IntPtr hdc,int nBkMode); const int TRANSPARENT=1; [System.Runtime.InteropServices.DllImport("gdi32")] private static extern IntPtr EndPath(IntPtr hdc); [System.Runtime.InteropServices.DllImport("gdi32")] private static extern IntPtr PathToRegion(IntPtr hdc); [System.Runtime.InteropServices.DllImport("gdi32")] private static extern int Ellipse(IntPtr hdc,int x1,int y1,int x2,int y2); [System.Runtime.InteropServices.DllImport("user32")] private static extern IntPtr SetWindowRgn(IntPtr hwnd,IntPtr hRgn,bool bRedraw); [System.Runtime.InteropServices.DllImport("user32")] private static extern IntPtr GetDC(IntPtr hwnd); /// <summary> /// 將控件轉為圓形 /// </summary> /// <param name="control">控件名</param> public static void MakeControlToCircle(Control[] control) { IntPtr dc; IntPtr region; for(int i=0;i<control.Length;i++) { dc=GetDC(control[i].Handle); BeginPath(dc); SetBkMode(dc,TRANSPARENT); Ellipse(dc,0,0,control[i].Width-3,control[i].Height-2); EndPath(dc); region=PathToRegion(dc); SetWindowRgn(control[i].Handle,region,false); } } #endregion
5、在統一運用法式中統一窗體只翻開一個
/// <summary> /// 在統一運用法式中統一窗體只翻開一個 /// </summary> /// <param name="frmOpen">要翻開的窗體實例</param> /// <returns></returns> public static void OpenSeamForm(Form frmOpen) { foreach (Form frm in Application.OpenForms) { if (frm.Text == frmOpen.Text) { frm.Visible = true; frm.WindowState = FormWindowState.Normal; frm.Activate(); return; } } frmOpen.Show(); }
信任本文所述實例對年夜家WinForm法式設計可以帶來很年夜贊助。