国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

在C#程序中對MessageBox進行定位的方法

2020-01-24 01:37:54
字體:
來源:轉載
供稿:網友

 在 C# 中沒有提供方法用來對 MessageBox 進行定位,但是通過 C++ 你可以查找窗口并移動它們,本文講述如何在 C# 中對 MessageBox 進行定位。

首先需在代碼上引入所需名字空間:
 

using System.Runtime.InteropServices;using System.Threading;

在你的 Form 類里添加如下 DllImport 屬性:
 

[DllImport("user32.dll")]static extern IntPtr FindWindow(IntPtr classname, string title); // extern method: FindWindow [DllImport("user32.dll")]static extern void MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool rePaint); // extern method: MoveWindow [DllImport("user32.dll")]static extern bool GetWindowRect(IntPtr hwnd, out Rectangle rect); // extern method: GetWindowRect

接下來就可以查找窗口并移動它:
 

void FindAndMoveMsgBox(int x, int y, bool repaint, string title){  Thread thr = new Thread(() => // create a new thread  {    IntPtr msgBox = IntPtr.Zero;    // while there's no MessageBox, FindWindow returns IntPtr.Zero    while ((msgBox = FindWindow(IntPtr.Zero, title)) == IntPtr.Zero) ;    // after the while loop, msgBox is the handle of your MessageBox    Rectangle r = new Rectangle();    GetWindowRect(msgBox, out r); // Gets the rectangle of the message box    MoveWindow(msgBox /* handle of the message box */, x , y,      r.Width - r.X /* width of originally message box */,      r.Height - r.Y /* height of originally message box */,      repaint /* if true, the message box repaints */);  });  thr.Start(); /: starts the thread}

你要在 MessageBox.Show 之前調用這個方法,并確保 caption 參數不能為空,因為 title 參數必須等于 caption 參數。

使用方法:

 FindAndMoveMsgBox(0,0,true,"Title");MessageBox.Show("Message","Title");

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 晋中市| 青河县| 海口市| 湟中县| 凯里市| 西充县| 小金县| 桃园县| 墨玉县| 普陀区| 青龙| 珠海市| 灵武市| 新昌县| 屯留县| 石狮市| 佛冈县| 清远市| 岱山县| 金山区| 喀喇| 普安县| 永州市| 来凤县| 遂平县| 颍上县| 迁安市| 黄龙县| 宜宾县| 德令哈市| 平武县| 木兰县| 获嘉县| 板桥市| 清流县| 七台河市| 辽源市| 安平县| 巩留县| 遂宁市| 屏南县|