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

首頁(yè) > 編程 > C# > 正文

在C#程序中對(duì)MessageBox進(jìn)行定位的方法

2019-10-29 21:41:25
字體:
供稿:網(wǎng)友

這篇文章主要介紹了在C#程序中對(duì)MessageBox進(jìn)行定位的方法,針對(duì)圖形化界面進(jìn)行調(diào)試,需要的朋友可以參考下

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

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

 

 
  1. using System.Runtime.InteropServices; 
  2. using System.Threading; 

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

 

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

接下來就可以查找窗口并移動(dòng)它:

 

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

你要在 MessageBox.Show 之前調(diào)用這個(gè)方法,并確保 caption 參數(shù)不能為空,因?yàn)?title 參數(shù)必須等于 caption 參數(shù)。

使用方法:

 

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 噶尔县| 丰原市| 馆陶县| 卓资县| 且末县| 玉山县| 宁强县| 洪泽县| 三明市| 玉林市| 万宁市| 张家港市| 缙云县| 井陉县| 赤水市| 福鼎市| 道真| 桂平市| 德惠市| 张掖市| 邵阳市| 视频| 石首市| 岐山县| 扎囊县| 佛坪县| 黄龙县| 博湖县| 香格里拉县| 昌邑市| 磐安县| 肇东市| 塔城市| 鄱阳县| 贡山| 固阳县| 郧西县| 九江市| 龙门县| 祥云县| 沅陵县|