WinForm中的幾個(gè)實(shí)用技巧匯總
本文匯總了幾個(gè)WinForm中常見的實(shí)用技巧,對(duì)于C#程序開發(fā)有著很好的參考借鑒價(jià)值。具體分析如下:
一、屏蔽窗體右上角關(guān)閉按鈕
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
二、屏蔽CTRL-V
在WinForm中的TextBox控件沒(méi)有辦法屏蔽CTRL-V的剪貼板粘貼動(dòng)作,如果需要一個(gè)輸入框,但是不希望用戶粘貼剪貼板的內(nèi)容,可以改用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;
}
三、應(yīng)用程序單例運(yùn)行
#region 單實(shí)例運(yùn)行
/// <summary>
/// 單實(shí)例運(yùn)行
/// </summary>
/// <param name="frm">所要運(yùn)行的主窗體</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("應(yīng)用程序已啟動(dòng)","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
#endregion
四、將控件轉(zhuǎn)換成圓形
#region 將控件轉(zhuǎn)換為圓形
[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>
/// 將控件轉(zhuǎn)為圓形
/// </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
五、在同一應(yīng)用程序中同一窗體只打開一個(gè)
/// <summary>
/// 在同一應(yīng)用程序中同一窗體只打開一個(gè)
/// </summary>
/// <param name="frmOpen">要打開的窗體實(shí)例</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();
}
相信本文所述實(shí)例對(duì)大家WinForm程序設(shè)計(jì)可以帶來(lái)很大幫助。
- WinForm特效之桌面上的遮罩層實(shí)現(xiàn)方法
- winform異型不規(guī)則界面設(shè)計(jì)的實(shí)現(xiàn)方法
- WinForm的延時(shí)加載控件概述
- WinForm中DefWndProc、WndProc與IMessageFilter的區(qū)別
- C#中Winform窗體Form的關(guān)閉按鈕變灰色的方法
- C# Winform實(shí)現(xiàn)捕獲窗體最小化、最大化、關(guān)閉按鈕事件的方法
- WinForm實(shí)現(xiàn)跨進(jìn)程通信的方法
- Winform下實(shí)現(xiàn)圖片切換特效的方法
- WinForm中變Enter鍵為Tab鍵實(shí)現(xiàn)焦點(diǎn)轉(zhuǎn)移的方法
- 在多線程中調(diào)用winform窗體控件的實(shí)現(xiàn)方法
- Winform實(shí)現(xiàn)抓取web頁(yè)面內(nèi)容的方法
相關(guān)文章
C#中使用ADOMD.NET查詢多維數(shù)據(jù)集的實(shí)現(xiàn)方法
這篇文章主要介紹了C#中使用ADOMD.NET查詢多維數(shù)據(jù)集的實(shí)現(xiàn)方法,詳細(xì)講述了C#中使用ADOMD.NET查詢多維數(shù)據(jù)集的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2014-10-10
C#?在PDF中添加墨跡注釋Ink?Annotation的步驟詳解
PDF中的墨跡注釋表現(xiàn)為徒手涂鴉式的形狀,該類型的注釋,可任意指定形狀頂點(diǎn)的位置及個(gè)數(shù),通過(guò)指定的頂點(diǎn),程序?qū)⑦B接各點(diǎn)繪制成平滑的曲線,下面通過(guò)C#程序代碼介紹下在pdf中添加注釋的步驟,感興趣的朋友一起看看吧2022-02-02
C#中的文件路徑獲取函數(shù)和文件名字獲取函數(shù)小結(jié)
這篇文章主要介紹了C#中的文件路徑獲取函數(shù)和文件名字獲取函數(shù)小結(jié),本文講解了獲取絕對(duì)文件路徑、獲取文件名字、獲得包含 path 目錄信等內(nèi)容,需要的朋友可以參考下2015-01-01
c# 從內(nèi)存中釋放Selenium chromedriver.exe
這篇文章主要介紹了c# 從內(nèi)存中釋放Selenium chromedriver.exe的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01
C# 設(shè)計(jì)模式之單例模式歸納總結(jié)
這篇文章主要介紹了C#設(shè)計(jì)模式之單例模式實(shí)例講解,本文講解了單例模式的定義、單例模式的優(yōu)缺點(diǎn),需要的朋友可以參考下2017-04-04
C#鍵盤輸入回車鍵實(shí)現(xiàn)點(diǎn)擊按鈕效果的方法
這篇文章主要介紹了C#鍵盤輸入回車鍵實(shí)現(xiàn)點(diǎn)擊按鈕效果的方法,可實(shí)現(xiàn)用回車鍵代替點(diǎn)擊按鈕的功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09
C#發(fā)送HttpPost請(qǐng)求來(lái)調(diào)用WebService的方法
在C#中發(fā)送HttpPost請(qǐng)求來(lái)調(diào)用WebService中的MyAction方法,代碼如下:需要的朋友可以參考一下2013-03-03

