C# 如何通过句柄操作 Excel2003 的文件菜单项中的另存为项
如题。GetSubMenu()无法获取到对应的句柄[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow1(
string lpClassName,
string lpWindowName
);
[DllImport("user32.dll", EntryPoint = "GetMenu")]
public static extern int GetMenu1(
int hwnd
);
[DllImport("user32.dll", EntryPoint = "GetSubMenu")]
public static extern int GetSubMenu1(
int hMenu,
int nPos
);
[DllImport("user32.dll", EntryPoint = "GetMenuItemID")]
public static extern int GetMenuItemID1(
int hMenu,
int nPos
);
[DllImport("user32.dll", EntryPoint = "PostMessage")]
public static extern int PostMessage1(
int hwnd,
int wMsg,
int wParam,
int lParam
);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
[DllImport("User32.dll ")]
public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText);
// 获取当前窗口句柄:GetForegroundWindow()
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetMenu(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetSubMenu")]
public static extern IntPtr GetSubMenu(IntPtr hMenu, int nPos);
[DllImport("user32.dll", EntryPoint = "GetMenuItemID")]
public static extern IntPtr GetMenuItemID(IntPtr hMenu, int nPos);
const int WM_COMMAND = 0x0111;
//调用菜单代码 但无法执行
Process.Start(@"C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE");
Thread.Sleep(2000);
int NotepadHwnd = FindWindow1(null, "Microsoft Excel - Book1");
//取得外部程式的菜单Handle
int gm = GetMenu1(NotepadHwnd);
//取得第一个子菜单Handle
gm = GetSubMenu1(gm, 0); //菜单的句柄 改变后面的0,就可以得到不同菜单的句柄
int id = GetMenuItemID1(gm, 3); //子菜单"另存新档"的ID
PostMessage1(NotepadHwnd, WM_COMMAND, id, 0);