给窗体中控件绘图的几种方法

本文介绍三种利用 Windows API 进行绘图及截取屏幕图像的方法:使用 API 函数绘制控件到位图;通过设置背景图像来复制控件;以及将屏幕上的特定位置复制到 Image 控件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一:WINDOWS API画法


  1. //获取要绘制的控件句柄
  2. Image img = GetWindow(this.tabPage12.Handle);
  3. //在母容器上创建图形对象 
  4. Graphics gOut = mOut.pnlOut.CreateGraphics();
  5. //在指定位置按指定大小绘出image 
  6. gOut.DrawImage((Image)img, form1.pnlImage.DisplayRectangle, this.tabPage12.DisplayRectangle, GraphicsUnit.Pixel);
  7. //释放图形对象所有资源 
  8. gOut.Dispose(); 
  9. img.Dispose(); 
  10. GC.Collect(); 
  11. form1.pnlImage.BringToFront();
  12.  
  13. #region 截图函数
  14.     [DllImport("gdi32.dll")] 
  15.     public static extern IntPtr CreateDC( 
  16.      string lpszDriver, // driver name驱动名 
  17.      string lpszDevice, // device name设备名 
  18.      string lpszOutput, // not used; should be NULL 
  19.      IntPtr lpInitData // optional printer data 
  20.      ); 
  21.     [DllImport("gdi32.dll")] 
  22.     public static extern int BitBlt( 
  23.      IntPtr hdcDest, // handle to destination DC目标设备的句柄 
  24.      int nXDest, // x-coord of destination upper-left corner目标对象的左上角的X坐标 
  25.      int nYDest, // y-coord of destination upper-left corner目标对象的左上角的Y坐标 
  26.      int nWidth, // width of destination rectangle目标对象的矩形宽度 
  27.      int nHeight, // height of destination rectangle目标对象的矩形长度 
  28.      IntPtr hdcSrc, // handle to source DC源设备的句柄 
  29.      int nXSrc, // x-coordinate of source upper-left corner源对象的左上角的X坐标 
  30.      int nYSrc, // y-coordinate of source upper-left corner源对象的左上角的Y坐标 
  31.      UInt32 dwRop // raster operation code光栅的操作值 
  32.      );
  33.     [DllImport("gdi32.dll")] 
  34.     public static extern IntPtr CreateCompatibleDC( 
  35.      IntPtr hdc // handle to DC 
  36.      );
  37.     [DllImport("gdi32.dll")] 
  38.     public static extern IntPtr CreateCompatibleBitmap( 
  39.      IntPtr hdc, // handle to DC 
  40.      int nWidth, // width of bitmap, in pixels 
  41.      int nHeight // height of bitmap, in pixels 
  42.      );
  43.     [DllImport("gdi32.dll")] 
  44.     public static extern IntPtr SelectObject( 
  45.      IntPtr hdc, // handle to DC 
  46.      IntPtr hgdiobj // handle to object 
  47.      );
  48.     [DllImport("gdi32.dll")] 
  49.     public static extern int DeleteDC( 
  50.      IntPtr hdc // handle to DC 
  51.      );
  52.     [DllImport("user32.dll")] 
  53.     public static extern bool PrintWindow( 
  54.      IntPtr hwnd, // Window to copy,Handle to the window that will be copied. 
  55.      IntPtr hdcBlt, // HDC to print into,Handle to the device context. 
  56.      UInt32 nFlags // Optional flags,Specifies the drawing options. It can be one of the following values. 
  57.      );
  58.     [DllImport("user32.dll")] 
  59.     public static extern IntPtr GetWindowDC( 
  60.      IntPtr hwnd 
  61.      );
  62.     public Bitmap GetWindow(IntPtr hWnd) 
  63.     { 
  64.         IntPtr hscrdc = GetWindowDC(hWnd); 
  65.         Control control = Control.FromHandle(hWnd); 
  66.         IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height); 
  67.         IntPtr hmemdc = CreateCompatibleDC(hscrdc); 
  68.         SelectObject(hmemdc, hbitmap); 
  69.         PrintWindow(hWnd, hmemdc, 0); 
  70.         Bitmap bmp = Bitmap.FromHbitmap(hbitmap); 
  71.         DeleteDC(hscrdc);//删除用过的对象 
  72.         DeleteDC(hmemdc);//删除用过的对象 
  73.         return bmp; 
  74.     } 
  75.     #endregion


方法二、给background赋值


  1. Bitmap bmp = new Bitmap(this.tabPage12.Width, this.tabPage12.Height);
  2. //将控件区域显呈到指定位图 
  3. this.tabPage12.DrawToBitmap(bmp, this.tabPage12.DisplayRectangle); 
  4. form1.pnlImage.BackgroundImage = (Image)bmp;
  5. /* 分屏显示座席 */ 
  6. form1.pnlImage.BackColor = Color.White; 
  7. form1.pnlImage.BringToFront();


方法三、将某个屏幕位置拷贝到image


  1. Image img = new Bitmap(tabPage13.Width, tabPage13.Height); 
  2. Graphics g = Graphics.FromImage(img); 
  3. g.CopyFromScreen(Obj.PointToScreen(Point.Empty), Point.Empty, tabPage13.Size); 
  4. IntPtr dc1 = g.GetHdc(); 
  5. g.ReleaseHdc(dc1); 
  6. form1.pnlImage.BackgroundImage = img;
  7. form1.pnlImage.BringToFront();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值