不同的显示器它分辨率不相同,
如何在显示器屏幕上绘制一条直线呢?
注意:不是在某个窗体上绘制,而是在整个显示器屏幕上画线,是不是要先获得什么句柄?该如何实现
我是想,打开这个程序 就会在屏幕上显示一个十字坐标......
下面是在 一个窗体上绘制图片的代码(可我要的不是在这个窗口里绘制,要能绘制在整个屏幕上)        private void btnShow_Click(object sender, EventArgs e)
        {
            Graphics g;
            Pen p = new Pen(Color.Black);
            g = this.CreateGraphics();
            int width = ClientRectangle.Width;
            int height = ClientRectangle.Height;
            g.DrawLine(p, 0, 0, width, height);
            p.Dispose();
            g.Dispose();        }

解决方案 »

  1.   

    API重绘桌面
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetDesktopWindow();[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern bool RedrawWindow(IntPtr hwnd, RECT rcUpdate, IntPtr hrgnUpdate, int flags);RedrawWindow(GetDesktopWindow(), null, IntPtr.Zero, 0x85);
      

  2.   

    先谢谢大哥啦,
    我去查了一下,可以发现我不会用啊,小弟是新手,大哥给提示一下 该用在什么地方?是不是 直接添加在
     public partial class Form1 : Form
        {}
    里面? 还是可以直接可以写在按钮事件里?
      

  3.   

    RECT 这个一直提示说需要引用,查没有查到....
    你看下面这样写对不?
    using System.Runtime.InteropServices;namespace Test10._27_Llc
    {
        public partial class Form1 : Form
        {
            [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern IntPtr GetDesktopWindow();
            [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern bool RedrawWindow(IntPtr hwnd, RECT rcUpdate, IntPtr hrgnUpdate, int flags);        public Form1()
            {
                InitializeComponent();
            }        private void btnShow_Click(object sender, EventArgs e)
            {
               RedrawWindow(GetDesktopWindow(), null, IntPtr.Zero, 0x85);        }
        }
    }
      

  4.   

    你在你的代码基础基础上去 计算显示器的 对角线的像素吧,这个比你看不懂 API要好。
      

  5.   

    RECT 这个一直提示说需要引用,查没有查到....
    你看下面这样写对不?
    using System.Runtime.InteropServices;namespace Test10._27_Llc
    {
        public partial class Form1 : Form
        {
            [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern IntPtr GetDesktopWindow();
            [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern bool RedrawWindow(IntPtr hwnd, RECT rcUpdate, IntPtr hrgnUpdate, int flags);        public Form1()
            {
                InitializeComponent();
            }        private void btnShow_Click(object sender, EventArgs e)
            {
               RedrawWindow(GetDesktopWindow(), null, IntPtr.Zero, 0x85);        }
        }
    }
    错误 2 参数 2: 无法从“<null>”转换为“Win32.RECT” C:\Users\dcx\Documents\Visual Studio 2010\Projects\屏幕绘图\屏幕绘图\Form1.cs 28 46 屏幕绘图