win32消息系统

来源:互联网 发布:淘宝网店模板下载 编辑:程序博客网 时间:2024/05/01 17:02

win32消息系统
using System.Runtime.InteropServices ;
        [DllImport("user32")]
        static extern int SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);  
        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam);//+1重载具有返回值的,返回的东西就是StringBuilder
      

        [DllImport("user32")]
        public static extern int EnumWindows(CallBack x, int y);//枚举出所有的窗口


 [DllImport("user32")]
        static extern int PostMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);  
        [ DllImport("User32.dll")]
       static extern IntPtr FindWindow(string lpClassName,string lpWindowName);

       [DllImport("user32.dll")]
       public static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter,string lpszClass,string lpszWindow);


[DllImport("User32.dll")]
        static extern int GetWindowText(IntPtr handle, StringBuilder text, int MaxLen);// 得到句柄窗体的名称长度
[DllImport("User32.dll")]
        static extern int GetWindowTextLength(IntPtr handle);// 得到句柄窗体的字符串

 [DllImport("User32.dll")]
        static extern int GetClassName(IntPtr handle, StringBuilder text, int MaxLen);// 得到句柄窗体的classname


/**********************找子窗口******************************************
  IntPtr hwnd = FindWindow("Notepad",null);
            IntPtr child = FindWindowEx(hwnd, IntPtr.Zero, "Edit", null);
            // IntPtr hwnd = new IntPtr(0x00cc02b0);
            //SendMessage(hwnd, 0x0111, 0x0000a220, 0);


FindWindowEx

VB声明
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
说明
在窗口列表中寻找与指定条件相符的第一个子窗口
返回值
Long,找到的窗口的句柄。如未找到相符窗口,则返回零。会设置GetLastError
参数表
参数 类型及说明
hWnd1 Long,在其中查找子的父窗口。如设为零,表示使用桌面窗口(通常说的顶级窗口都被认为是桌面的子窗口,所以也会对它们进行查找)
hWnd2 Long,从这个窗口后开始查找。这样便可利用对FindWindowEx的多次调用找到符合条件的所有子窗口。如设为零,表示从第一个子窗口开始搜索
lpsz1 String,欲搜索的类名。零表示忽略
lpsz2 String,欲搜索的类名。零表示忽略


/****************************************************************

     /**********************取得窗口的标题******************************************      
             IntPtr hwnd2 = new IntPtr(0x00050522);
             int len = GetWindowTextLength(hwnd2)+1;
             StringBuilder text = new StringBuilder(len);
             int i = GetWindowText(hwnd2, text, len);
            
             System.Diagnostics.Debug.WriteLine(text.ToString());
             System.Diagnostics.Debug.WriteLine(i.ToString());
/****************************************************************


 /****************************************得到句柄窗体的classname******************************************      
             IntPtr hwnd2 = new IntPtr(0x00050522);
             int len = GetWindowTextLength(hwnd2)+1;
             StringBuilder text = new StringBuilder(len);
             int i = GetClassName(hwnd2, text, len);
            
             System.Diagnostics.Debug.WriteLine(text.ToString());
             System.Diagnostics.Debug.WriteLine(i.ToString());
/**********************************************************************************************************      
             IntPtr hwnd2 = new IntPtr(0x00050522);
             int len = GetWindowTextLength(hwnd2)+1;
             StringBuilder text = new StringBuilder(len);
             int i = GetWindowText(hwnd2, text, len);
            
             System.Diagnostics.Debug.WriteLine(text.ToString());
             System.Diagnostics.Debug.WriteLine(i.ToString());
/****************************************************************


/*******************************枚举出所有的窗口*********************************

 public delegate bool CallBack(int hwnd, int lParam);

        public static bool Report(int hwnd, int lParam)
        {

 

            System.Diagnostics.Debug.WriteLine(hwnd);

            return true;

        }

          private void Form1_Load(object sender, EventArgs e)
        {
            CallBack myCallBack = new CallBack(Report);

            EnumWindows(myCallBack, 0);

 

        }
/****************************************************************

     /**********************取得textbox的值******************************************     
 IntPtr hwnd2 = new IntPtr(0x000d00de);
            
             const int buffer_size = 1024;

             string aa = "";
             StringBuilder buffer = new StringBuilder(buffer_size);
             SendMessage(hwnd2, 0xd, buffer_size, buffer);

 

             System.Diagnostics.Debug.WriteLine(buffer.ToString());


/****************************************************************