2017-05-03 126 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); 
     } 
    } 

}