2017-05-03 79 views
-1

我需要编写一个C#程序,它将识别光标下的一个窗口并在其上绘制边框。C#。 WINAPI。在窗口上绘制

我可以很容易地得到一个窗口句柄:

... 
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()方法,而不是我想要绘制的表单)。这似乎并不奏效。

请帮助我。我不知道该怎么办。

+0

不工作。该窗口不属于你。所有者将画任何你可能成功绘画的东西。您需要了解Win32绘画的工作原理。无论问题是什么,这都不是解决方案。 –

回答

0

这不是我的问题的完整解决方案,但它是有效的。我想要在目标窗口上绘制(如果目标窗口位于其他窗口下,边界也必须位于其他窗口下)......但是解决方案(当绘制高于所有窗口的边界时)比没有任何东西好。

我希望我的问题和解决方案能帮助别人。谢谢大家^ _ ^。

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); 
     } 
    } 

}