C#Graphics 畫圖類操作


一、如何在桌面作圖

1.引用

using System.Drawing;

using System.Runtime.InteropServices;

2.調用系統API

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

 3.初始化Graphics 類

            IntPtr desk = GetDesktopWindow();
            IntPtr deskDC = GetDCEx(desk, IntPtr.Zero, 0x403);
            Graphics g = Graphics.FromHdc(deskDC);

4.操作作圖

            //Start point
            Point newPoint = new Point(0, 0);
            Pen p = new Pen(Color.Blue, 1);
            var Rectangle = new Rectangle(newPoint, new Size(500, 300));
            g.DrawRectangle(p, Rectangle);
            g.DrawString("測試", new Font("宋體", 50, FontStyle.Bold), Brushes.Red, new PointF(100, 100));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM