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