请教大哥们帮忙
我的目的是点击button1然后就create一个notepad,然后鼠标移动notepad中间点右健, 目的是希望弹出右健菜单,
namespace WindowsApplication
{
  private void button1_Click(object sender, EventArgs e)
  {
    WindowsAp.Class1.S();
    //WindowsAp.Class1.Fun();
  }
}namespace WindowsAp
{
    public class Class1
    {
        public static void S()
        {
            Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(Fun));
            t.Start();
        }        public static void Fun()
        {
            Process.Start(@"C:\Users\v-blyan\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories\notepad");
            System.Threading.Thread.Sleep(2000);
            h = WIN32APIFun.GetForegroundWindow(); //WIN32APIFun 是我定义的API函数类
            WIN32APIFun.SetActiveWindow(h);
            WIN32APIFun.SetCursorPos(500, 400);
            WIN32APIFun.SendMessage(h, (uint)MOUSEEVENT.WM_RBUTTONDOWN, 0, 0);
            WIN32APIFun.SendMessage(h, (uint)MOUSEEVENT.WM_RBUTTONUP, 0, 0);
            //SendKeys.SendWait("DSDD");
        }
    }
}点了button1后,notepad创建了,鼠标也移动过去了,可就是没有点击右健的操作, 望高手指点阿,谢谢拉!

解决方案 »

  1.   

    能把你写的WIN32APIFun函数贴出来吗,这样好看些
      

  2.   

    把你这两句:
    WIN32APIFun.SendMessage(h, (uint)MOUSEEVENT.WM_RBUTTONDOWN, 0, 0); 
    WIN32APIFun.SendMessage(h, (uint)MOUSEEVENT.WM_RBUTTONUP, 0, 0); 换成:
    WIN32APIFun.SendMessage(h, (uint)MOUSEEVENT.WM_RBUTTONDBLCLK, 0, 0);在你的函数里面定义下WM_RBUTTONDBLCLK
      

  3.   


    namespace WindowsAp
    {
        public struct PWINDOWINFO
        {
            public uint cbSize;
            public Rectangle rcWindow;
            public Rectangle rcClient;
            public uint dwStyle;
            public uint dwExStyle;
            public uint dwWindowStatus;
            public uint cxWindowBorders;
            public uint cyWindowBorders;
            public ushort atomWindowType;
            public ushort wCreatorVersion;
        }    public enum WinState
        {
            SW_HIDE            = 0,
            SW_SHOWNORMAL      = 1,
            SW_NORMAL          = 1,
            SW_SHOWMINIMIZED   = 2,
            SW_SHOWMAXIMIZED   = 3,
            SW_MAXIMIZE        = 3,
            SW_SHOWNOACTIVATE  = 4,
            SW_SHOW            = 5,
            SW_MINIMIZE        = 6,
            SW_SHOWMINNOACTIVE = 7,
            SW_SHOWNA          = 8,
            SW_RESTORE         = 9,
            SW_SHOWDEFAULT     = 10,
            SW_FORCEMINIMIZE   = 11,
            SW_MAX             = 11
        }    public enum MOUSEEVENT
        {
            WM_MOUSEFIRST    = 0x0200,
            WM_MOUSEMOVE     = 0x0200,
            WM_LBUTTONDOWN   = 0x0201,
            WM_LBUTTONUP     = 0x0202,
            WM_LBUTTONDBLCLK = 0x0203,
            WM_RBUTTONDOWN   = 0x0204,
            WM_RBUTTONUP     = 0x0205,
            WM_RBUTTONDBLCLK = 0x0206,
            WM_MBUTTONDOWN   = 0x0207,
            WM_MBUTTONUP     = 0x0208,
            WM_MBUTTONDBLCLK = 0x0209,
            WM_MOUSEWHEEL    = 0x020a
        }    public class WIN32APIFun
        {
            [DllImport("user32.dll")]
            public static extern IntPtr SetFocus(IntPtr hWnd);
            [DllImport("user32.dll")]
            public static extern IntPtr SetCapture(IntPtr hWnd);
            [DllImport("user32.dll")]
            public static extern bool ReleaseCapture();
            [DllImport("user32.dll")]
            public static extern IntPtr SetActiveWindow(IntPtr hWnd);
            [DllImport("user32.dll")]
            public static extern IntPtr ClientToScreen(IntPtr hWnd, ref Point p);
            [DllImport("user32.dll")]
            public static extern IntPtr ScreenToClient(IntPtr hWnd, ref Point p);
            [DllImport("user32.dll")]
            public static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowRect(IntPtr hWnd, Rectangle rt);
            [DllImport("user32.dll")]
            public static extern bool GetWindowInfo(IntPtr hWnd, ref PWINDOWINFO pwi);
            [DllImport("user32.dll")]
            public static extern bool GetClassName(IntPtr hWnd, StringBuilder sb, int size);
            [DllImport("user32.dll")]
            public static extern int GetWindowText(IntPtr hWnd, StringBuilder sb, int size);
            [DllImport("user32.dll")]
            public static extern IntPtr GetForegroundWindow();        [DllImport("user32.dll")]
            public static extern int SendMessage(IntPtr hWnd, uint wMsg, long wParam, long lParam);
            [DllImport("user32.dll")]
            public static extern int PostMessage(IntPtr hWnd, uint wMsg, long wParam, long lParam);
            [DllImport("user32.dll")]
            public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
            public delegate bool WindowEnumDelegate(IntPtr pHWnd, IntPtr p);
            [DllImport("user32.dll")]
            public static extern bool EnumChildWindows(IntPtr parentHWnd, WindowEnumDelegate windowEnumCallback, IntPtr i);
            [DllImport("user32.dll")]
            public static extern IntPtr GetParent(IntPtr hWnd);        [DllImport("user32.dll")]
            public static extern bool IsWindowVisible(IntPtr hWnd);        [DllImport("Oleacc.dll")]
            public static extern int AccessibleObjectFromWindow(IntPtr hWnd, uint dwObjectID, ref Guid refGuidID, ref IAccessible ppvObject);
            [DllImport("Oleacc.dll")]
            public static extern int GetRoleText(int dwRole, StringBuilder roleText, int size);
            [DllImport("Oleacc.dll")]
            public static extern int AccessibleChildren(Accessibility.IAccessible paccContainer, int iChildStart, int cChildren, [Out] object[] rgvarChildren, out int pcObtained);        public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
            [DllImport("user32.dll")]
            public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hIstance, int threadId);
            [DllImport("user32.dll")]
            public static extern bool UnhookWindowsHookEx(int idHook);
            [DllImport("user32.dll")]
            public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
            [DllImport("kernel32.dll")]
            public static extern int GetCurrentThreadId();        //about all Cursor
            [DllImport("user32.dll")]
            public static extern bool SetCursorPos(int x, int y);
            [DllImport("user32.dll")]
            public static extern int ClipCursor(ref Rectangle lpRect);
            [DllImport("user32.dll")]
            public static extern int GetCursorPos(ref Point lpPoint);
            [DllImport("user32.dll")]
            public static extern bool ShowCursor(bool bShow);
            [DllImport("user32.dll")]
            public static extern bool EnableWindow(IntPtr hWnd, bool bEnable); // indicate the window whether can be input infomation by mouse
        }
    }
      

  4.   

    WIN32APIFun.SendMessage(h, (uint)MOUSEEVENT.WM_RBUTTONDOWN, 0, 0);  
    WIN32APIFun.SendMessage(h, (uint)MOUSEEVENT.WM_RBUTTONUP, 0, 0);这两句如果应用在Form1窗口上是可以达到点击右健弹出菜单的效果的,但是现在的感觉是好像到了另一个窗口上他就没 反映了,我个人的感觉是 这个点击的作用好像是还停留在原来的Form1的窗口上,但是我给的sendmessage的句柄确实是notepad的阿?而且我也确认了呀,谢谢楼上的大哥,请再指教阿!
      

  5.   

    右键消息应该发送给Notepad中的Edit窗体
    参考如下代码:
    [DllImport("User32.DLL")]
    public static extern int SendMessage(IntPtr hWnd,
        uint Msg, int wParam, int lParam);
    [DllImport("User32.DLL")]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent,
        IntPtr hwndChildAfter, string lpszClass, string lpszWindow);public const uint WM_RBUTTONDOWN = 0x0204;
    public const uint WM_RBUTTONUP = 0x0205;
    private void button1_Click(object sender, EventArgs e)
    {
        Process vProcess = Process.Start("notepad.exe");
        while (vProcess.MainWindowHandle == IntPtr.Zero) vProcess.Refresh();
        IntPtr vHandle = FindWindowEx(vProcess.MainWindowHandle,
            IntPtr.Zero, "Edit", null);
        SendMessage(vHandle, WM_RBUTTONDOWN, 0, 20/*x*/ | 10/*y*/ << 16);
        SendMessage(vHandle, WM_RBUTTONUP, 0, 20/*x*/ | 10/*y*/ << 16);
    }