c# API 句柄初探

每一个控件在窗体上都被认为是一个子窗体 获取到窗体句柄之后 IntPtr hwnd = FindWindow(null, "你需要获取的窗体的标题,例如:Form1"); 
IntPtr ButtonHwnd= FindWindowEx(hwnd, IntPtr.Zero, "控件的名字", null); 这样就能得到控件的句柄
SendMessage(ButtonHwnd, WM_SETTEXT, IntPtr.Zero, name); 这样就能更改控件的内容了
如果是button控件 可以这样 SendMessage(ButtonHwnd, WM_LBUTTONDOWN, IntPtr.Zero, null);
SendMessage(ButtonHwnd, WM_LBUTTONUP, IntPtr.Zero, null); 这样就是点击一次按钮 根据不同的控件你可以发送不同的控件消息来控制
读取内容的话用 GetWindowText(ButtonHwnd, szClassName, null);

C#取窗体句柄

IntPtr hWnd = System.Diagnostics.Process.GetProcessesByName("AppName")[0].MainWindowHandle;
 
[DllImport("user32.dll")]
        private static extern int GetDC(int hwnd);

        private void button1_Click(object sender, EventArgs e)
        {
          System.IntPtr p = (IntPtr)GetDC(0);// '取得屏幕
          Graphics g= Graphics.FromHdc(p);
          g.DrawRectangle(new Pen(Color.Black),new Rectangle (100,100,100,100));

        }
可能用到的API有:
[DllImport("user32.dll", EntryPoint = "GetDCEx", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr IntGetDCEx(HandleRef hWnd, HandleRef hrgnClip, int flags);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern int IntReleaseDC(HandleRef hWnd, HandleRef hDC);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern bool RedrawWindow(HandleRef hwnd, ref RECT rcUpdate, HandleRef hrgnUpdate, int flags);
给你个在桌上画圆的代码吧:
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", EntryPoint = "GetDCEx", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags);

private void button1_Click(object sender, EventArgs e)
{
IntPtr desk = GetDesktopWindow();
IntPtr deskDC = GetDCEx(desk, IntPtr.Zero, 0x403);
Graphics g = Graphics.FromHdc(deskDC);
g.FillEllipse(SystemBrushes.ControlText, 0, 0, 100, 100);
}
我利用了USER32.DLL和GDI32.DLL,为什么这样看不到矩形??   
  能否给出提示,谢谢   
  IntPtr   hDC   =   PlatformInvokeUSER32.GetDC(PlatformInvokeUSER32.GetDesktopWindow());   
                          Graphics   m_Graphics=Graphics.FromHdc(hDC);   
                          Pen   redPen=new   Pen(Color.Red,   10);   
                          Rectangle   rWorkArea   =   Screen.GetWorkingArea(Screen.PrimaryScreen.WorkingArea);   
                          m_Graphics.DrawRectangle(redPen,rWorkArea);   
                          PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.GetDesktopWindow(),   hDC);   
[DllImport("User32.dll")]     
      
            public   extern   static   System.IntPtr   GetDC(System.IntPtr   hWnd);     
      
    
      
            private   void   button1_Click(object   sender,   System.EventArgs   e)     
      
            {     
      
                      System.IntPtr   DesktopHandle   =   GetDC(System.IntPtr.Zero);     
      
                      Graphics   g   =   System.Drawing.Graphics.FromHdc(DesktopHandle);     
      
                      g.FillRectangle(new   SolidBrush(Color.Red),0,0,100,100);     
      
            }   
C#在屏幕上画图
原文地址:https://www.cnblogs.com/bkycjj/p/3440623.html