Adjust the form without borders with the mouse

Source: Internet
Author: User

The code here is used to simulate adjusting the size or position of a window with a mouse (preferably a form without an edge). It is interesting. I think it is easy to use and share it with you, hope that the experts will give you some advice to improve the program (modified). You can also use this method to perform window form control, in this way, you can adjust the size or position of your control at runtime. Note that wndproc is used for implementation:

Using system;
Using system. drawing;
Using system. Windows. forms;

Namespace size_moveform
{
/// <Summary>
/// Summary of formbase.
/// You can adjust the size and position of the form.
/// Class inherits a new form class or form instance with this feature
/// </Summary>
Public class formbase: system. Windows. Forms. Form
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;

Public formbase ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();

//
// Todo: add Any constructor code after initializecomponent calls
//
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
//
// Formbase
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (192,146 );
This. formborderstyle = system. Windows. Forms. formborderstyle. None;
This. Name = "formbase ";
This. Text = "formbase ";

}
# Endregion

Private const int wm_nchittest = 0x84; // move the mouse, hold down or release the system message
Private const int htclient = 0x1; // work zone
Private const int htsysmenu = 3; // System Menu
Private const int htcaption = 0x2; // Title Bar

Private const int htleft = 10; // left
Private const int htright = 11; // right
Private const int httop = 12; // up
Private const int httopleft = 13; // top left
Private const int httopright = 14; // top right
Private const int htbottom = 15; // downward
Private const int htbottomleft = 16; // bottom left
Private const int htbottomright = 17; // bottom right
Private int m_borderwidth = 2;
Private int m_captionheight = 20;

Private const int borderwidth = 5; // The width of the custom form edge

// You can adjust the size and position of the form.
Protected override void wndproc (ref message m)
{
If (M. MSG = wm_nchittest)
{
Base. wndproc (ref m );
If (designMode)
{
Return;
}
If (INT) M. Result = htclient)
{
M. hwnd = This. Handle;

System. Drawing. Rectangle rect = This. rectangletoscreen (this. clientrectangle );
Point c_pos = cursor. position;

If (cursor. position. x <= rect. Left + m_borderwidth) & (c_pos.y <= rect. Top + m_borderwidth ))
M. Result = (intptr) httopleft; // top left
Else if (c_pos.x> = rect. Left + rect. Width-m_BorderWidth) & (c_pos.y <= rect. Top + m_borderwidth ))
M. Result = (intptr) httopright; // upper right
Else if (c_pos.x <= rect. Left + m_borderwidth) & (c_pos.y> = rect. Top + rect. Height-m_BorderWidth ))
M. Result = (intptr) htbottomleft; // bottom left
Else if (c_pos.x> = rect. Left + rect. Width-m_BorderWidth) & (c_pos.y> = rect. Top + rect. Height-m_BorderWidth ))
M. Result = (intptr) htbottomright; // bottom right
Else if (c_pos.x <= rect. Left + m_BorderWidth-1)
M. Result = (intptr) htleft; // left
Else if (c_pos.x> = rect. Left + rect. Width-m_BorderWidth)
M. Result = (intptr) htright; // right
Else if (c_pos.y <= rect. Top + m_BorderWidth-1)
M. Result = (intptr) httop; //
Else if (c_pos.y> = rect. Top + rect. Height-m_BorderWidth)
M. Result = (intptr) htbottom; //
Else if (c_pos.y <= rect. Top + m_captionheight + m_borderwidth)
{
If (c_pos.x <= rect. Left + m_borderwidth + m_captionheight)
{
M. Result = (intptr) htsysmenu; // simulates the system menu. Double-click it to close the form.
}
Else
{
M. Result = (intptr) htcaption; // simulate the title bar, move or double-click to maximize or minimize the form
}
}
}
Return;
}
Else
{
Base. wndproc (ref m );
}
}
}
}

 

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.