国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 龙泉市| 江川县| 扬中市| 珲春市| 惠水县| 章丘市| 新绛县| 宜城市| 酒泉市| 观塘区| 临海市| 芦溪县| 仁寿县| 青岛市| 崇信县| 阳江市| 福州市| 招远市| 铜山县| 宕昌县| 阿合奇县| 陵川县| 福泉市| 天峨县| 微博| 黑河市| 新郑市| 和平县| 古田县| 株洲县| 商都县| 牡丹江市| 兰西县| 漾濞| 米林县| 武宁县| 壶关县| 松溪县| 岫岩| 板桥市| 静海县|