问题描述
我是编程的新手.我正在写一个基于对话框的应用程序,上面有一个静态控件.使用
I am a newcomer to programming. I am writing a dialog based application which has a static control on it. Using
Using
void CMy1stDlg::OnMouseMove(UINT nFlags, CPoint point)
{
if (this == GetCapture())
{
CClientDC aDC(this);
aDC.SetPixel(point, RGB(255,0,0));
}
}
我可以创建如下结果
I can create results like
但是我想要的是鼠标的位置仅在静态窗口内绘制.我在MSDN中找不到对this
的引用,也不知道为什么以下方法失败.
However what I want is that the locus of mouse is only drawn within the static window. I can't find the reference to this
in MSDN and I don't know why the following method fails.
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
的内容?我将不胜感激.
How can I get what I want? Are there any methods to get something for the static window analogous to this
? I will appreciate any help with these.
推荐答案
好的,请尝试以下操作:
OK, try 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));
}
}
此代码可能还需要进行一些修复,例如,在检查是否绘制像素之前,将矩形缩小(缩小到仅用于客户的区域).
This code may need some fixes too, eg shrink the rectangle (to its client-only area), before checking whether to draw the pixel.
请注意,您无需检查GetCapture()
;如果您的对话框没有捕获鼠标,则无论如何都不会收到此消息.
Please note that you don't need to check GetCapture()
; if your dialog hasn't captured the mouse it won't be receiving this message anyway.
此外,所有这些功能都是Windows SDK功能的包装,例如ClientDC()
类,基本上包装了GetDC()
/ReleaseDC()
.
Also, all these functions are wrappers of Windows SDK ones, eg the ClientDC()
class, basically wraps GetDC()
/ReleaseDC()
.
这篇关于类似于`this`的子窗口的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!