C# C.WinApi。在窗户上画
我需要写一个C程序来识别光标下的窗口,并在上面画一个边框 我可以很容易地得到一个windows处理程序:C# C.WinApi。在窗户上画,c#,winapi,drawing,gdi,C#,Winapi,Drawing,Gdi,我需要写一个C程序来识别光标下的窗口,并在上面画一个边框 我可以很容易地得到一个windows处理程序: ... Point point; WinApi.GetCursorPos(out point); WinApi.WindowFromPoint(point); ... 但我不能在那扇窗户上画画 public static void drawSelectionRectangle(IntPtr handler) { Rectangle rectangle; WinApi.Get
...
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);
}
我调用了drawSelectionRectangleIntPtr处理程序一次,方法是通过MyForm的onPaint方法单击按钮并循环,而不是在我想要绘制的表单上。这似乎不起作用
请帮帮我。我不知道该怎么办。这不是我问题的完全解决方案,但它是有效的 我想在目标窗口上绘制,若目标窗口位于其他窗口下,边框也必须位于其他窗口下……但当边框在所有窗口上绘制时,这是一个比没有更好的解决方案 我希望我的问题和解决方案能帮助别人。谢谢大家
public partial class Form1 : Form
{
private IntPtr selectedWindowHandler;
...
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Cursor.Current = new Cursor(Properties.Resources.aimImage.GetHicon());
mousePressed = true;
pictureBox_aimImage.Invalidate();
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
mousePressed = false;
invalidateAllWindows();
}
// this method for pictureBox1 which contain custom cursor
// to choose window for draw border you must click on this box
// and drag to targwt window
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (mousePressed)
{
Point point;
WinApi.GetCursorPos(out point);
IntPtr hWnd = Window.getHandler(point);
if (hWnd.Equals(selectedWindowHandler))
{
drawSelectionRectangle(selectedWindowHandler);
pictureBox_aimImage.Invalidate();
} else
{
selectedWindowHandler = hWnd;
invalidateAllWindows();
}
}
}
// when i once called InvalidateRect(...) not all border was cleared
// i don't know why
private static void invalidateAllWindows()
{
WinApi.InvalidateRect(IntPtr.Zero, IntPtr.Zero, true);
WinApi.InvalidateRect(IntPtr.Zero, IntPtr.Zero, true);
WinApi.InvalidateRect(IntPtr.Zero, IntPtr.Zero, true);
}
...
public static Rectangle getRectangle(IntPtr handler)
{
Rectangle rectangle;
WinApi.GetWindowRect(handler, out rectangle);
rectangle = new Rectangle(
rectangle.Location,
new Size(
rectangle.Size.Width - rectangle.Location.X,
rectangle.Size.Height - rectangle.Location.Y
)
);
return rectangle;
}
public static void drawSelectionRectangle(IntPtr handler)
{
//getting target window rectangle for GDI+
Rectangle rect = getRectangle(handler);
//getting context of desktop
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
using (g)
{
//drawing borders
g.DrawRectangle(new Pen(Color.Red, 3), rect);
}
}
}
不去上班。窗户不是你的。所有者将在你可能成功绘制的任何东西上绘制。您需要了解Win32绘画是如何工作的。不管问题是什么,这都不是解决办法。