国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 济宁市| 神池县| 门源| 嫩江县| 湖口县| 舞钢市| 平谷区| 城口县| 通道| 湾仔区| 赤水市| 贺州市| 东乡族自治县| 怀安县| 霍邱县| 建阳市| 广丰县| 湟源县| 长宁区| 叶城县| 恩施市| 新营市| 宜昌市| 高邑县| 镇康县| 广灵县| 嘉善县| 确山县| 灵山县| 习水县| 天门市| 建阳市| 阜城县| 红桥区| 通山县| 宁明县| 宁安市| 乃东县| 十堰市| 阜宁县| 福安市|