标题:C# 如何通过句柄操作 Excel2003 的文件菜单项中的另存为项
取消只看楼主
xxp5310776
Rank: 1
来 自:湖北武汉
等 级:新手上路
帖 子:49
专家分:1
注 册:2006-11-19
结帖率:100%
已结贴  问题点数:20 回复次数:1 
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);
搜索更多相关主题的帖子: public 如何 
2014-05-29 16:18
xxp5310776
Rank: 1
来 自:湖北武汉
等 级:新手上路
帖 子:49
专家分:1
注 册:2006-11-19
得分:0 
不知道各位大虾有没有看。
自己顶吧!!!

2014-05-30 10:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-432239-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.118755 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved