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

205 篇文章 5 订阅

[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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值