C # in the potential to drag the form to the end

Source: Internet
Author: User
Tags implement min return

Problem Description:

Want to be in. NET implementation of some irregular forms, no caption,formborderstyle = System.Windows.Forms.FormBorderStyle.None; drag, minimize, maximize, and implement special operations (as shown in Figure 1) )。 Drag and drop the form in the yellow area of area 1. Minimize the form in the Orange area, area 2, and close the form in the blue area, area 3, and implement special operations on the form (such as double form) in the green area, area 4.

(Figure 1)

  Problem implementation:

  The first method: Add a label to add the click event to the label. (as shown in Figure 2) If you want to do this in this way, cut the plot for each color area and make sure that you have the correct slice length and width, and then set the label background to this picture.

(Figure 2)

Handle their click events, and drag and drop to handle MouseDown MouseUp events.

The second way: do not add label only to handle mouse events, judge the location of the mouse and then decide what to do, this method is very resource-intensive, each mouse click to determine whether the mouse in a region and then decide whether to deal with. But this process is packed in polymorphic form. The program looks more neat.

Defining constants
private point Point;
Private Const int dragmove=172;
Private Const int dragmin=72;
Private Const int dragclose=72;
Private Const int dragdouble=78;
Private Const int dragheight=29;
Private Mousehandleenum Dragenum;

Defining MouseDown Events
private void Dragmain_mousedown (object sender, System.Windows.Forms.MouseEventArgs e)
{
Point. x=e.x;
Point. Y=e.y;
if (e.y<dragheight)
{
if (E.x<dragmove)
{
Dragenum = Mousehandleenum.move;
Return
}
if (e.x<dragmove+dragmin)
{
Dragenum = Mousehandleenum.min;
Return
}
if (e.x<dragmove+dragmin+dragclose)
{
Dragenum = Mousehandleenum.close;
Return
}
if (e.x<dragmove+dragmin+dragclose+dragdouble)
{
Dragenum = mousehandleenum.double;
Return
}
}

}


Defining MouseUp Events
private void Dragmain_mouseup (object sender, System.Windows.Forms.MouseEventArgs e)
{
Point. X=e.x-point.x;
Point. Y=e.y-point.y;
Idragmouse Idragmouse;
Switch (dragenum)
{
Case Mousehandleenum.move:
Idragmouse = new MouseMove (point,this);
Break
Case Mousehandleenum.min:
Idragmouse = new Mousemin (point,this);
Break
Case Mousehandleenum.close:
Idragmouse = new Mouseclose (point,this);
Break
Case Mousehandleenum.double:
Idragmouse = new Mousedouble (point,this);
Break
Default
Idragmouse = null;
Break
}
if (idragmouse!=null)
Idragmouse.mousedo ();
}
}

Define base class
Namespace Dragmouse
{
public enum Mousehandleenum
{
None=0,
Move=1,
min=2,
Close=3,
Double=4,
}
public class Dragmousebase
{
protected point Point;
public form form;
Public Dragmousebase (Point point, Form form)
{
This.point = point;
This.form = form;
}
}
}

Defining interfaces
Namespace Dragmouse
{
<summary>
///
</summary>
public interface Idragmouse
{
void Mousedo ();
}
}

Drag and drop operation
Namespace Dragmouse
{
<summary>
///
</summary>
public class Mouseclose:dragmousebase,idragmouse
{
Public Mouseclose (Point Point,form Form): Base (Point,form)
{
//
Todo:add constructor Logic here
//
}
#region Idragmouse Members

public void Mousedo ()
{
Application.exit ();
Todo:add Mouseclose.mousedo Implementation
}

#endregion
}
}

Other operations are similar.

 The third method is to use the responsibility chain this design pattern to wrap the mouse click operation, assign the operation to each responsibility chain node, is the program more object-oriented, has the better extensibility.

Two mouse events
private void Dragmain_mousedown (object sender, System.Windows.Forms.MouseEventArgs e)
{
Request. GetInformation (E.X,E.Y);

}


private void Dragmain_mouseup (object sender, System.Windows.Forms.MouseEventArgs e)
{
Request. Setscreenpoint (E.X,E.Y);

}

The encapsulated Request class
public class Request
{
public int Iscreenx;
public int iscreeny;

public int EX;
public int EY;

public ReadOnly int Yhigh;
public ReadOnly int draglength;
public ReadOnly int minlength;
public ReadOnly int closelength;
public ReadOnly int doublelength;

Private DragHandler DragHandler;
Private Minhandler Minhandler;
Private Closehandler Closehandler;
Private Doublehandler Doublehandler;

Public Form ParentForm;

public void Setscreenpoint (int ix,int IY)
{
Iscreenx = IX;
Iscreeny = IY;
Draghandler.handlerequest (this);
}

public void getinformation (int ex,int ey)
{
Ex=ex;
Ey=ey;
}

Public Request (int yhigh,int draglength,form Form)
{
Yhigh = Yhigh;
Draglength = Draglength;
ParentForm = form;
DragHandler = new DragHandler ();
Minhandler =new Minhandler ();
Closehandler = new Closehandler ();
Doublehandler = new Doublehandler ();

Draghandler.setsuccessor (Minhandler);
Minhandler.setsuccessor (Closehandler);
Closehandler.setsuccessor (Doublehandler);
}


Public Request (int yhigh,int draglength,int minlength,form Form): This (yhigh,draglength,form)
{
MinLength = minlength;
}


Public Request (int yhigh,int draglength,int minlength,int closelength,form Form): This (Yhigh,draglength,minlength, Form
{
Closelength = Closelength;
}


Public Request (int yhigh,int draglength,int minlength,int closelength, int doublelength, form form): This (Yhigh, Draglength,minlength,closelength,form)
{
Doublelength = Doublelength;
}

}

Drag and drop operation
public class Draghandler:handler
{
Override public void HandleRequest (Request request)
{
Determine if we can handle the request
if ((Request.ey<request.yhigh) && (request.ex<request.draglength))//some complex decision making!
{
Request.parentForm.Left + = Request.iscreenx-request.ex;
Request.parentForm.Top + = Request.iscreeny-request.ey;

Request handling code goes here
}
Else
{
Not handled here-pass on and next in the chain
if (Successorhandler!= null)
Successorhandler.handlerequest (Request);
}
}
}

Other operations are similar

The Fourth approach:(only ideas have not found a successful implementation)

In MFC, you can use PostMessage or SENDMESSAG to send messages, when the mouse clicks, but not on the form's captiontitle, sends a message to tell the system mouse in Captiontitle (each window titlebar), The drag and drop of the window can be hosted by the system. You now have a mouse-click anywhere in the window to drag and drop the form. But not the diversification that is required to achieve the above.

if (point.y<this->m_height)
{
Send a message to the system to disguise the mouse on caption Bar.
if (Point.x<this->m_drag)
{
PostMessage (Wm_nclbuttondown,
Htcaption,
Makelparam (POINT.X,POINT.Y));
Return
}
if (Point.x<this->m_drag+this->m_min&&point.x>this->m_drag)
{
PostMessage (Wm_nclbuttondown,
Htminbutton,
Makelparam (POINT.X,POINT.Y));
Return
}
if (point.x<this->m_drag+this->m_min+this->m_close&&point.x>this->m_drag+this->m_ Min)
{
PostMessage (Wm_nclbuttondown,
Htclose,
Makelparam (POINT.X,POINT.Y));
Return
}
if (point.x<this->m_drag+this->m_min+this->m_close+this->m_double&&point.x>this->m _drag+this->m_min+this->m_close)
{
CRgn *rgn = new CRgn ();
CRect *rect =new CRect ();
This->getwindowrect (*rect);
This->setwindowrgn (*rgn,true);
Return
}

}



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.