给窗体中控件绘图的几种方法
2017-10-09 17:00
162 查看
方法一:WINDOWS API画法
//获取要绘制的控件句柄
Image img = GetWindow(this.tabPage12.Handle);
//在母容器上创建图形对象
Graphics gOut = mOut.pnlOut.CreateGraphics();
//在指定位置按指定大小绘出image
gOut.DrawImage((Image)img, form1.pnlImage.DisplayRectangle, this.tabPage12.DisplayRectangle, GraphicsUnit.Pixel);
//释放图形对象所有资源
gOut.Dispose();
img.Dispose();
GC.Collect();
form1.pnlImage.BringToFront();
#region 截图函数
[DllImport("gdi32.dll")]
public static extern IntPtr CreateDC(
string lpszDriver, //
driver name驱动名
string lpszDevice, //
device name设备名
string lpszOutput, //
not used; should be NULL
IntPtr lpInitData //
optional printer data
);
[DllImport("gdi32.dll")]
public static extern int BitBlt(
IntPtr hdcDest, //
handle to destination DC目标设备的句柄
int nXDest, //
x-coord of destination upper-left corner目标对象的左上角的X坐标
int nYDest, //
y-coord of destination upper-left corner目标对象的左上角的Y坐标
int nWidth, //
width of destination rectangle目标对象的矩形宽度
int nHeight, //
height of destination rectangle目标对象的矩形长度
IntPtr hdcSrc, //
handle to source DC源设备的句柄
int nXSrc, //
x-coordinate of source upper-left corner源对象的左上角的X坐标
int nYSrc, //
y-coordinate of source upper-left corner源对象的左上角的Y坐标
UInt32 dwRop //
raster operation code光栅的操作值
);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(
IntPtr hdc //
handle to DC
);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(
IntPtr hdc, //
handle to DC
int nWidth, //
width of bitmap, in pixels
int nHeight //
height of bitmap, in pixels
);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(
IntPtr hdc, //
handle to DC
IntPtr hgdiobj //
handle to object
);
[DllImport("gdi32.dll")]
public static extern int DeleteDC(
IntPtr hdc //
handle to DC
);
[DllImport("user32.dll")]
public static extern bool PrintWindow(
IntPtr hwnd, //
Window to copy,Handle to the window that will be copied.
IntPtr hdcBlt, //
HDC to print into,Handle to the device context.
UInt32 nFlags //
Optional flags,Specifies the drawing options. It can be one of the following values.
);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(
IntPtr hwnd
);
public Bitmap GetWindow(IntPtr hWnd)
{
IntPtr hscrdc = GetWindowDC(hWnd);
Control control = Control.FromHandle(hWnd);
IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
IntPtr hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(hWnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return bmp;
}
#endregion
方法二、给background赋值
Bitmap bmp = new Bitmap(this.tabPage12.Width, this.tabPage12.Height);
//将控件区域显呈到指定位图
this.tabPage12.DrawToBitmap(bmp, this.tabPage12.DisplayRectangle);
form1.pnlImage.BackgroundImage = (Image)bmp;
/* 分屏显示座席 */
form1.pnlImage.BackColor = Color.White;
form1.pnlImage.BringToFront();
方法三、将某个屏幕位置拷贝到image
Image img = new Bitmap(tabPage13.Width, tabPage13.Height);
Graphics g = Graphics.FromImage(img);
g.CopyFromScreen(Obj.PointToScreen(Point.Empty), Point.Empty, tabPage13.Size);
IntPtr dc1 = g.GetHdc();
g.ReleaseHdc(dc1);
form1.pnlImage.BackgroundImage = img;
form1.pnlImage.BringToFront();
//获取要绘制的控件句柄
Image img = GetWindow(this.tabPage12.Handle);
//在母容器上创建图形对象
Graphics gOut = mOut.pnlOut.CreateGraphics();
//在指定位置按指定大小绘出image
gOut.DrawImage((Image)img, form1.pnlImage.DisplayRectangle, this.tabPage12.DisplayRectangle, GraphicsUnit.Pixel);
//释放图形对象所有资源
gOut.Dispose();
img.Dispose();
GC.Collect();
form1.pnlImage.BringToFront();
#region 截图函数
[DllImport("gdi32.dll")]
public static extern IntPtr CreateDC(
string lpszDriver, //
driver name驱动名
string lpszDevice, //
device name设备名
string lpszOutput, //
not used; should be NULL
IntPtr lpInitData //
optional printer data
);
[DllImport("gdi32.dll")]
public static extern int BitBlt(
IntPtr hdcDest, //
handle to destination DC目标设备的句柄
int nXDest, //
x-coord of destination upper-left corner目标对象的左上角的X坐标
int nYDest, //
y-coord of destination upper-left corner目标对象的左上角的Y坐标
int nWidth, //
width of destination rectangle目标对象的矩形宽度
int nHeight, //
height of destination rectangle目标对象的矩形长度
IntPtr hdcSrc, //
handle to source DC源设备的句柄
int nXSrc, //
x-coordinate of source upper-left corner源对象的左上角的X坐标
int nYSrc, //
y-coordinate of source upper-left corner源对象的左上角的Y坐标
UInt32 dwRop //
raster operation code光栅的操作值
);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(
IntPtr hdc //
handle to DC
);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(
IntPtr hdc, //
handle to DC
int nWidth, //
width of bitmap, in pixels
int nHeight //
height of bitmap, in pixels
);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(
IntPtr hdc, //
handle to DC
IntPtr hgdiobj //
handle to object
);
[DllImport("gdi32.dll")]
public static extern int DeleteDC(
IntPtr hdc //
handle to DC
);
[DllImport("user32.dll")]
public static extern bool PrintWindow(
IntPtr hwnd, //
Window to copy,Handle to the window that will be copied.
IntPtr hdcBlt, //
HDC to print into,Handle to the device context.
UInt32 nFlags //
Optional flags,Specifies the drawing options. It can be one of the following values.
);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(
IntPtr hwnd
);
public Bitmap GetWindow(IntPtr hWnd)
{
IntPtr hscrdc = GetWindowDC(hWnd);
Control control = Control.FromHandle(hWnd);
IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
IntPtr hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(hWnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return bmp;
}
#endregion
方法二、给background赋值
Bitmap bmp = new Bitmap(this.tabPage12.Width, this.tabPage12.Height);
//将控件区域显呈到指定位图
this.tabPage12.DrawToBitmap(bmp, this.tabPage12.DisplayRectangle);
form1.pnlImage.BackgroundImage = (Image)bmp;
/* 分屏显示座席 */
form1.pnlImage.BackColor = Color.White;
form1.pnlImage.BringToFront();
方法三、将某个屏幕位置拷贝到image
Image img = new Bitmap(tabPage13.Width, tabPage13.Height);
Graphics g = Graphics.FromImage(img);
g.CopyFromScreen(Obj.PointToScreen(Point.Empty), Point.Empty, tabPage13.Size);
IntPtr dc1 = g.GetHdc();
g.ReleaseHdc(dc1);
form1.pnlImage.BackgroundImage = img;
form1.pnlImage.BringToFront();
相关文章推荐
- 给窗体中控件绘图的几种方法
- MFC中从窗体类外修改控件属性的几种方法
- 使用反射实现根据名称动态创建窗体的几种方法。
- WPF中不规则窗体与WindowsFormsHost控件兼容问题的解决方法
- "控件必须放在具有 runat=server 的窗体标记内"错误的解决方法
- 设置编辑控件输入长度的几种方法
- vs2010下使用绘图控件MsChart的方法
- 【C#】MDI窗体中,将子窗体置于父窗体控件之上的方法
- 总结窗体间传值的几种方法
- ActiveX 控件注册的几种方法
- "控件必须放在具有 runat=server 的窗体标记内"错误的解决方法
- 窗体透明、窗体上控件和DC绘图不透明
- C#中窗体间传递数据的几种方法
- Qt中几种设置控件的图片背景的方法
- asp.net 导出excel时报类型“DataGridLinkButton”的控件“dgExcel__ctl1__ctl0”必须放在具有 runat=server 的窗体标记内 解决方法
- "控件必须放在具有 runat=server 的窗体标记内"错误的解决方法
- C# 窗体中一个类中调用另一个窗体的控件或方法
- 初识Windows窗体(包括各种控件,属性,方法)
- C#中子线程操作主线程中窗体上控件的方法
- 用委托的方法传递消息到窗体控件中: Visual Studio 2010 vb.net