很多朋友在研究如何控制目标程序中的文本框等控件的值,偶然的机会我也需要做这么个东东,花了一个晚上研究终于搞定,呵呵,在这里与大家共享。
代码思路并不复杂,程序运行后用CTRL+G扑获目标窗体的句柄,然后用EnumChildWindows枚举窗体上的控件,用PostMessage发送消息给目标实现控制。
实现环境是Borland C++ Builder,如果你用VC需要稍微做点修改。
代码思路并不复杂,程序运行后用CTRL+G扑获目标窗体的句柄,然后用EnumChildWindows枚举窗体上的控件,用PostMessage发送消息给目标实现控制。
实现环境是Borland C++ Builder,如果你用VC需要稍微做点修改。
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "trayicon"
#pragma resource "*.dfm"
TForm1 *Form1;
HWND hEditList[64];
int hEditCount;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
::RegisterHotKey(Handle,1000,MOD_CONTROL,'G'); //G:Get Window
}
//---------------------------------------------------------------------------
LRESULT CALLBACK numChildProc(HWND hwnd, LPARAM)//枚举窗体所有控件的回调函数
{
char text[128], title[128];
GetClassName(hwnd, text, 128);
//SendMessage(hwnd, WM_GETTEXT, (WPARAM)128, (LPARAM)title);
Form1->ListBox1->Items->Add(text);
hEditList[hEditCount]=hwnd;
//Form1->ListBox1->Items->Add(title);
hEditCount++;
return true; //true表示继续取,false表示不取
}
//---------------------------------------------------------------------------
void __fastcall TForm1::getActiveWindow()
{
char szCaption[255];
hWindow=GetForegroundWindow();
GetWindowText(hWindow,szCaption,255);
Edit3->Text=szCaption;
//::SetForegroundWindow( Handle );//激活当前窗口
//ShowWindow(Handle,SW_RESTORE);
//Application->BringToFront();
TrayIcon1->Restore();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sendToEdit()
{
//SetWindowText(hWindow,"JJJJJ");
//HWND hEdit = ::FindWindowEx( hWindow, NULL, Edit1->Text.c_str(), NULL );//查找Edit句柄
HWND hEdit=hEditList[ListBox1->ItemIndex];
if(hEdit==NULL)
return;
if(::GetForegroundWindow()!=hWindow)
::SetForegroundWindow( hWindow );//激活当前窗口
::SetFocus( hEdit );//至焦点
char * msgSend;
::SendMessage(hEdit ,WM_SETTEXT,255,(LPARAM)Edit2->Text.c_str());
//::PostMessage(hEdit ,WM_KEYUP,VK_RETURN,0);
::PostMessage(hEdit ,WM_KEYDOWN,VK_RETURN,0);
//::PostMessage(hEdit ,WM_KEYUP,VK_RETURN,0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(hWindow==NULL)
{
ShowMessage("请先用CTRL+G得到目标窗体");
return;
}
//ShowMessage(Edit2->Text);
sendToEdit();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WndProc(TMessage& Msg)//在这里实现热键
{
if(Msg.Msg==WM_HOTKEY)//得到热键消息
{
if(LOWORD(Msg.LParam)==MOD_CONTROL && HIWORD(Msg.LParam)=='G')
{
getActiveWindow();
if(hWindow==NULL)
return;
ListBox1->Clear();
hEditCount=0;
EnumChildWindows(hWindow, (int (__stdcall *)())numChildProc, 0); //枚举窗体所有的控件
}
}
else
TForm::WndProc(Msg);//将消息向下传递
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
hide();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
Edit1->Text=ListBox1->Items->Strings[ListBox1->ItemIndex];
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit3DblClick(TObject *Sender)
{
Edit3->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1DblClick(TObject *Sender)
{
Edit1->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit2DblClick(TObject *Sender)
{
Edit2->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
SetWindowPos(Handle,HWND_TOPMOST,0,0,0,0,SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE);//设置窗口在最上。
}
//---------------------------------------------------------------------------
void __fastcall TForm1::hide()
{
TrayIcon1->Minimize();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::show()
{
TrayIcon1->Restore();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrayIcon1Click(TObject *Sender)
{
show();
}
#include <vcl.h>
#pragma hdrstop
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "trayicon"
#pragma resource "*.dfm"
TForm1 *Form1;
HWND hEditList[64];
int hEditCount;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
::RegisterHotKey(Handle,1000,MOD_CONTROL,'G'); //G:Get Window
}
//---------------------------------------------------------------------------
LRESULT CALLBACK numChildProc(HWND hwnd, LPARAM)//枚举窗体所有控件的回调函数
{
char text[128], title[128];
GetClassName(hwnd, text, 128);
//SendMessage(hwnd, WM_GETTEXT, (WPARAM)128, (LPARAM)title);
Form1->ListBox1->Items->Add(text);
hEditList[hEditCount]=hwnd;
//Form1->ListBox1->Items->Add(title);
hEditCount++;
return true; //true表示继续取,false表示不取
}
//---------------------------------------------------------------------------
void __fastcall TForm1::getActiveWindow()
{
char szCaption[255];
hWindow=GetForegroundWindow();
GetWindowText(hWindow,szCaption,255);
Edit3->Text=szCaption;
//::SetForegroundWindow( Handle );//激活当前窗口
//ShowWindow(Handle,SW_RESTORE);
//Application->BringToFront();
TrayIcon1->Restore();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sendToEdit()
{
//SetWindowText(hWindow,"JJJJJ");
//HWND hEdit = ::FindWindowEx( hWindow, NULL, Edit1->Text.c_str(), NULL );//查找Edit句柄
HWND hEdit=hEditList[ListBox1->ItemIndex];
if(hEdit==NULL)
return;
if(::GetForegroundWindow()!=hWindow)
::SetForegroundWindow( hWindow );//激活当前窗口
::SetFocus( hEdit );//至焦点
char * msgSend;
::SendMessage(hEdit ,WM_SETTEXT,255,(LPARAM)Edit2->Text.c_str());
//::PostMessage(hEdit ,WM_KEYUP,VK_RETURN,0);
::PostMessage(hEdit ,WM_KEYDOWN,VK_RETURN,0);
//::PostMessage(hEdit ,WM_KEYUP,VK_RETURN,0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(hWindow==NULL)
{
ShowMessage("请先用CTRL+G得到目标窗体");
return;
}
//ShowMessage(Edit2->Text);
sendToEdit();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WndProc(TMessage& Msg)//在这里实现热键
{
if(Msg.Msg==WM_HOTKEY)//得到热键消息
{
if(LOWORD(Msg.LParam)==MOD_CONTROL && HIWORD(Msg.LParam)=='G')
{
getActiveWindow();
if(hWindow==NULL)
return;
ListBox1->Clear();
hEditCount=0;
EnumChildWindows(hWindow, (int (__stdcall *)())numChildProc, 0); //枚举窗体所有的控件
}
}
else
TForm::WndProc(Msg);//将消息向下传递
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
hide();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
Edit1->Text=ListBox1->Items->Strings[ListBox1->ItemIndex];
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit3DblClick(TObject *Sender)
{
Edit3->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1DblClick(TObject *Sender)
{
Edit1->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit2DblClick(TObject *Sender)
{
Edit2->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
SetWindowPos(Handle,HWND_TOPMOST,0,0,0,0,SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE);//设置窗口在最上。
}
//---------------------------------------------------------------------------
void __fastcall TForm1::hide()
{
TrayIcon1->Minimize();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::show()
{
TrayIcon1->Restore();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TrayIcon1Click(TObject *Sender)
{
show();
}