I can't understand how to use SendMessage or PostMessage calls

前端 未结 5 820
南方客
南方客 2020-12-29 14:39

I need to simulate a keypress in a third party application. Let\'s say I have a C# application that needs to send an \"8\" to the Calculator application. I can\'t use the Se

5条回答
  •  佛祖请我去吃肉
    2020-12-29 14:51

    The solution here helped me but I had to edit it, also it's now shorter:

    Also here a usefull list of Virtual Key Codes

            [DllImport("user32.dll")]
            public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
            [DllImport("user32.dll")]
            static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
    
            private void button1_Click(object sender, EventArgs e)
            {
                const int WM_SYSKEYDOWN = 0x0104;
    
                IntPtr WindowToFind = FindWindow(null, "Calculator");
    
                PostMessage(WindowToFind, WM_SYSKEYDOWN, ((int)Keys.NumPad7), 0);
            }
    

提交回复
热议问题