WinForm special effect: intercepts clicks on various parts of the form.
The title bar of windows forms cannot be controlled directly through some default events. You Need To Know windows messages related to WM_NCHITTEST. The following example demonstrates the simplest effect piece: it will swap the effects of the customer area and the title bar. For example, you cannot hold down the title bar and drag the form, but instead move it to the customer area, disabled. Among them, m. Result is defined from-2 to 21, which correspond to each part of the entire form, for example, 1 represents the customer zone, 8 represents the minimization button, and so on.
[Csharp]View plaincopy
- Using System;
- Using System. Windows. Forms;
- Namespace WindowsApplication42
- {
- Public partial class Form1: Form
- {
- Public Form1 () {InitializeComponent (); this. Text = "double-click the icon in the upper left corner to close the form ";}
- // Block the title bar cursor position event
- Const int WM_NCHITTEST = 0x84;
- Protected override void WndProc (ref Message m)
- {
- Base. WndProc (ref m); if (m. Msg = WM_NCHITTEST)
- {
- Switch (m. Result. ToInt32 ())
- {
- Case 1: // customer Zone
- M. Result = new IntPtr (2); break;
- Case 2: // Title Bar
- M. Result = new IntPtr (1); break;
- Case 20: m. Result = new IntPtr (0); break;
- Default: Console. WriteLine (m); break;
- }
- }
- }
- }
- }
The meanings of constants are as follows: Extract from Delphi7 VCL:
[Html]View plaincopy
- {WM_NCHITTEST and MOUSEHOOKSTRUCT Mouse Position Codes}
- {$ EXTERNALSYM policror}
- Ror =-2;
- {$ Externalsym httransparent}
- HTTRANSPARENT =-1;
- {$ Externalsym htnowhere}
- HTNOWHERE = 0;
- {$ Externalsym htclient}
- HTCLIENT = 1;
- {$ Externalsym htcaption}
- HTCAPTION = 2;
- {$ Externalsym htsysmenu}
- HTSYSMENU = 3;
- {$ Externalsym htgrowbox}
- HTGROWBOX = 4;
- {$ Externalsym htsize}
- HTSIZE = HTGROWBOX;
- {$ Externalsym htmenu}
- HTMENU = 5;
- {$ Externalsym hthscroll}
- HTHSCROLL = 6;
- {$ Externalsym htvscroll}
- HTVSCROLL = 7;
- {$ Externalsym htminbutton}
- HTMINBUTTON = 8;
- {$ Externalsym htmaxbutton}
- HTMAXBUTTON = 9;
- {$ Externalsym htleft}
- HTLEFT = 10;
- {$ Externalsym htright}
- HTRIGHT = 11;
- {$ Externalsym httop}
- HTTOP = 12;
- {$ Externalsym httopleft}
- HTTOPLEFT = 13;
- {$ Externalsym httopright}
- HTTOPRIGHT = 14;
- {$ Externalsym htbottom}
- HTBOTTOM = 15;
- {$ Externalsym htbottomleft}
- HTBOTTOMLEFT = 16;
- {$ Externalsym htbottomright}
- HTBOTTOMRIGHT = 17;
- {$ Externalsym htborder}
- HTBORDER = 18;
- {$ Externalsym htreduce}
- HTREDUCE = HTMINBUTTON;
- {$ Externalsym htzoom}
- HTZOOM = HTMAXBUTTON;
- {$ Externalsym htsizefirst}
- HTSIZEFIRST = HTLEFT;
- {$ Externalsym htsizelast}
- HTSIZELAST = HTBOTTOMRIGHT;
- {$ Externalsym htobject}
- HTOBJECT = 19;
- {$ Externalsym htclose}
- HTCLOSE = 20;
- {$ Externalsym hthelp}
- HTHELP = 21;
C # How does winform control the pop-up position of the form?
The reload constructor of the form to be popped up transfers coordinates to the past.
Modify the location attribute of the form.
Position of winform display
Private void button#click (object sender, EventArgs e)
{
Form2 frm2 = new Form2 ();
// Frm2.Location = new Point (this. Left, this. Top );
Frm2.StartPosition = FormStartPosition. Manual;
Frm2.Left = this. textBox1.Left + this. Left;
Frm2.Top = this. textBox1.Top + this. Top + this. textBox1.Height + this. Height-this. ClientRectangle. Height;
Frm2.Show ();
}
This. Height-this. ClientRectangle. Height this is the title bar Height