您的位置:首页 > 其它

C​#​在​屏​幕​上​画​图

2014-07-11 13:55 337 查看
[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); 

}  

转自;http://wenku.baidu.com/link?url=K8JBBC9vfsDVYPi3sQ0H1ytB0vUpQZUmMw1AsBK5YVJ9Poeh5nkkzUWNkBcRd6dqgB9FQmbtO2CPFirTP8AjjGTA2lYw6oGPdghzXyWbrYG 

c#中怎么通过hdc获取当前窗口的句柄

using System.Runtime.InteropServices;
using System.Drawing;

public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
class Program
{
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern IntPtr ReleaseDC(IntPtr hc, IntPtr hDest);
[DllImport("user32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowRect(IntPtr hwnd, ref Rect lpRect);
static void Main(string[] args)
{
IntPtr platDC = GetForegroundWindow();
IntPtr windDC = GetWindowDC(platDC);
Rect rect = new Rect();
GetWindowRect(windDC, ref  rect);
Bitmap bmp = new Bitmap(rect.Right - rect.Left, rect.Bottom - rect.Top);
Graphics g = Graphics.FromImage(bmp);
IntPtr hdc = g.GetHdc();
}
}


转自:http://zhidao.baidu.com/link?url=AyjsfNQDhofQqTKVz0J8qXhP6gJocy28MKhv1EEPPrx-GyqWWN1_BhbMeQPsHySVJNBSARnXscBiCbd0A31Peq
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: