【发布时间】:2017-10-01 17:19:02
【问题描述】:
我需要编写一个 C# 程序来识别光标下的窗口并在其上绘制边框。
我可以轻松获得一个 Windows 处理程序:
...
Point point;
WinApi.GetCursorPos(out point);
WinApi.WindowFromPoint(point);
...
但我不能在那个窗口上画画……
public static void drawSelectionRectangle(IntPtr handler)
{
Rectangle rectangle;
WinApi.GetWindowRect(handler, out rectangle);
WinApi.PAINTSTRUCT paintProperties;
IntPtr paintContext = WinApi.BeginPaint(handler, out paintProperties);
IntPtr pen = WinApi.CreatePen(WinApi.PenStyle.PS_SOLID, 5, (uint) ColorTranslator.ToWin32(Color.Red));
WinApi.SelectObject(paintContext, pen);
WinApi.Rectangle(paintContext, rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
WinApi.ValidateRect(handler, IntPtr.Zero);
WinApi.EndPaint(handler, ref paintProperties);
}
我调用了drawSelectionRectangle(IntPtr handler) 一次(通过按钮单击)和循环调用(通过 MyForm 的onPaint() 方法,而不是我想要绘制的表单)。这似乎不起作用。
请帮帮我。我不知道该怎么办。
【问题讨论】:
-
不行。窗户不属于你。所有者将在您可能成功绘制的任何内容上进行绘制。您需要了解 Win32 绘图的工作原理。不管是什么问题,这都不是解决方案。