Two ways to move a C # borderless form

Source: Internet
Author: User

Reprint: http://blog.csdn.net/dxsh126/article/details/2940226

First, you need to use a WIMDOWSAPI function, so you must introduce
Using System.Runtime.InteropServices;
namespaces;

Then, there are two ways to use the API, one that does not need to override the WndProc window procedure in a way that does not require an API function. Another method requires two:
SendMessage send message as specified window procedure
ReleaseCapture Release mouse capture

Finally, there are some necessary constant declarations that can be found in the MSDN or CPP header files:
Private Const INT WM_SYSCOMMAND = 0x0112;//System Information when you click the icon in the upper-left corner of the window
Private Const int sc_move = 0xf010;//Mobile Information
Private Const INT htcaption = 0x0002;//indicates system information when the mouse is in the window title bar
Private Const int wm_nchittest = 0x84;//A message sent by the mouse in the form client area (other than the title bar and border)
Private Const int htclient = 0x1;//represents the mouse in the window client area of the system message
Private Const int sc_maximize = 0xf030;//maximized information
Private Const int sc_minimize = 0xf020;//minimized information

Here we go.

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.Runtime.InteropServices;

Namespace Windowsapplicationtestnoboarderandmove
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}

[DllImport ("User32.dll")]
public static extern bool ReleaseCapture ();

[DllImport ("User32.dll")]
public static extern bool SendMessage (INTPTR hwnd, int wmsg, int wParam, int lParam);

Private Const INT WM_SYSCOMMAND = 0x0112;//System Information when you click the icon in the upper-left corner of the window
Private Const int sc_move = 0xf010;//Mobile Information
Private Const INT htcaption = 0x0002;//indicates system information when the mouse is in the window title bar
Private Const int wm_nchittest = 0x84;//A message sent by the mouse in the form client area (other than the title bar and border)
Private Const int htclient = 0x1;//represents the mouse in the window client area of the system message
Private Const int sc_maximize = 0xf030;//maximized information
Private Const int sc_minimize = 0xf020;//minimized information

private void Closetoolstripmenuitem_click (object sender, EventArgs e)
{
Application.exit ();
}

The following code I use///commented, this is two ways to implement

private void Form1_mousedown (object sender, MouseEventArgs e)
//{
ReleaseCapture ();//First release the mouse focus capture so that no more wm_nchittest messages are emitted
SendMessage (this. Handle, Wm_syscommand, Sc_move + htcaption, 0);//Then send a message to the current form, the message is move + means the mouse is on the title bar
//}


If you use this method of rewriting, comment out the above section ...

/**/
protected override void WndProc (ref Message m)
{
Switch (m.msg)
{
Case Wm_syscommand:
if (M.wparam = = (INTPTR) sc_maximize)
{
M.wparam = (IntPtr) sc_minimize;
}
Break
Case WM_NCHITTEST://If the mouse moves or clicks
Base. WndProc (ref m);//Call the Base class window procedure--wndproc method to process the message
if (M.result = = (INTPTR) htclient)//If the return is Htclient
{
M.result = (IntPtr) htcaption;//change it to htcaption
return;//Direct Return Exit method
}
Break
}
Base. WndProc (ref m); If you are not moving the mouse or clicking a message, call the base class's window procedure for processing
}


There is a problem with this rewrite method.
Guess what happens if you double-click the mouse on the form? Hehe, the window realizes maximization ...
}
}


Reprint: http://blog.csdn.net/dxsh126/article/details/2940226

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.