How do I force a Windows Form application to exit
Your main form is an object of type System.Windows.Forms.Form. Use its Close method to exit the application. If you wish to Exit from a Form’s constructor, this will not work. A workaround is to set a boolean flag that you can later check in the Form’s Load method to call Close if required. Another way is to use the Application.Exit() method.
How do I cancel a context menu programatically?
First keep track of which control is showing the ContextMenu by listening to the menu’s Popup event and querying for the SourceControl. Then when you are ready to cancel the popup, do as follows: [C#] [DllImport(‘user32.dll’, CharSet=CharSet.Auto)] extern internal static IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); private void Timer_Tick(object sender, EventArgs e) { if(menuSourceControl != null) SendMessage(menuSourceControl.Handle, 0x001F/*WM_CANCELMODE*/, IntPtr.Zero, IntPtr.Zero); } [VB.Net] _ extern internal static IntPtr SendMessage(IntPtr hWnd, Integer msg, IntPtr wParam, IntPtr lParam) Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs) If Not menuSourceControl Is Nothing Then SendMessage(menuSourceControl.Handle, 0x001F, IntPtr.Zero, IntPtr.Zero) End If End Sub
How do I invalidate a control including it’s NonClient area?
You can do so as follows in your Control: private void InvalidateWindow() { NativeMethods.RedrawWindow(this.Handle, IntPtr.Zero, IntPtr.Zero, 0x0400/*RDW_FRAME*/ | 0x0100/*RDW_UPDATENOW*/ | 0x0001/*RDW_INVALIDATE*/); }
How do I provide a 1 pixel border in the NonClient area of my Control?
You will have to first provide some space in the NC area by setting the WS_BORDER flag in CreateParams and then draw the border yourself by listening to the WM_NCPAINT message in your Control, as follows: protected override CreateParams CreateParams { get { System.Windows.Forms.CreateParams cp = base.CreateParams; if(this.needFlatBorder) { cparams.ExStyle &= ~512 /*WS_EX_CLIENTEDGE*/; cparams.Style &= ~8388608 /*WS_BORDER*/; cp.Style |= 0x800000; // WS_BORDER } } } protected override void WndProc(ref Message m) { if(m.Msg == 133/*WM_NCPAINT*/) { this.DrawFlatNCBorder(ref m); } base.WndProc(ref m); } private void DrawFlatNCBorder(ref Message msg) { IntPtr hRgn1 = (IntPtr) msg.WParam; // The update region is clipped to the window frame. When wParam is 1, the entire window frame needs to be updated. IntPtr hdc = NativeMethods.GetDCEx(msg.HWnd, hRgn1, 1/*DCX_WINDOW*/|0x0020/*DCX_PARENTCLIP*/); if (hdc != IntPtr.Zero) { using (Graphics g = Graphics.FromHdc(hdc)) { Rectangle bounds = new Rectangle(0,0,this.Width,this.Height); ControlPaint.DrawBorder(g,bounds,this.borderColor,ButtonBorderStyle.Solid); // create a clipping region for remaining parts to be drawn excluding // the border we did just drew bounds.Inflate(-1, -1); IntPtr hRgn2 = NativeMethods.CreateRectRgn(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom); if(hRgn2 == (IntPtr)1) { // Provide a new clipping region. msg.WParam = (IntPtr) hRgn2; } else { // combine with existing clipping region. NativeMethods.CombineRgn(hRgn1, hRgn1, hRgn2, NativeMethods.RGN_AND); NativeMethods.DeleteObject(hRgn2); } } msg.Result = (IntPtr) 1; NativeMethods.ReleaseDC(msg.HWnd, hdc); } Invalidate(); }
How do I provide a 2 pixel 3d border in the Non-Client area of my Control derived class?
You can do so as follows by overriding the CreateParams property in your Control. The advantage with this approach is that drawing is handled by the system as soon as you set the flag below. protected override CreateParams CreateParams { get { CreateParams cparams; cparams = base.CreateParams; if(this.need3DBorder) { cparams.ExStyle &= ~512; cparams.Style &= ~8388608 /*WS_BORDER*/; cparams.ExStyle = cparams.ExStyle | 512 /*WS_EX_DLGFRAME*/; } return cparams; } }