The MFC cursor moves to the button when the edit box displays the time and displays the cursor's window position and client area location

Source: Internet
Author: User

Building an MFC Application

Choose a Dialog based box

Add a button Three edit box, three edit boxes to display the system time, the cursor in the location of Windows, the cursor in the client position.

Change the button's captain to Time,id to Idc_time.

Click Project/Class Wizard/member variables/, add variable M_time,m_showtime,m_client,m_window for buttons and edit boxes (there are many methods).

Note that the category is control and the variable types are CButton and CEdit, respectively

Click on the message in the Class Wizard to find MouseMove, double-click,


The above red characters can be added directly in the code as follows: (Red for adding parts

In the Dlg.h

Public
CButton M_button;
CEdit M_showtime;
CEdit m_client;
CEdit M_window;
afx_msg void OnMouseMove (UINT nflags, CPoint point);

In the Dlg.cpp

void C cursor Movement DLG::D odataexchange (cdataexchange* PDX)
{
CDialogEx::D odataexchange (PDX);
DDX_Control (PDX, Idc_time, M_button);
DDX_Control (PDX, idc_edit1, m_showtime);
DDX_Control (PDX, Idc_edit2, m_client);
DDX_Control (PDX, Idc_edit3, M_window);
}


BEGIN_MESSAGE_MAP (c cursor Move dlg, CDialogEx)
On_wm_syscommand ()
On_wm_paint ()
On_wm_querydragicon ()
On_wm_mousemove ()
End_message_map ()


void C Mobile Mouse display Dlg::onmousemove (UINT nflags, CPoint Point)
{
/* below to get the coordinates of the mouse current point above the screen


(x, y) coordinates are referenced by the entire screen


*/
CString M_cspointscreen;
CPoint p;
GetCursorPos (&P);
M_cspointscreen.format (_t ("%d,%d"), p.x, P.Y);
M_window. SetWindowText (m_cspointscreen);


/* The coordinates above to get the current point of the mouse in the window (client area coordinates)

(x, y) coordinates are referenced by the customer area origin (that is, below the window title, left border to right).


CString M_cspointwindow;
M_cspointwindow.format (_t ("%d,%d"), Point.x, POINT.Y);
m_client. SetWindowText (M_cspointwindow);

Implemented to = when the cursor is on the button, the system time is displayed in edit box 1
CRect rect;

The following four Behavior button area client coordinates
rect.bottom=122;
rect.top=11;
rect.left=11;
rect.right=151;
/*rect. Bottomright= (152.111);
Rect. Topleft= (11,17); *//error

InvalidateRect (&rect); UpdateWindow ();

Get button area can be used GetDlgItem (idc_time)->getclientrect (&rect);

Here are some other methods, but they are not very handy.

GetDlgItem (idc_time)->getclientrect (&rect);//inaccurate

M_button. GetClientRect (&rect);//Not sensitive


GetWindowRect (GetDlgItem (idc_time), &rect);


// GetDlgItem (idc_time)->getwindowrect (&rect);//Gets the position of the control relative to the screen
ScreenToClient (rect);//convert to relative position on dialog box

Show System time
if (rect. PtInRect (point))
{


CString str;
CTime time = Ctime::getcurrenttime ();
str = time. Format (_t ("%y-%m-%d%h:%m:%s%A"));


Setdlgitemtext (idc_edit1, str);//mouse has in the rectangle.
To add your process code ...
}
Else
{
M_showtime. SetWindowText (_t ("")); Mouse has leave the rectangle.
To add your process code ...
}
Cdialogex::onmousemove (nflags, point);
}

The MFC cursor moves to the button when the edit box displays the time and displays the cursor's window position and client area location

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.