溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

獲取MDI窗體中控件坐標(biāo)的方法/屏幕局部截圖原位寫(xiě)入

發(fā)布時(shí)間:2020-08-15 04:47:18 來(lái)源:網(wǎng)絡(luò) 閱讀:976 作者:fengyp 欄目:編程語(yǔ)言

一、程序介紹:
在有MDI窗體的工具條和菜單欄,并且窗體在任何大小時(shí),要想準(zhǔn)確獲得窗體中控件的坐標(biāo),可以使用下列代碼:

這段代碼的大體含義是在原來(lái)的窗體上的pbImg(pictureBox)位置進(jìn)行屏幕截圖后寫(xiě)回pbImg,而屏幕任何內(nèi)容的位置不能變化,讓人看不出是截圖后重新寫(xiě)入的。
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));

二、程序中用到的典型方法介紹

1、Control.PointToScreen 方法

將指定工作區(qū)點(diǎn)的位置計(jì)算成屏幕坐標(biāo)。
命名空間: System.Windows.Forms
程序集: System.Windows.Forms(在 system.windows.forms.dll 中)

C#語(yǔ)法:
public Point PointToScreen ( Point p)
參數(shù)
p:要轉(zhuǎn)換的工作區(qū)坐標(biāo) Point。

返回值:一個(gè) Point,它表示轉(zhuǎn)換后的 Point、p(以屏幕坐標(biāo)表示)。

2、Graphics.CopyFromScreen 方法 (Int32,?Int32,?Int32,?Int32,?Size)

執(zhí)行顏色數(shù)據(jù)(對(duì)應(yīng)于由像素組成的矩形)從屏幕到 Graphics 的繪圖圖面的位塊傳輸。

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

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

參數(shù)
sourceX
Type: System.Int32
位于源矩形左上角的點(diǎn)的 x 坐標(biāo)。

sourceY
Type: System.Int32:位于源矩形左上角的點(diǎn)的 y 坐標(biāo)。

destinationX
Type: System.Int32:位于目標(biāo)矩形左上角的點(diǎn)的 x 坐標(biāo)。

destinationY
Type: System.Int32:位于目標(biāo)矩形左上角的點(diǎn)的 y 坐標(biāo)。

blockRegionSize
Type: System.Drawing.Size:要傳輸?shù)膮^(qū)域大小。

異常:
Win32Exception:操作失敗。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI