【转】[C#.NET][VB.NET] 用 Win32 API 控制 外部應用程式選單
2017-04-17 15:16
2006 查看
1.這並不是用SendKey所組成的,所以不管應用程式是否在最上層,都能控制的到。 2.匯入 System.Runtime.InteropServices 及 System.Threading 命名空間 3.選單控制主要由下列三隻API組成
[DllImport("user32.dll", EntryPoint = "GetMenu")] public static extern int GetMenu( int hwnd ); [DllImport("user32.dll", EntryPoint = "GetSubMenu")] public static extern int GetSubMenu( int hMenu, int nPos ); [DllImport("user32.dll", EntryPoint="GetMenuItemID")] public static extern int GetMenuItemID( int hMenu, int nPos );
4.GetMenu所指的就是"主選單"工具列,如下圖所示。
而GetSubMenu指的就是"主選單"內的"子選單",由下圖可看到項目有五個"子選單",分別為:檔案(F)、編輯(E)、格式(O)、檢視(V)、說明(H)。
GetMenuItemID則是"子選單"中的"項目"
5.選單API的啟始值是0,GetSubMenu(gm, 0)表示第1個選單
GetSubMenu(gm, 1)表示第2個選單
6.GetMenuItemID用法同如上所示 ,int id = GetMenuItemID(gm, 3),表示第4個項目。
7.完整範例 C#
using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; using System.Threading; namespace CS_ControlMenu { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private const int WM_COMMAND = 0x111; [DllImport("user32.dll", EntryPoint = "FindWindow")] public static extern int FindWindow( string lpClassName, string lpWindowName ); [DllImport("user32.dll", EntryPoint = "GetMenu")] public static extern int GetMenu( int hwnd ); [DllImport("user32.dll", EntryPoint = "GetSubMenu")] public static extern int GetSubMenu( int hMenu, int nPos ); [DllImport("user32.dll", EntryPoint="GetMenuItemID")] public static extern int GetMenuItemID( int hMenu, int nPos ); [DllImport("user32.dll", EntryPoint = "PostMessage")] public static extern int PostMessage( int hwnd, int wMsg, int wParam, int lParam ); private void button1_Click(object sender, EventArgs e) { //呼叫外部程式 Process.Start(@"C:\windows\system32\notepad.exe"); //等執行程式一下下 Thread.Sleep(100); //取得外部程式的Handle int NotepadHwnd = FindWindow("notepad", null); //取得外部程式的選單Handle int gm = GetMenu(NotepadHwnd); //取得第一個子選單Handle gm = GetSubMenu(gm, 0); //選單的句柄 改變後面的0,就可以得到不同選單的句柄 //取得子選單的ID int id = GetMenuItemID(gm, 3); //子選單"另存新檔"的ID //執行子選單 PostMessage (NotepadHwnd, WM_COMMAND, id, 0); } } } 转载地址:http://www.dotblogs.com.tw/yc421206/archive/2009/07/06/9142.aspx
[DllImport("user32.dll", EntryPoint = "GetMenu")] public static extern int GetMenu( int hwnd ); [DllImport("user32.dll", EntryPoint = "GetSubMenu")] public static extern int GetSubMenu( int hMenu, int nPos ); [DllImport("user32.dll", EntryPoint="GetMenuItemID")] public static extern int GetMenuItemID( int hMenu, int nPos );
4.GetMenu所指的就是"主選單"工具列,如下圖所示。
而GetSubMenu指的就是"主選單"內的"子選單",由下圖可看到項目有五個"子選單",分別為:檔案(F)、編輯(E)、格式(O)、檢視(V)、說明(H)。
GetMenuItemID則是"子選單"中的"項目"
5.選單API的啟始值是0,GetSubMenu(gm, 0)表示第1個選單
GetSubMenu(gm, 1)表示第2個選單
6.GetMenuItemID用法同如上所示 ,int id = GetMenuItemID(gm, 3),表示第4個項目。
7.完整範例 C#
using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; using System.Threading; namespace CS_ControlMenu { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private const int WM_COMMAND = 0x111; [DllImport("user32.dll", EntryPoint = "FindWindow")] public static extern int FindWindow( string lpClassName, string lpWindowName ); [DllImport("user32.dll", EntryPoint = "GetMenu")] public static extern int GetMenu( int hwnd ); [DllImport("user32.dll", EntryPoint = "GetSubMenu")] public static extern int GetSubMenu( int hMenu, int nPos ); [DllImport("user32.dll", EntryPoint="GetMenuItemID")] public static extern int GetMenuItemID( int hMenu, int nPos ); [DllImport("user32.dll", EntryPoint = "PostMessage")] public static extern int PostMessage( int hwnd, int wMsg, int wParam, int lParam ); private void button1_Click(object sender, EventArgs e) { //呼叫外部程式 Process.Start(@"C:\windows\system32\notepad.exe"); //等執行程式一下下 Thread.Sleep(100); //取得外部程式的Handle int NotepadHwnd = FindWindow("notepad", null); //取得外部程式的選單Handle int gm = GetMenu(NotepadHwnd); //取得第一個子選單Handle gm = GetSubMenu(gm, 0); //選單的句柄 改變後面的0,就可以得到不同選單的句柄 //取得子選單的ID int id = GetMenuItemID(gm, 3); //子選單"另存新檔"的ID //執行子選單 PostMessage (NotepadHwnd, WM_COMMAND, id, 0); } } } 转载地址:http://www.dotblogs.com.tw/yc421206/archive/2009/07/06/9142.aspx
相关文章推荐
- [C#.NET][VB.NET] Winform 應用程式等級的例外捕捉 / Winform of Application Level wicth Exception Catch(转载)
- [C#.NET][VB.NET] 程式執行時拖曳控制項
- [C#.NET][VB.NET] Winform 應用程式等級的例外捕捉 / Winform Application of Level wicth Exception Catch
- 面向Google、YouTube、Facebook以及其他Web 2.0 API的C#和VB.NET类库
- API 在vb.net中應用注意事項
- [C#.NET][VB.NET] 自訂控制項的智能標籤-ControlDesigner / User Control of Smart Tag-ControlDesigner
- VB.NET及C#调用API
- [C#.NET][VB.NET] 自訂控制項的顯示視窗屬性 /User Control of Properties(一)
- c#学习必修:Microsoft Win32 to Microsoft .NET Framework API Map
- [C#.NET][VB.NET] 自訂控制項的顯示視窗屬性 /User Control of Properties(二)
- [C#.NET][VB.NET] 自訂控制項的集合屬性 /User Control of Collection Properties
- C#用 SendKyes 结合 Process 或 API FindWindow、SendMessage(PostMessage) 等控制外部程序
- VB.NET实现的控制外部应用程序窗口或控件行为的函数源程序
- C#用 SendKyes 结合 Process 或 API FindWindow、SendMessage(PostMessage) 等控制外部程序
- [转贴]dotNET C# Programmer’s Guide to the Win32 API (Win32 API 声明C#版)
- vb.net c#.net API 查询工具
- [C#.NET][VB.NET] 自訂控制項的智能標籤-ComponentDesigner / User Control of Smart Tag-ComponentDesigner
- C#用 SendKyes 结合 Process 或 API FindWindow、SendMessage(PostMessage) 等控制外部程序[转]
- [C#.NET][VB.NET] Process 類別 / 如何 列舉執行中的程式
- C#+Win32 API程序控制鼠标的操作