SoFunction
Updated on 2025-03-08

A summary of several practical techniques in WinForm

This article summarizes several common practical techniques in WinForm, which are of great reference value for C# program development. The specific analysis is as follows:

1. Close button in the upper right corner of the block form

1. Rewrite OnClosing

protected override void OnClosing(CancelEventArgs e)
{
  if()
  {
    =true;
 //
 // WHATE TODO 
 //
  }
}

2. Rewrite WndProc

protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
if ( == WM_SYSCOMMAND && (int)  == SC_CLOSE)
{
// User clicked close button
 = ;
return;
}
(ref m);
}

For more methods, please refer to:
/u/20091220/21/?94449
/u/20090419/18/

2. Block CTRL-V

The TextBox control in WinForm has no way to block the CTRL-V clipboard paste action. If you need an input box but don't want the user to paste the clipboard content, you can use the RichTextBox control instead and block the CTRL-V key in KeyDown. Example:

private void richTextBox1_KeyDown(object sender,  e)
{
if( && ==)
 = true;
}

3. Application singleton operation

#region Single instance run/// <summary>
/// Single instance running/// </summary>
/// <param name="frm">The main form to be run</param>public static void SingleRun(Form frm)
{
   mutex = new (false,"SINGLE_INSTANCE_MUTEX");
  if (!(0, false))
  {
 ();
 mutex = null;
  }
  if (mutex != null)
  {
 (frm);
  }
  else
  {
 ("Application started","Information prompt",,);
  }
}
#endregion

4. Convert controls into circles

 #region Convert control to circle [("gdi32")]
 private static extern IntPtr BeginPath(IntPtr hdc);
 [("gdi32")]
 private static extern int SetBkMode(IntPtr hdc,int nBkMode); 
 const int TRANSPARENT=1;
 [("gdi32")]   
 private static extern IntPtr EndPath(IntPtr hdc);
 [("gdi32")]
 private static extern IntPtr PathToRegion(IntPtr hdc);
 [("gdi32")]
 private static extern int Ellipse(IntPtr hdc,int x1,int y1,int x2,int y2);
 [("user32")]
 private static extern IntPtr SetWindowRgn(IntPtr hwnd,IntPtr hRgn,bool bRedraw);
 [("user32")]
 private static extern IntPtr GetDC(IntPtr hwnd);
 /// &lt;summary&gt;
 /// Convert the control to a circle /// &lt;/summary&gt;
 /// <param name="control">Control name</param> public static void MakeControlToCircle(Control[] control)
 {
  IntPtr dc;
  IntPtr region;
  for(int i=0;i&lt;;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. Only one window is opened in the same application

/// &lt;summary&gt;
/// Only one opens in the same application/// &lt;/summary&gt;
/// <param name="frmOpen">Form instance to be opened</param>/// &lt;returns&gt;&lt;/returns&gt;
public static void OpenSeamForm(Form frmOpen)
{
  foreach (Form frm in )
  {
 if ( == )
 {
    = true;
    = ;
   ();
   return;
 }
  }
  ();
}

I believe that the examples described in this article can be of great help to everyone's WinForm programming.