WIN32 枚举窗口 鼠标键盘事件

 

//查找窗口    
  TCHAR szTitle[MAX_PATH] = {0};                                
    HWND hwnd = ::FindWindow(TEXT("#32770"),"PE工具");                                
    if(hwnd != NULL)                                
    {                                
        //修改窗口标题                            
        ::SetWindowText(hwnd,"PE工具123");                            
    }                                
    else                                
    {                                
        ::MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);                            
    }

 

    //窗口控制                                
                                
    TCHAR szTitle[MAX_PATH] = {0};                                
    HWND hwnd = ::FindWindow(TEXT("#32770"),TEXT("PE工具"));                                
    if(hwnd != NULL)                                
    {                                
        typedef void (WINAPI *PSWITCHTOTHISWINDOW) (HWND,BOOL);                            
        PSWITCHTOTHISWINDOW SwitchToThisWindow;                            
        HMODULE hUser32=LoadLibrary("user32.dll");                            
        SwitchToThisWindow=(PSWITCHTOTHISWINDOW)GetProcAddress(hUser32,"SwitchToThisWindow");                            
                                
        //切换窗口                            
        SwitchToThisWindow(hwnd,false);                            
                                
        Sleep(3000);                            
        //关闭窗口                             
        ::SendMessage(hwnd,WM_CLOSE,0,0);                            
    }                                
    else                                
    {                                
        ::MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);                            
    }                            

 

    //查找子窗口                                                                
    TCHAR szTitle[MAX_PATH] = {0};                                
    HWND hwnd = ::FindWindow(TEXT("#32770"),"Dialog");                                
    if(hwnd != NULL)                                
    {                                
        //查找子窗口                            
        HWND hEdit = FindWindowEx(hwnd,NULL,"Edit","");                            
        //设置标题                            
        ::SetWindowText(hEdit,"文本框新的标题");                            
        //修改内容                            
        ::SendMessage(hEdit,WM_SETTEXT,0,(LPARAM)"新的内容");                            
    }                                
    else                                
    {                                
        ::MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);                            
    }

 

//在多个相同类中获取唯一ID
    TCHAR szTitle[MAX_PATH] = {0};                            
    HWND hwnd = ::FindWindow(TEXT("#32770"),TEXT("吃货作业"));                            
    if(hwnd != NULL)                            
    {
        //查找子窗口                        
        HWND hEdit =::GetDlgItem(hwnd,0x3EE); //3EE为ID值                        
        //获取内容                        
        ::SendMessage(hEdit,WM_GETTEXT,MAX_PATH,(LPARAM)szTitle);                        
        //修改内容                        
        ::SendMessage(hEdit,WM_SETTEXT,0,(LPARAM)"新的内容");                        
    }                            
    else                            
    {                            
        ::MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);                        
    }                

 

//枚举打开所有子窗口
BOOL CALLBACK EnumChildProc(HWND hWnd,LPARAM lParam) { TCHAR szTitle[MAX_PATH] = {0}; ::GetWindowText(hWnd,szTitle,MAX_PATH); MessageBox(NULL,szTitle,"[子窗口]",MB_OK); return true; } VOID EnumChildWindow() { TCHAR szTitle[MAX_PATH] = {0}; HWND hWnd = ::FindWindow(TEXT("#32770"),TEXT("吃货作业")); if(hWnd != NULL) { ::EnumChildWindows(hWnd,EnumChildProc,0); } else { ::MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK); } }

 

//枚举所有打开窗口                                                        
BOOL CALLBACK EnumOpenWindowProc(HWND hWnd,LPARAM lParam)                              
{                              
    TCHAR szTitle[MAX_PATH] = {0};                            
    ::GetWindowText(hWnd,szTitle,MAX_PATH);                             
    MessageBox(NULL,szTitle,"[窗口]",MB_OK);                              
    if(strcmp(szTitle,"吃货作业") == 0)                        
    {                        
        MessageBox(NULL,szTitle,"[窗口]",MB_OK);                      
        return FALSE;                    
    }                        
    return TRUE;                              
}      
                        
VOID EnumOpenWindows()                            
{                            
    EnumWindows(EnumOpenWindowProc,NULL);                        
}                            

 

//模拟鼠标点击
TCHAR szTitle[MAX_PATH] = {0};    
    RECT r;    
    HWND hwnd = ::FindWindow(TEXT("#32770"),TEXT("吃货作业"));    
    if(hwnd != NULL)    
    {    
        HWND hButton = FindWindowEx(hwnd,NULL,"Button","OK");
        
        //获取窗口坐标
        ::GetWindowRect(hButton,&r);
        
        printf("%d %d",r.left,r.top);
        
        //设置鼠标的位置
        ::SetCursorPos(r.left+10,r.top+5);
        Sleep(2000);
        //鼠标左键单击
        mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//点下左键 
        mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//松开左键
    }    
    else    
    {    
        ::MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);
    }

 

//模拟键盘点击(搜索:键盘键与虚拟键码对照表)            
    TCHAR szTitle[MAX_PATH] = {0};    
    RECT r;    
    HWND hwnd = ::FindWindow(TEXT("#32770"),TEXT("吃货作业"));    
    if(hwnd != NULL)    
    {    
        //HWND hButton = FindWindowEx(hwnd,NULL,"Button","刷新(&R)");
        HWND hEdit =::GetDlgItem(hwnd,0x3E9);
        
        //获取窗口坐标
        ::GetWindowRect(hEdit,&r);
        
        //设置鼠标的位置
        ::SetCursorPos(r.left+1,r.top+1);
        Sleep(1000);
        
        //鼠标左键单击
        mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//点下左键 
        mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//松开左键
        
        //模拟键盘
        keybd_event(97,0,0,0);
        keybd_event(97,0,KEYEVENTF_KEYUP,0);
        Sleep(1000);
        keybd_event(66,0,0,0);
        keybd_event(66,0,KEYEVENTF_KEYUP,0);
        Sleep(1000);
        keybd_event(16,0,0,0);
        keybd_event(67,0,0,0);
        keybd_event(67,0,KEYEVENTF_KEYUP,0);
        keybd_event(16,0,KEYEVENTF_KEYUP,0);
        
    }    
    else    
    {    
        ::MessageBox(NULL,TEXT("窗口没有找到"),TEXT("[ERROR]"),MB_OK);
    }

 

上一篇:Windows环境下使用pip安装pycrypto包报错


下一篇:win32汇编MessageBox