如何用windows消息执行Button的OnClick事件

drlqyy 2007-06-18 05:43:41
为何用SendMessage(hEdit, WM_LBUTTONDOWN,MK_LBUTTON,0);只能把焦点聚在Button h上而不能触发OnClick事件
...全文
699 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
i_love_pc 2007-06-19
  • 打赏
  • 举报
回复
蹭点分!
drlqyy 2007-06-18
  • 打赏
  • 举报
回复
感谢N27741(软件菜鸟)问题已解决

PostMessage(hButton,WM_LBUTTONDOWN,BM_CLICK,0);
PostMessage(hButton,WM_LBUTTONUP,BM_CLICK,0);
n27741 2007-06-18
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
PostMessage(Button1->Handle,WM_LBUTTONDOWN,0,0);
PostMessage(Button1->Handle,WM_LBUTTONUP,0,0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessage("button1 be clicked");
}
//---------------------------------------------------------------------------
drlqyy 2007-06-18
  • 打赏
  • 举报
回复
程序没有反应
cczlp 2007-06-18
  • 打赏
  • 举报
回复
按下还有抬起
SendMessage(hEdit, WM_LBUTTONDOWN,MK_LBUTTON,0);
SendMessage(hEdit, WM_LBUTTONUP,MK_LBUTTON,0);
Avoid 2007-06-18
  • 打赏
  • 举报
回复
WM_LBUTTONDOWN这是鼠标右键消息。。。。
drlqyy 2007-06-18
  • 打赏
  • 举报
回复
我的目标窗口有3个Button按扭,两个Edit文本框,要触发的按钮按扭是第二个,还是不行
__fastcall TfmMain::TfmMain(TComponent* Owner)
: TForm(Owner)
{
strAppPath = ExtractFilePath(Application->ExeName);
if(strAppPath.LastDelimiter("\\") != strAppPath.Length())
strAppPath += "\\";
TIniFile *pIni = new TIniFile(strAppPath + "autorun.inf");
strProduceName[0] = pIni->ReadString("Install", "SaleCard", "Explorer.exe");
delete pIni;
strOfficeSn[0] = "P0586";
strOfficeSn[1] = "府谷县大昌汗乡李家梁煤矿";

}
kk=0;
BOOL CALLBACK EnumChildProc0(HWND hWnd, LPARAM lParam)
{
char szClassName[30];
GetClassName(hWnd, szClassName, 29);
if(strcmp(szClassName, "WindowsForms10.BUTTON.app4") == 0)
{
*((HWND *)lParam) = hWnd;
kk++;
if(kk==2)
return false;
else
return true;
}
return true;
}
void __fastcall TfmMain::FormCreate(TObject *Sender)
{
kk = 0;
}

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


void __fastcall TfmMain::btn2Click(TObject *Sender)
{
nIndex= ((TImage *)Sender)->Tag;
if(nIndex < 0 || nIndex > 2)
ShellExecute(Handle, "open", "Explorer.exe", NULL, NULL,SW_SHOW ); // SW_HIDE
else
ShellExecute(Handle, "open",
(strAppPath + strProduceName[nIndex]).c_str(),
NULL, NULL, SW_SHOW);
}
//---------------------------------------------------------------------------

void __fastcall TfmMain::btn3Click(TObject *Sender)
{
char m[256] ; GetClassName(FindWindow(NULL,"卖卡"),m,255);
HWND hForm = FindWindow( NULL,"卖卡");
if(hForm)
AutoFillCode(hForm, strOfficeSn);
else
MessageBox(Handle,String("没有找到需要填写序列号的窗口").c_str() ,"没有找到窗口", MB_OK | MB_ICONWARNING);
}
//---------------------------------------------------------------------------

void __fastcall TfmMain::btn4Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------

void __fastcall TfmMain::btn5Click(TObject *Sender)
{
HWND hForm = FindWindow( NULL,"卖卡");
if(hForm)
{
kk = 0;
HWND hButton = NULL;
EnumChildWindows(hForm, (WNDENUMPROC)EnumChildProc0, (LPARAM)&hButton);
if(hButton != NULL)
{
SendMessage(hButton, BM_CLICK,0,0);
SendMessage(hButton, WM_LBUTTONUP,0,BM_CLICK);
}
BringWindowToTop(hForm);
}
kk = 0;
}
ccrun.com 2007-06-18
  • 打赏
  • 举报
回复
BM_CLICK可以触发按钮的点击事件。不过前提是首先,目标窗口是一个Button,其次,句柄正确吗?
Y___Y 2007-06-18
  • 打赏
  • 举报
回复
BM_CLICK消息绝对可以的
drlqyy 2007-06-18
  • 打赏
  • 举报
回复
试了一下,不行,和以前一样
Y___Y 2007-06-18
  • 打赏
  • 举报
回复
应该是BM_CLICK消息
况且按钮事件是鼠标弹起的时候触发的,再加一句SendMessage(hEdit, WM_LBUTTONUP,MK_LBUTTON,0);或许可以

13,870

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧