获取MDI窗体中控件坐标的方法/屏幕局部截图原位写入

1、程序介绍:
在有MDI窗体的工具条和菜单栏,而且窗体在任何大小时,要想准确得到窗体中控件的坐标,能够使用下列代码:windows

这段代码的大致含义是在原来的窗体上的pbImg(pictureBox)位置进行屏幕截图后写回pbImg,而屏幕任何内容的位置不能变化,让人看不出是截图后从新写入的。
Bitmap CatchBmp = new Bitmap(pbImg.Width, pbImg.Height);
Graphics g = Graphics.FromImage(CatchBmp);
var screenPoint = PointToScreen(pbImg.Location);
//
//x1 = panel1.Width+panel1.Left;
//y1 = menuStrip1.Height+menuStrip1.Location.Y ;
g.CopyFromScreen(new Point(screenPoint.X + panel1.Width + panel1.Left, screenPoint.Y + menuStrip1.Height + menuStrip1.Location.Y), new Point(0, 0), new Size(pbImg.Width, pbImg.Height));ide

2、程序中用到的典型方法介绍工具

一、Control.PointToScreen 方法orm

将指定工做区点的位置计算成屏幕坐标。
命名空间: System.Windows.Forms
程序集: System.Windows.Forms(在 system.windows.forms.dll 中)ip

C#语法:
public Point PointToScreen ( Point p)
参数
p:要转换的工做区坐标 Point。 it

返回值:一个 Point,它表示转换后的 Point、p(以屏幕坐标表示)。 io

二、Graphics.CopyFromScreen 方法 (Int32, Int32, Int32, Int32, Size)form

执行颜色数据(对应于由像素组成的矩形)从屏幕到 Graphics 的绘图图面的位块传输。class

命名空间: System.Drawing
程序集: System.Drawing(位于 System.Drawing.dll)命名空间

C#语法:
public void CopyFromScreen( int sourceX, int sourceY, int destinationX, int destinationY, Size blockRegionSize)

参数
sourceX
Type: System.Int32
位于源矩形左上角的点的 x 坐标。

sourceY
Type: System.Int32:位于源矩形左上角的点的 y 坐标。

destinationX
Type: System.Int32:位于目标矩形左上角的点的 x 坐标。

destinationY
Type: System.Int32:位于目标矩形左上角的点的 y 坐标。

blockRegionSize
Type: System.Drawing.Size:要传输的区域大小。

异常:Win32Exception:操做失败。

相关文章
相关标签/搜索