C # Move the borderless form, right-click the taskbar menu, and adjust the size

Source: Internet
Author: User

Recently, I made a borderless main form interface, which encountered many problems and paid a lot of effort to solve the problem. After that, I wanted to write down the process and keep a souvenir, it also provides some help to netizens who encounter the same problem. Because there is no border, the buttons such as maximize, minimize, and close are gone, and the form cannot be dragged. the taskbar does not have a right-click menu and the size cannot be adjusted, when setting full screen, the entire screen is filled up, And the taskbar is covered. These are all problems. The following problems are solved one by one:

1. To maximize, minimize, and close the buttons, you only need to drag three common buttons. Of course, you need to set the background image, in order to achieve the beautiful result, there is nothing to say, it is very simple to three buttons, corresponding to the event respectively set the windowstate of the form to maximize, minimize, and close it.

 

2. There are many ways to drag a form. One is to use windows api and the other is to add a mousemove event. The following are two methods:

Method 1:

Add the following code to the form class:

using System.Runtime.InteropServices;     [DllImport("user32.dll")]     public static extern bool ReleaseCapture();     [DllImport("user32.dll")]     public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);     public const int WM_SYSCOMMAND = 0x0112;     public const int SC_MOVE = 0xF010;     public const int HTCAPTION = 0x0002; 

Then, add the following two sentences to the form MouseDown event:

ReleaseCapture ();
SendMessage (this. Handle, WM_SYSCOMMAND, SC _MOVE + HTCAPTION, 0 );
That's it.

Method 2:

Add two variables to the form class:

Point mouseOff;

Bool leftFlag;

Add the following code to the MouseDown, MouseUp, and MouseMove events of the form:

// MouseDown

            if (e.Button == MouseButtons.Left)            {                mouseOff = new Point(-e.X, -e.Y);                 leftFlag = true;            }

// MouseUp

            if (leftFlag)            {                leftFlag = false;            }

// MouseMove

            if (leftFlag)            {                Point mouseSet = System.Windows.Forms.Control.MousePosition;                mouseSet.Offset(mouseOff.X, mouseOff.Y);                  Location = mouseSet;            }

3. Right-click the taskbar and choose

Add the following code to the form class:

        [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]        public static extern int GetWindowLong(HandleRef hWnd, int nIndex);        [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]        public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);

Then add the following code to the constructor or form Load event:

            int WS_SYSMENU = 0x00080000;            int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));            SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU);

4. Adjust the form size

One solution seen on the Web is to add the following code in the Form class (from: http://blog.sina.com.cn/s/blog_7010d0100100lqpu.html)

# Region control change form size const int WM_NCHITTEST = 0x0084; const int HTLEFT = 10; // left boundary const int HTRIGHT = 11; // right boundary const int HTTOP = 12; // upper boundary const int HTTOPLEFT = 13; // upper left const int HTTOPRIGHT = 14; // upper right const int HTBOTTOM = 15; // lower boundary const int HTBOTTOMLEFT = 0x10; // const int HTBOTTOMRIGHT = 17 in the lower left corner; // protected override void WndProc (ref Message m) {base. wndProc (ref m); switch (m. msg) {case WM _ NCHITTEST: {Point vPoint = new Point (int) m. LParam & 0 xFFFF, (int) m. LParam> 16 & 0 xFFFF); vPoint = PointToClient (vPoint); // judgment: The mouse event takes effect only when the current form status is not maximized. if (this. windowState! = FormWindowState. maximized) {if (vPoint. X <= 5) {if (vPoint. Y <= 5) {m. result = (IntPtr) HTTOPLEFT;} else if (vPoint. y> = ClientSize. height-5) {m. result = (IntPtr) HTBOTTOMLEFT;} else {m. result = (IntPtr) HTLEFT ;}} else if (vPoint. x> = ClientSize. width-5) {if (vPoint. Y <= 5) {m. result = (IntPtr) HTTOPRIGHT;} else if (vPoint. y> = ClientSize. height-5) {m. result = (IntPtr) HTBOTTOMRIGHT;} else {m. result = (IntPtr) HTRIGHT ;}} else if (vPoint. Y <= 5) {m. result = (IntPtr) HTTOP;} else if (vPoint. y> = ClientSize. height-5) {m. result = (IntPtr) HTBOTTOM ;}} break ;}}# endregion

After the experiment, do not add SetWindowLong (new HandleRef (this, this. handle),-16, windowLong | WS_SYSMENU); this code is available, but the above Code is invalid after this sentence is added, that is to say, after adding the right-click menu of the taskbar with the previous method, the above Code cannot be used to adjust the size. Therefore, other solutions are collected, on the MSDN forum, I saw a good solution for a foreign user. I dragged a panel to the bottom right corner of the form, made the panel small, set the panel Cursor to SizeNWSE, and then on this panel
Add the following code to the MouseMove event:

            if (e.Button == MouseButtons.Left)            {                this.Size = new Size(this.PointToClient(MousePosition).X, this.PointToClient(MousePosition).Y);            }

This achieves a similar drag effect, but there is a drag icon in the lower right corner.

5. Solve the Problem of full screen after Maximization

Add the following code to the constructor:

 this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;          

 

So far, all the problems have been solved or roughly solved, and then we will beautify our forms.

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.