C# SendMessage 第一次接触

        第一次接触SendMessage,先记下一个简单的使用案例。

        需要使用的Spy++,进行窗口ClassName的查找。 

        文章内容代码逻辑为自己编写,SendMessage使用方式为网络内搜索所得。

 

       [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
 
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
 

        const int BM_CLICK = 0xF5;
        const int WM_KEYDOWN = 0x0100;
        const int WM_KEYUP = 0x0101; 

 

 

    

            int i = 0;
            while (true)
            {

                //   #32770为需要查找窗口的ClassName,可以在窗口打开时使用Spy++进行查找
                IntPtr WindownHand = FindWindow("#32770", "页面设置");
                if (WindownHand != IntPtr.Zero)
                {

                    //查找窗口内的子项目  例如窗口名称为 “横向(&A)”   的对象 ,  注: 此处只是一个单选按钮,所以它的名称就是窗口标题 
                    IntPtr Wk = FindWindowEx(WindownHand, IntPtr.Zero, null, "横向(&A)");
                    SendMessage(Wk, BM_CLICK, 0, 0);

                    IntPtr Wk1 = FindWindowEx(WindownHand, IntPtr.Zero, null, "确定");
                    SendMessage(Wk1, BM_CLICK, 0, 0);

                    //IntPtr Wk1 = FindWindowEx(WindownHand, IntPtr.Zero, null, "确定");
                    //SendMessage(Wk1, BM_CLICK, 0, 0);
                    break;
                }
                else
                {
                    if (i > 20)
                        break;
                    Thread.Sleep(500);
                    i++;
                }
            }

 

 

   

你可能感兴趣的:(C# SendMessage 第一次接触)