欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WinForm中的幾個實用技巧匯總

 更新時間:2014年08月26日 11:49:00   投稿:shichen2014  
這篇文章主要介紹了WinForm中的幾個實用技巧,是進(jìn)行C#程序設(shè)計中經(jīng)常用到的技巧,需要的朋友可以參考下

本文匯總了幾個WinForm中常見的實用技巧,對于C#程序開發(fā)有著很好的參考借鑒價值。具體分析如下:

一、屏蔽窗體右上角關(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控件沒有辦法屏蔽CTRL-V的剪貼板粘貼動作,如果需要一個輸入框,但是不希望用戶粘貼剪貼板的內(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 單實例運(yùn)行
/// <summary>
/// 單實例運(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)用程序已啟動","信息提示",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)用程序中同一窗體只打開一個

/// <summary>
/// 在同一應(yīng)用程序中同一窗體只打開一個
/// </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程序設(shè)計可以帶來很大幫助。

相關(guān)文章

最新評論