首页 > Windows开发 > 详细

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

时间:2014-11-07 02:27:10      阅读:223      评论:0      收藏:0      [点我收藏+]

方法一: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();


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

原文:http://blog.chinaunix.net/uid-25498312-id-4601214.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!