C++获取程序窗口外的鼠标坐标

时间:2021-03-19 08:08:41

首先 你可以在OnInitDialog里设置一个定时器
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
SetTimer(1,100,NULL);//时间设置短一点 显示的也快点
return TRUE; // return TRUE unless you set the focus to a control

然后为对话框添加WM_TIME消息
void CAdcDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
POINT pos;
GetCursorPos(&pos); //取鼠标的坐标
CString str;
str.Format("%d,%d",pos.x,pos.y);
m_dd=str;
UpdateData(FALSE);
CDialog::OnTimer(nIDEvent);
}