我是编程的新手。我正在编写一个基于对话框的应用程序,它有一个静态控件。使用
Using
void CMy1stDlg::OnMouseMove(UINT nFlags, CPoint point)
{
if (this == GetCapture())
{
CClientDC aDC(this);
aDC.SetPixel(point, RGB(255,0,0));
}
}
但是我想要的是鼠标的轨迹只在静态窗口内绘制。我this
在 MSDN 中找不到引用,我不知道为什么下面的方法失败了。
void CMy1stDlg::OnMouseMove(UINT nFlags, CPoint point)
{
CWnd* pMYSTATIC = GetDlgItem (IDC_MYSTATIC); //IDC_MYSTATIC is the ID of the static control
if (pMYSTATIC == GetCapture())
{
CClientDC aDC(pMYSTATIC);
aDC.SetPixel(point, RGB(255,0,0));
}
}
我怎样才能得到我想要的?是否有任何方法可以为类似于 的静态窗口获取某些东西this
?我将不胜感激。
好的,试试这个:
void CMy1stDlg::OnMouseMove(UINT nFlags, CPoint point)
{
CRect rect;
// Get static control's rectangle
GetDlgItem(IDC_MYSTATIC)->GetWindowRect(&rect);
// Convert it to client coordinates
ScreenToClient(&rect);
// Check if mouse pointer is inside the static window, if so draw the pixel
if (rect.PtInRect(point))
{
CClientDC dc(this);
dc.SetPixel(point.x, point.y, RGB(255,0,0));
}
}
在检查是否绘制像素之前,此代码可能也需要一些修复,例如缩小矩形(到其仅客户端区域)。
请注意,您不需要检查GetCapture()
;如果您的对话框没有捕获鼠标,它无论如何都不会收到此消息。
此外,所有这些功能都是 Windows SDK 的包装器,例如ClientDC()
类,基本上包装GetDC()
/ ReleaseDC()
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句