如何向一个指定的按钮或者EDIT的控件发送消息,模拟手动点击或者手动输入呢???

时间:2022-11-02 16:55:07
如何向一个指定的按钮或者EDIT的控件发送消息,模拟手动点击或者手动输入呢???

16 个解决方案

#1


可以这样:
模拟一个按钮进行点击,可用两种方法:
PostMessage(Button1->Handle,WM_LBUTTONDOWN,MK_LBUTTON,0);
PostMessage(Button1->Handle,WM_LBUTTONUP,MK_LBUTTON,0);
或用鼠标事件:
mouse_event

模似对文本框的输入:
PostMessage(Edit1->Handle,WM_CHAR,'A',0);
或用keybd_event来实现

#2


ToolButton1->OnClick(ToolButton1);

#3


要声明的是WM_LBUTTONDOWN和WM_LBUTTONUP消息中的两个附加参数LPARAM可以设置鼠标的位置。

#4


我指的是对另一个进程的某些控件进行的模拟操作,并且这些进程不一定都是用CB开发的,这又如何去做那(对本程序中的控件的操作我已经实现过了,并且没有什么问题)。

#5


只要你是windows平台上,都可以调用API函数啊。

#6


先取得控件的handle,然后发消息

#7


首先我们可以枚举窗口,得到窗口的句柄。在得到窗口的句柄后,再遍历该窗口上的控件,取得相应的句柄,就可以使用消息函数和键盘模拟和鼠标模拟。

#8


如何得到另一个进程中指定控件的句柄呢??能不能给一段代码呢??

#9


我现在手上也没有代码,不过用API肯定可以实现,这样,我晚上试一试,我要下班了。

#10


好的谢谢了

#11


有人回答吗???我可以再加分的

#12


转载:遍历一个窗体句柄下的所有句柄,找到符合条件的句柄
#include  <vcl.h> 
#pragma  hdrstop 

#include  "Unit1.h" 
#include  <winuser.h> 
#include  <stdio.h> 
//--------------------------------------------------------------------------- 
#pragma  package(smart_init) 
#pragma  resource  "*.dfm" 
//typedef 
struct  WINLIST 

        HANDLE  hWnd; 
char  cWinBuf[256]; 
}  ; 
void  CloseSpedia(void); 
//BOOL  CALLBACK  EnumWindowsProc(  HWND  hWnd,  LPARAM  lParam  ); 
//char  buffer[256]; 
TForm1  *Form1; 
struct  WINLIST  gWinList[256]; 
int  giCountWin,j; 
//-cpp------------ 

bool  CALLBACK  EnumWindowsProc(  HWND  hWnd,  LPARAM  lParam  ) 

        char  buffer[256]; 

        GetWindowText(hWnd,  buffer,  256); 
        if  (  strlen(buffer)  ) 
        { 
                if  (giCountWin  <  256) 
                { 
                        gWinList[  giCountWin].hWnd  =  hWnd; 
                        strcpy(gWinList[  giCountWin].cWinBuf,buffer); 
                        giCountWin  ++; 
                } 
        } 
        return  TRUE; 

//----------------------------------------------------- 
void  CloseSpedia(void)// 

        giCountWin  =  0; 
        EnumWindows(  (WNDENUMPROC)EnumWindowsProc,0); 
//        ShowMessage(IntToStr(giCountWin).c_str()); 
        for  (  j  =  0;  j<giCountWin;  j++) 
        { 
//                if(j>0)Form1->CheckListBox1->Items->Strings[j]=gWinList[j].cWinBuf; 
                Form1->ListBox1->Items->Add(gWinList[j].cWinBuf); 
//                ShowMessage(gWinList[j].cWinBuf); 

/*                if  (  strcmp(gWinList[j].cWinBuf,"618m")  ==  0  ) 
                { 
                        ShowWindow(gWinList[j].hWnd,SW_MAXIMIZE);          //重新打开窗口 
//                        ShowMessage(gWinList[j].cWinBuf); 
//                        PostMessage(  gWinList[j].hWnd,WM_SHOWWINDOW,0,0); 
//                        break; 
                } 

                if  (  strcmp(gWinList[j].cWinBuf,"未命名  -  画图")  ==  0  ) 
                { 
                        PostMessage(  gWinList[j].hWnd,WM_CLOSE,0,0);  //Close  the  window 
//                        break; 
                } 
*/ 
        } 

// 

//--------------------------------------------------------------------------- 
__fastcall  TForm1::TForm1(TComponent*  Owner) 
        :  TForm(Owner) 


//--------------------------------------------------------------------------- 

void  __fastcall  TForm1::Button1Click(TObject  *Sender) 

        CloseSpedia(); 

//--------------------------------------------------------------------------- 

void  __fastcall  TForm1::Button3Click(TObject  *Sender) 

        Close(); 

//--------------------------------------------------------------------------- 
void  __fastcall  TForm1::Button2Click(TObject  *Sender) 

        ListBox1->Clear(); 

//---------------------------------------------------------------------------
将  EnumWindows  改为  EnumChildWindows

#13


FindWindowEx找已知句柄窗体上的控件,不过,用这个函数你需要知道控件的名称,我昨天晚上试了一试,没有成功。我今天再试一试。

#14


我的笨办法,先获得窗体的句柄,然后通过位置获得按钮或者Edit的位置,我下面的程序就是一个监控程序,发现提示登录数据库的对话框,然后就填写密码,自动按回车,登录,用来监控我的一个服务器后台程序。
你参考一下吧。
//---------------------------------------------------------------------------
#include <vcl.h>
#include <inifiles.hpp>
#include <stdio.h>
#pragma hdrstop

#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
int Counter=0;
bool startdrag=false;
bool startdrag2=false;
bool disableall=true;
bool forcesame=false;
HWND hmain;
HWND hchild;
FILE * log;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::TestButtonClick(TObject *Sender)
{
//搜索窗体
    HWND hw=NULL,hwchild;
    if(!CaptionEdit->Text.IsEmpty())
    {
        if(forcesame)
            hw=FindWindow(ClassEdit->Text.c_str(),CaptionEdit->Text.c_str());
        else
            hw=FindWindow(NULL,CaptionEdit->Text.c_str());
//        hwchild=GetWindow(hw,UINT uCmd); mouse_event Processs
//        FindWindow(NULL,ChildClassEdit->Text.c_str());
    }
//发送消息
    if(hw)
    {
        if(ChildFillEdit->Text.IsEmpty())
        {
            StatusBar1->SimpleText="要填充的内容为空!";
        }
        BringWindowToTop(hw);
        POINT pm;
        pm.x=CXEdit->Text.ToIntDef(-1);
        pm.y=CYEdit->Text.ToIntDef(-1);
        hchild=WindowFromPoint(pm);
        char capstr[100];
        if(forcesame)
        {
            GetClassName(hchild,capstr,100);
            if(String(capstr)!=ChildClassEdit->Text)
            {
                StatusBar1->SimpleText="没有发现类名匹配的子窗体!";
                return;
            }
        }
        strcpy(capstr,ChildFillEdit->Text.c_str());
//        SetWindowText(hchild,capstr);
        SendMessage(hchild,WM_SETTEXT,255,(LPARAM)capstr);
        switch(CloseCB->ItemIndex)
        {
            case 0:
                PostMessage (hw,WM_KEYDOWN,VK_RETURN,0);
                break;
            case 1:
                PostMessage (hw,WM_KEYDOWN,VK_ESCAPE,0);
                break;
            case 2:
                PostMessage (hw,WM_KEYDOWN,VK_SPACE,0);
                break;
/*            case 3:
                PostMessage (hw,WM_KEYDOWN,VK_SPACE,0);
                break;
*/        }
        StatusBar1->SimpleText="成功发送消息!\t"+IntToStr(Counter>9?(Counter=Counter-10,Counter):++Counter);
        if(log)
            fprintf(log,"%s%s\n",Now().FormatString("YYYY-MM-DD hh:nn:ss").c_str()," 发现窗体,发送消息成功!");
    }
    else
        if(forcesame)
            StatusBar1->SimpleText="没有标题为["+CaptionEdit->Text+"]类名为["+ClassEdit->Text+"]的窗体!";
        else
            StatusBar1->SimpleText="没有标题为["+CaptionEdit->Text+"]的窗体!";
    Application->ProcessMessages();
//    mouse_event(MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE,(Screen->Width-4)*65535/Screen->Width,(Screen->Height-5)*65535/Screen->Height,0,0);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CrossImageMouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
    if(startdrag)
    {
        POINT pm;POINT spm;
        pm.x=X; pm.y=Y;
        spm=CrossImage->ClientToScreen(pm);
        PXEdit->Text=IntToStr(spm.x);
        PYEdit->Text=IntToStr(spm.y);
        hmain=WindowFromPoint(spm);
        char capstr[100];
        GetWindowText(hmain,capstr,100);
        CaptionEdit->Text=String(capstr);
        GetClassName(hmain,capstr,100);
        ClassEdit->Text=String(capstr);
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CrossImageMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if(!disableall)
    {
        Screen->Cursor=crCross;
        startdrag=true;
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CrossImageMouseUp(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if(!disableall)
    {
        Screen->Cursor=crDefault;
        startdrag=false;
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CrossImage2MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if(!disableall)
    {
        Screen->Cursor=crCross;
        startdrag2=true;
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CrossImage2MouseUp(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if(!disableall)
    {
        Screen->Cursor=crDefault;
        startdrag2=false;
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CrossImage2MouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
    if(startdrag2)
    {
        POINT pm; POINT spm;
        pm.x=X;
        pm.y=Y;
        char temp[256];
        spm=CrossImage2->ClientToScreen(pm);
        CXEdit->Text=IntToStr(spm.x);
        CYEdit->Text=IntToStr(spm.y);
        hchild=WindowFromPoint(spm);
        char capstr[100];
        GetWindowText(hchild,capstr,100);
        GetClassName(hchild,capstr,100);
        ChildClassEdit->Text=String(capstr);
        SendMessage(hchild,WM_GETTEXT,255,(LPARAM)temp);
        ChildFillEdit->Text=temp;

    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ChildFillEditDblClick(TObject *Sender)
{
    SetWindowText(hchild,ChildFillEdit->Text.c_str());
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ControlP(bool c)
{
    IntervalEdit->Enabled=c;
    CloseCB->Enabled=c;
    AutoCB->Enabled=c;
    CaptionEdit->Enabled=c;
    ClassEdit->Enabled=c;
    ChildClassEdit->Enabled=c;
    ChildFillEdit->Enabled=c;
    CXEdit->Enabled=c;
    CYEdit->Enabled=c;
    PXEdit->Enabled=c;
    PYEdit->Enabled=c;
}
void __fastcall TMainForm::Label2DblClick(TObject *Sender)
{
    disableall=!disableall;
    ControlP(!disableall);
}

#15


谢谢各位的帮助,我先试一试如果成功我马上给分

#16


studay

#1


可以这样:
模拟一个按钮进行点击,可用两种方法:
PostMessage(Button1->Handle,WM_LBUTTONDOWN,MK_LBUTTON,0);
PostMessage(Button1->Handle,WM_LBUTTONUP,MK_LBUTTON,0);
或用鼠标事件:
mouse_event

模似对文本框的输入:
PostMessage(Edit1->Handle,WM_CHAR,'A',0);
或用keybd_event来实现

#2


ToolButton1->OnClick(ToolButton1);

#3


要声明的是WM_LBUTTONDOWN和WM_LBUTTONUP消息中的两个附加参数LPARAM可以设置鼠标的位置。

#4


我指的是对另一个进程的某些控件进行的模拟操作,并且这些进程不一定都是用CB开发的,这又如何去做那(对本程序中的控件的操作我已经实现过了,并且没有什么问题)。

#5


只要你是windows平台上,都可以调用API函数啊。

#6


先取得控件的handle,然后发消息

#7


首先我们可以枚举窗口,得到窗口的句柄。在得到窗口的句柄后,再遍历该窗口上的控件,取得相应的句柄,就可以使用消息函数和键盘模拟和鼠标模拟。

#8


如何得到另一个进程中指定控件的句柄呢??能不能给一段代码呢??

#9


我现在手上也没有代码,不过用API肯定可以实现,这样,我晚上试一试,我要下班了。

#10


好的谢谢了

#11


有人回答吗???我可以再加分的

#12


转载:遍历一个窗体句柄下的所有句柄,找到符合条件的句柄
#include  <vcl.h> 
#pragma  hdrstop 

#include  "Unit1.h" 
#include  <winuser.h> 
#include  <stdio.h> 
//--------------------------------------------------------------------------- 
#pragma  package(smart_init) 
#pragma  resource  "*.dfm" 
//typedef 
struct  WINLIST 

        HANDLE  hWnd; 
char  cWinBuf[256]; 
}  ; 
void  CloseSpedia(void); 
//BOOL  CALLBACK  EnumWindowsProc(  HWND  hWnd,  LPARAM  lParam  ); 
//char  buffer[256]; 
TForm1  *Form1; 
struct  WINLIST  gWinList[256]; 
int  giCountWin,j; 
//-cpp------------ 

bool  CALLBACK  EnumWindowsProc(  HWND  hWnd,  LPARAM  lParam  ) 

        char  buffer[256]; 

        GetWindowText(hWnd,  buffer,  256); 
        if  (  strlen(buffer)  ) 
        { 
                if  (giCountWin  <  256) 
                { 
                        gWinList[  giCountWin].hWnd  =  hWnd; 
                        strcpy(gWinList[  giCountWin].cWinBuf,buffer); 
                        giCountWin  ++; 
                } 
        } 
        return  TRUE; 

//----------------------------------------------------- 
void  CloseSpedia(void)// 

        giCountWin  =  0; 
        EnumWindows(  (WNDENUMPROC)EnumWindowsProc,0); 
//        ShowMessage(IntToStr(giCountWin).c_str()); 
        for  (  j  =  0;  j<giCountWin;  j++) 
        { 
//                if(j>0)Form1->CheckListBox1->Items->Strings[j]=gWinList[j].cWinBuf; 
                Form1->ListBox1->Items->Add(gWinList[j].cWinBuf); 
//                ShowMessage(gWinList[j].cWinBuf); 

/*                if  (  strcmp(gWinList[j].cWinBuf,"618m")  ==  0  ) 
                { 
                        ShowWindow(gWinList[j].hWnd,SW_MAXIMIZE);          //重新打开窗口 
//                        ShowMessage(gWinList[j].cWinBuf); 
//                        PostMessage(  gWinList[j].hWnd,WM_SHOWWINDOW,0,0); 
//                        break; 
                } 

                if  (  strcmp(gWinList[j].cWinBuf,"未命名  -  画图")  ==  0  ) 
                { 
                        PostMessage(  gWinList[j].hWnd,WM_CLOSE,0,0);  //Close  the  window 
//                        break; 
                } 
*/ 
        } 

// 

//--------------------------------------------------------------------------- 
__fastcall  TForm1::TForm1(TComponent*  Owner) 
        :  TForm(Owner) 


//--------------------------------------------------------------------------- 

void  __fastcall  TForm1::Button1Click(TObject  *Sender) 

        CloseSpedia(); 

//--------------------------------------------------------------------------- 

void  __fastcall  TForm1::Button3Click(TObject  *Sender) 

        Close(); 

//--------------------------------------------------------------------------- 
void  __fastcall  TForm1::Button2Click(TObject  *Sender) 

        ListBox1->Clear(); 

//---------------------------------------------------------------------------
将  EnumWindows  改为  EnumChildWindows

#13


FindWindowEx找已知句柄窗体上的控件,不过,用这个函数你需要知道控件的名称,我昨天晚上试了一试,没有成功。我今天再试一试。

#14


我的笨办法,先获得窗体的句柄,然后通过位置获得按钮或者Edit的位置,我下面的程序就是一个监控程序,发现提示登录数据库的对话框,然后就填写密码,自动按回车,登录,用来监控我的一个服务器后台程序。
你参考一下吧。
//---------------------------------------------------------------------------
#include <vcl.h>
#include <inifiles.hpp>
#include <stdio.h>
#pragma hdrstop

#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
int Counter=0;
bool startdrag=false;
bool startdrag2=false;
bool disableall=true;
bool forcesame=false;
HWND hmain;
HWND hchild;
FILE * log;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::TestButtonClick(TObject *Sender)
{
//搜索窗体
    HWND hw=NULL,hwchild;
    if(!CaptionEdit->Text.IsEmpty())
    {
        if(forcesame)
            hw=FindWindow(ClassEdit->Text.c_str(),CaptionEdit->Text.c_str());
        else
            hw=FindWindow(NULL,CaptionEdit->Text.c_str());
//        hwchild=GetWindow(hw,UINT uCmd); mouse_event Processs
//        FindWindow(NULL,ChildClassEdit->Text.c_str());
    }
//发送消息
    if(hw)
    {
        if(ChildFillEdit->Text.IsEmpty())
        {
            StatusBar1->SimpleText="要填充的内容为空!";
        }
        BringWindowToTop(hw);
        POINT pm;
        pm.x=CXEdit->Text.ToIntDef(-1);
        pm.y=CYEdit->Text.ToIntDef(-1);
        hchild=WindowFromPoint(pm);
        char capstr[100];
        if(forcesame)
        {
            GetClassName(hchild,capstr,100);
            if(String(capstr)!=ChildClassEdit->Text)
            {
                StatusBar1->SimpleText="没有发现类名匹配的子窗体!";
                return;
            }
        }
        strcpy(capstr,ChildFillEdit->Text.c_str());
//        SetWindowText(hchild,capstr);
        SendMessage(hchild,WM_SETTEXT,255,(LPARAM)capstr);
        switch(CloseCB->ItemIndex)
        {
            case 0:
                PostMessage (hw,WM_KEYDOWN,VK_RETURN,0);
                break;
            case 1:
                PostMessage (hw,WM_KEYDOWN,VK_ESCAPE,0);
                break;
            case 2:
                PostMessage (hw,WM_KEYDOWN,VK_SPACE,0);
                break;
/*            case 3:
                PostMessage (hw,WM_KEYDOWN,VK_SPACE,0);
                break;
*/        }
        StatusBar1->SimpleText="成功发送消息!\t"+IntToStr(Counter>9?(Counter=Counter-10,Counter):++Counter);
        if(log)
            fprintf(log,"%s%s\n",Now().FormatString("YYYY-MM-DD hh:nn:ss").c_str()," 发现窗体,发送消息成功!");
    }
    else
        if(forcesame)
            StatusBar1->SimpleText="没有标题为["+CaptionEdit->Text+"]类名为["+ClassEdit->Text+"]的窗体!";
        else
            StatusBar1->SimpleText="没有标题为["+CaptionEdit->Text+"]的窗体!";
    Application->ProcessMessages();
//    mouse_event(MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE,(Screen->Width-4)*65535/Screen->Width,(Screen->Height-5)*65535/Screen->Height,0,0);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CrossImageMouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
    if(startdrag)
    {
        POINT pm;POINT spm;
        pm.x=X; pm.y=Y;
        spm=CrossImage->ClientToScreen(pm);
        PXEdit->Text=IntToStr(spm.x);
        PYEdit->Text=IntToStr(spm.y);
        hmain=WindowFromPoint(spm);
        char capstr[100];
        GetWindowText(hmain,capstr,100);
        CaptionEdit->Text=String(capstr);
        GetClassName(hmain,capstr,100);
        ClassEdit->Text=String(capstr);
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CrossImageMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if(!disableall)
    {
        Screen->Cursor=crCross;
        startdrag=true;
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CrossImageMouseUp(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if(!disableall)
    {
        Screen->Cursor=crDefault;
        startdrag=false;
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CrossImage2MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if(!disableall)
    {
        Screen->Cursor=crCross;
        startdrag2=true;
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CrossImage2MouseUp(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if(!disableall)
    {
        Screen->Cursor=crDefault;
        startdrag2=false;
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CrossImage2MouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
    if(startdrag2)
    {
        POINT pm; POINT spm;
        pm.x=X;
        pm.y=Y;
        char temp[256];
        spm=CrossImage2->ClientToScreen(pm);
        CXEdit->Text=IntToStr(spm.x);
        CYEdit->Text=IntToStr(spm.y);
        hchild=WindowFromPoint(spm);
        char capstr[100];
        GetWindowText(hchild,capstr,100);
        GetClassName(hchild,capstr,100);
        ChildClassEdit->Text=String(capstr);
        SendMessage(hchild,WM_GETTEXT,255,(LPARAM)temp);
        ChildFillEdit->Text=temp;

    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ChildFillEditDblClick(TObject *Sender)
{
    SetWindowText(hchild,ChildFillEdit->Text.c_str());
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ControlP(bool c)
{
    IntervalEdit->Enabled=c;
    CloseCB->Enabled=c;
    AutoCB->Enabled=c;
    CaptionEdit->Enabled=c;
    ClassEdit->Enabled=c;
    ChildClassEdit->Enabled=c;
    ChildFillEdit->Enabled=c;
    CXEdit->Enabled=c;
    CYEdit->Enabled=c;
    PXEdit->Enabled=c;
    PYEdit->Enabled=c;
}
void __fastcall TMainForm::Label2DblClick(TObject *Sender)
{
    disableall=!disableall;
    ControlP(!disableall);
}

#15


谢谢各位的帮助,我先试一试如果成功我马上给分

#16


studay