C #, MFC, and Win32 -- move the Untitled window

Source: Internet
Author: User

This article introduces howProgramChina Mobile untitled window

 

C #

There are two methods to sum up in C:

1. You can control the status of the form by clicking and moving the left mouse button,CodeSee:

Point offset; Private Void Frm_mousedown ( Object Sender, mouseeventargs E) { Offset = New Point ( - E. X, - E. y ); } Private Void Frm_mousemove ( Object Sender, mouseeventargs E) { If (E. Button = Mousebuttons. Left) { Point newpoint = Control. mouseposition; Newpoint. offset (offset. X, offset. y ); Location = Newpoint; } }

 

ThisAlgorithmIt is also easy to analyze. Needless to say (note that the coordinates recorded in mousedown are relative to the coordinates of the form, while those recorded in mousemove are relative to the coordinates of the desktop ). However, the efficiency of this method is not very high.

2. Reload the wndproc Method

 Protected  Override  Void Wndproc (  Ref  Message m)
{
If (M. msg = Zero X 0201 ){ // Left-click message
M. msg = 0x00a1 ; // Change the message to a non-customer zone by pressing the mouse
M. lparam = Intptr. zero; // Default Value
M. wparam = New Intptr ( 2 ); // Place the cursor in the title bar
Base . Wndproc ( Ref M );
}
}

I feel that this method is best implemented in C.

 

MFC

MFC ProgramDivided into dialog box programs and document programs

Dialog Box:

There are two methods in the dialog box program:

1. Add a response to the message wm_lbuttondown in the dialog box and add the code

Void Ctestdlg: onlbuttondown (uint nflags, cpoint point)
{
Postmessage (wm_nclbuttondown, htcaption, makelparam (point. X, point. y ));

Cdialog: onlbuttondown (nflags, point );

 

2. Add a response to the message wm_nchittest and add the code.

 
Lresult ctestdlg: onnchittest (cpoint point)
{
Crect RC;
Getclientrect (&RC );
Clienttoscreen (&RC );
ReturnRC. ptinrect (point)?Htcaption: cdialog: onnchittest (point );

//Return cdialog: onnchittest (point );
}

 

In the MFC Documentation Program:

In the document program, I can't use the above method. I tried the above Code in the cview derived class and found that only the customer zone can be moved, rather than the entire program window. You can only use the first method in C.

The specific code is:

Define a cpoint offset field in the derived class of the cview class to record the coordinates when the mouse clicks

The message code is as follows:

VoidCtryview: onlbuttondown (uint nflags, cpoint point)
{
//Clienttoscreen (& Point );
Offset=Cpoint (-Point. X,-Point. y );

//This is too hard to reduce by 65.
Offset. Y-=65;
Cview: onlbuttondown (nflags, point );
}

The function that responds to mouse movement is:

Void  Ctryview: onmousemove (uint nflags, cpoint point)
{
Clienttoscreen ( & Point );
If (Nflags & Mk_lbutton)
{
Cpoint newpoint = Point;
Newpoint. offset (offset. X, offset. y );
Getparentframe () -> Setwindowpos ( & Wndnotopmost, newpoint. X, newpoint. y, 0 , 0 , Swp_nosize );
}

Cview: onmousemove (nflags, point );
}

 

The offset. y above must be subtracted from the height of the non-customer zone to make the program run properly. However, this 65 is too hard. If the program does not have a toolbar or the menu bar, it cannot be 65. Which of the following methods can be used to calculate the height !!

 

Win32

Add a response to the wm_lbuttondown message to the wndproc function. The Code is as follows:

CaseWm_lbuttondown: sendmessage (hwnd, wm_syscommand, SC _move+ Htcaption,0);Break;

 

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.