c#进程操作-sendmessage全解

来源:互联网 发布:centos samba 配置 编辑:程序博客网 时间:2024/06/08 15:34
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Security.Cryptography;using System.Diagnostics;using System.Runtime.InteropServices;using System.Drawing;namespace util{    public class Sendmessage1    {        [DllImport("user32.dll", EntryPoint = "SendMessageA")]        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam);        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam);        public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, uint wParam, uint lParam);        public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, bool wParam, int lParam);        [DllImport("user32.dll")]        public static extern IntPtr GetForegroundWindow();        //键盘按下(目标窗体也许并不接受)        public static int WM_KEYDOWN = 0x0100;        public static int WM_KEYUP = 0x0101;        //移动鼠标时发生,同WM_MOUSEFIRST        public static int WM_MOUSEMOVE = 0x200;        //按下鼠标左键        public static int WM_LBUTTONDOWN = 0x201;        //释放鼠标左键        public static int WM_LBUTTONUP = 0x202;        //双击鼠标左键        public static int WM_LBUTTONDBLCLK = 0x203;        //按下鼠标右键        public static int WM_RBUTTONDOWN = 0x204;        //释放鼠标右键        public static int WM_RBUTTONUP = 0x205;        //双击鼠标右键        public static int WM_RBUTTONDBLCLK = 0x206;        //按下鼠标中键        public static int WM_MBUTTONDOWN = 0x207;        //双击鼠标中键        public static int WM_MBUTTONDBLCLK = 0x209;        //释放鼠标中键        public static int WM_MBUTTONUP = 0x208;        //发送字符        public static int WM_CHAR = 0x102;        //发送文本        public static int WM_SETTEXT = 0x0C;        //获取文本        public static int WM_GETTEXT = 0x0D;        //获取形状        public static int EM_GETRECT = 0xB2;        //获取文本行数        public static int EM_GETLINECOUNT = 0xBA;        //获得文本是否被修改过        public static int EM_GETMODIFY = 0xB8;        //获得光标在文本第几行        public static int EM_LINEFROMCHAR = 0xC9;        //获得多行文本编辑控件的滚动框的当前位置(像素值)        public static int EM_GETTHUMB = 0xBE;        //滚动到文本指定行列        public static int EM_LINESCROLL = 0xB6;        //获得文本光标处之前的字符数,包括回车换行         public static int EM_LINEINDEX = 0xBB;        //限制文本长度        public static int EM_LIMITTEXT = 0xC5;        //撤销操作,可撤销很多次        public static int EM_UNDO = 0xC7;        //设定只读        public static int EM_SETREADONLY = 0xCF;        //控件获得焦点        public static int WM_SETFOCUS = 0x07;        //控件失去焦点        public static int WM_KILLFOCUS = 0x08;        //滚动        public static int EM_SCROLL = 0xB5;        //选取指定范围字符串(要先获得焦点)        public static int EM_SETSEL = 0xB1;        //发送键盘点击事件        public static void keyclick(IntPtr handle, int charcode)        {            SendMessage(handle, WM_KEYDOWN, charcode, IntPtr.Zero);            SendMessage(handle, WM_KEYUP, charcode, IntPtr.Zero);        }        //模拟鼠标点击        public static void mouseclick(IntPtr handle)        {            SendMessage(handle, WM_LBUTTONDOWN, 0,0);            SendMessage(handle, WM_LBUTTONUP, 0,0);        }        //模拟鼠标点击指定位置        public static void mouseclick(IntPtr handle,int x,int y)        {            SendMessage(handle, WM_LBUTTONDOWN, 0, y*65536+x);            SendMessage(handle, WM_LBUTTONUP, 0, y * 65536 + x);        }        //发送字符        public static void sendchar(IntPtr handle, int charcode)  //发送A字符为65        {            SendMessage(handle, WM_CHAR, (IntPtr)charcode, IntPtr.Zero);        }        //发送文本        public static void settext(IntPtr handle, String s)         {            SendMessage(handle, WM_SETTEXT, IntPtr.Zero, s);        }        //获取文本        public static String gettext(IntPtr handle)        {            const int buffer_size = 1024;            StringBuilder buffer = new StringBuilder(buffer_size);            SendMessage(handle, WM_GETTEXT, buffer_size, buffer);            return buffer.ToString();        }          //获取形状        public static Size getrect(IntPtr handle)        {            Rectangle rect1 = new Rectangle();            SendMessage(handle, EM_GETRECT, (IntPtr)0, ref rect1);            Size size = new Size(rect1.Width, rect1.Height);            return size;        }             //获取文本行数        public static int getlinecount(IntPtr handle)        {            int linenum = SendMessage(handle, EM_GETLINECOUNT, 0, 0);            return linenum;        }        //获得文本是否被修改过        public static bool getmodify(IntPtr handle)        {            int 是否修改过 = SendMessage(handle, EM_GETMODIFY, 0, 0);            if (是否修改过 == 0)                return false;            else                return true;        }                  //获得光标在文本第几行        public static int linefromchar(IntPtr handle)        {            int 光标在第几行 = SendMessage(handle, EM_LINEFROMCHAR, -1, 0);            return 光标在第几行;   //下标从0开始        }        //获得多行文本编辑控件的滚动框的当前位置(像素值)        public static int getthumb(IntPtr handle)        {            int rc = SendMessage(handle, EM_GETTHUMB, 0, 0);            return rc;   //下标从0开始        }        //滚动到文本指定行列        public static void linescroll(IntPtr handle,int row,int column)        {            SendMessage(handle, EM_LINESCROLL, column, row);        }        //获得文本光标处之前的字符数,包括回车换行         public static int lineindex(IntPtr handle)        {            int 第一个字母前的字符数 = SendMessage(handle, EM_LINEINDEX, -1, 0);            return 第一个字母前的字符数;        }        //限制文本可输入长度        public static void limittext(IntPtr handle,int maxnum)        {            SendMessage(handle, EM_LIMITTEXT, maxnum, 0);        }        //撤销操作,可撤销很多次        public static void undo(IntPtr handle)        {            SendMessage(handle, EM_UNDO, 0, 0);        }        //设定只读        public static void serreadonly(IntPtr handle)        {            SendMessage(handle, EM_SETREADONLY, true, 0);        }        //控件获得焦点        public static void setfocus(IntPtr handle)        {            SendMessage(handle, WM_SETFOCUS, 0, 0);        }        //控件失去焦点        public static void killfocus(IntPtr handle)        {            SendMessage(handle, WM_KILLFOCUS, 0, 0);        }        //滚动        public static void scroll(IntPtr handle)        {            SendMessage(handle, EM_SCROLL, 3, 0);//滚动到下一页            SendMessage(handle, EM_SCROLL, 2, 0);//滚动到上一页            SendMessage(handle, EM_SCROLL, 1, 0);//滚动到下一行            SendMessage(handle, EM_SCROLL, 0, 0);//滚动到上一行        }        //选取指定范围字符串(要先获得焦点)        public static void setsel(IntPtr handle,int begin,int end)        {            SendMessage(handle, WM_SETFOCUS, 0, 0);            SendMessage(handle, EM_SETSEL, begin, end);        }//        用 InPtr 类型统一处理//        [DllImport("user32.dll", EntryPoint = "SendMessageA")]//        private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);//        public static int EM_GETRECT = 0xB2;//调用1//            Rectangle rect1 = new Rectangle();//            IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Rectangle)));//            Marshal.StructureToPtr(rect1, buffer, true);//            SendMessage(this.richTextBox1.Handle, EM_GETRECT, (IntPtr)0, buffer);//            rect1 = (Rectangle)Marshal.PtrToStructure(buffer, typeof(Rectangle));//            Marshal.FreeHGlobal(buffer);//            button1.Size = new Size(rect1.Width, rect1.Height);//调用2//            Rectangle rect = new Rectangle();//            GCHandle gch = GCHandle.Alloc(rect);//            SendMessage(this.richTextBox1.Handle, EM_GETRECT, (IntPtr)0, (IntPtr)gch);//            rect = (Rectangle)Marshal.PtrToStructure((IntPtr)gch, typeof(Rectangle));//            button1.Size = new Size(rect.Width, rect.Height);//            gch.Free();        //调节音量        const uint WM_APPCOMMAND = 0x319;        const uint APPCOMMAND_VOLUME_UP = 0x0a;        const uint APPCOMMAND_VOLUME_DOWN = 0x09;        const uint APPCOMMAND_VOLUME_MUTE = 0x08;        public static void volume(IntPtr handle)        {            SendMessage(handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_UP * 0x10000);//调高音量            SendMessage(handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_DOWN * 0x10000);//降低音量        }    }}
原创粉丝点击