WinForm special effect: intercepts clicks on various parts of the form.

Source: Internet
Author: User

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
  1. Using System;
  2. Using System. Windows. Forms;
  3. Namespace WindowsApplication42
  4. {
  5. Public partial class Form1: Form
  6. {
  7. Public Form1 () {InitializeComponent (); this. Text = "double-click the icon in the upper left corner to close the form ";}
  8. // Block the title bar cursor position event
  9. Const int WM_NCHITTEST = 0x84;
  10. Protected override void WndProc (ref Message m)
  11. {
  12. Base. WndProc (ref m); if (m. Msg = WM_NCHITTEST)
  13. {
  14. Switch (m. Result. ToInt32 ())
  15. {
  16. Case 1: // customer Zone
  17. M. Result = new IntPtr (2); break;
  18. Case 2: // Title Bar
  19. M. Result = new IntPtr (1); break;
  20. Case 20: m. Result = new IntPtr (0); break;
  21. Default: Console. WriteLine (m); break;
  22. }
  23. }
  24. }
  25. }
  26. }

The meanings of constants are as follows: Extract from Delphi7 VCL:

[Html]View plaincopy
  1. {WM_NCHITTEST and MOUSEHOOKSTRUCT Mouse Position Codes}
  2. {$ EXTERNALSYM policror}
  3. Ror =-2;
  4. {$ Externalsym httransparent}
  5. HTTRANSPARENT =-1;
  6. {$ Externalsym htnowhere}
  7. HTNOWHERE = 0;
  8. {$ Externalsym htclient}
  9. HTCLIENT = 1;
  10. {$ Externalsym htcaption}
  11. HTCAPTION = 2;
  12. {$ Externalsym htsysmenu}
  13. HTSYSMENU = 3;
  14. {$ Externalsym htgrowbox}
  15. HTGROWBOX = 4;
  16. {$ Externalsym htsize}
  17. HTSIZE = HTGROWBOX;
  18. {$ Externalsym htmenu}
  19. HTMENU = 5;
  20. {$ Externalsym hthscroll}
  21. HTHSCROLL = 6;
  22. {$ Externalsym htvscroll}
  23. HTVSCROLL = 7;
  24. {$ Externalsym htminbutton}
  25. HTMINBUTTON = 8;
  26. {$ Externalsym htmaxbutton}
  27. HTMAXBUTTON = 9;
  28. {$ Externalsym htleft}
  29. HTLEFT = 10;
  30. {$ Externalsym htright}
  31. HTRIGHT = 11;
  32. {$ Externalsym httop}
  33. HTTOP = 12;
  34. {$ Externalsym httopleft}
  35. HTTOPLEFT = 13;
  36. {$ Externalsym httopright}
  37. HTTOPRIGHT = 14;
  38. {$ Externalsym htbottom}
  39. HTBOTTOM = 15;
  40. {$ Externalsym htbottomleft}
  41. HTBOTTOMLEFT = 16;
  42. {$ Externalsym htbottomright}
  43. HTBOTTOMRIGHT = 17;
  44. {$ Externalsym htborder}
  45. HTBORDER = 18;
  46. {$ Externalsym htreduce}
  47. HTREDUCE = HTMINBUTTON;
  48. {$ Externalsym htzoom}
  49. HTZOOM = HTMAXBUTTON;
  50. {$ Externalsym htsizefirst}
  51. HTSIZEFIRST = HTLEFT;
  52. {$ Externalsym htsizelast}
  53. HTSIZELAST = HTBOTTOMRIGHT;
  54. {$ Externalsym htobject}
  55. HTOBJECT = 19;
  56. {$ Externalsym htclose}
  57. HTCLOSE = 20;
  58. {$ Externalsym hthelp}
  59. HTHELP = 21;

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

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.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.