如何触发一个按钮的单击事件

shone_sun 2006-10-17 07:33:50

写的程序中可以获得另外一个窗体的句柄,
而且可以获得此窗体上一个按钮的句柄

要如何操作
才能触发这个按钮的单击事件
效果相当于有鼠标点击了这个按钮

急等

另,我这里只有窗体和按钮的句柄,没有按钮的ID
...全文
629 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
shone_sun 2006-10-23
  • 打赏
  • 举报
回复
看了所有的回帖

我用postmessage解决了
好像所有回帖里的postmessage都不对
放出我的方法,大家讨论

PostMessage(按钮所在窗体句柄,WM_COMMAND,MAKELONG(GetDlgCtrlID(按钮句柄),BN_CLICKED),(LONG)按钮句柄);

Jokar 2006-10-23
  • 打赏
  • 举报
回复
PostMessage(按钮所在窗体句柄,WM_COMMAND,MAKELONG(GetDlgCtrlID(按钮句柄),BN_CLICKED),(LONG)按钮句柄);
--------------------
有了窗口ID~当然好说了~ 谁让 BN_CLICKED 是 notification消息呢~ ID是必须的~
Jokar 2006-10-23
  • 打赏
  • 举报
回复
to lz
偶的不用 消息机制:呵呵

CYourWnd *pWnd = (CYourWnd*)CWnd::FromHandlePermanent(<窗体句柄>);
pWnd->OnBtnClicked(....); // 这里直接调用你的处理函数~hehe(名字可能不一样)

ps:以为不能用 窗口ID呢~
zoucongjie 2006-10-19
  • 打赏
  • 举报
回复

SendMessage(hWnd, WM_LBUTTONUP, 0, 0);

csShooter 2006-10-19
  • 打赏
  • 举报
回复

SendMessage(hWnd, WM_LBUTTONDOWN, 0, 0);
--------------------------------------

hWnd = FindWindows(...);
飞哥 2006-10-19
  • 打赏
  • 举报
回复
SendMessage Function

--------------------------------------------------------------------------------

The SendMessage function sends the specified message to a window or windows. It calls the window procedure for the specified window and does not return until the window procedure has processed the message.

To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread's message queue and return immediately, use the PostMessage or PostThreadMessage function.


Syntax

LRESULT SendMessage( HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
);
Parameters

hWnd
[in] Handle to the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
Msg
[in] Specifies the message to be sent.
wParam
[in] Specifies additional message-specific information.
lParam
[in] Specifies additional message-specific information.
Return Value

The return value specifies the result of the message processing; it depends on the message sent.


Remarks

Applications that need to communicate using HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for inter-application communication.

The system only does marshalling for system messages (those in the range 0 to WM_USER). To send other messages (those above WM_USER) to another process, you must do custom marshalling.

If the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. If the specified window was created by a different thread, the system switches to that thread and calls the appropriate window procedure. Messages sent between threads are processed only when the receiving thread executes message retrieval code. The sending thread is blocked until the receiving thread processes the message. However, the sending thread will process incoming nonqueued messages while waiting for its message to be processed. To prevent this, use SendMessageTimeout with SMTO_BLOCK set. For more information on nonqueued messages, see Nonqueued Messages.

Windows 95/98/Me: SendMessageW is supported by the Microsoft® Layer for Unicode (MSLU). To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

Example

For an example, see Processing Keyboard Input.

Function Information

Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows 95, Windows NT 3.1
Unicode Implemented as Unicode and ANSI versions on Windows NT, Windows 2000, Windows XP

飞哥 2006-10-19
  • 打赏
  • 举报
回复
PostMessage Function

--------------------------------------------------------------------------------

The PostMessage function places (posts) a message in the message queue associated with the thread that created the specified window and returns without waiting for the thread to process the message.

To post a message in the message queue associate with a thread, use the PostThreadMessage function.


Syntax

BOOL PostMessage( HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
);
Parameters

hWnd
[in] Handle to the window whose window procedure is to receive the message. The following values have special meanings.
HWND_BROADCAST
The message is posted to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows. The message is not posted to child windows.
NULL
The function behaves like a call to PostThreadMessage with the dwThreadId parameter set to the identifier of the current thread.
Msg
[in] Specifies the message to be posted.
wParam
[in] Specifies additional message-specific information.
lParam
[in] Specifies additional message-specific information.
Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.




Remarks

Messages in a message queue are retrieved by calls to the GetMessage or PeekMessage function.

Applications that need to communicate using HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for inter-application communication.

The system only does marshalling for system messages (those in the range 0 to WM_USER). To send other messages (those above WM_USER) to another process, you must do custom marshalling.

If you send a message in the range below WM_USER to the asynchronous message functions (PostMessage, SendNotifyMessage, and SendMessageCallback), its message parameters cannot include pointers. Otherwise, the operation will fail. The functions will return before the receiving thread has had a chance to process the message and the sender will free the memory before it is used.

Do not post the WM_QUIT message using PostMessage; use the PostQuitMessage function.

Windows 2000/XP: There is a limit of 10,000 posted messages per message queue. This limit should be sufficiently large. If your application exceeds the limit, it should be redesigned to avoid consuming so many system resources. To adjust this limit, modify the following registry key:

Function Information

Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows 95, Windows NT 3.1
Unicode Implemented as Unicode and ANSI versions on Windows NT, Windows 2000, Windows XP
MagicianLiu 2006-10-18
  • 打赏
  • 举报
回复
ceshi
Jokar 2006-10-18
  • 打赏
  • 举报
回复
又看了看~ 没有id好像不好办~ 反正使用消息影射是不灵了(对于BN_CLICKED来说)

干脆:
CYourWnd *pWnd = (CYourWnd*)CWnd::FromHandlePermanent(<窗体句柄>);
pWnd->OnBtnClicked(....); // 这里直接调用你的处理函数~hehe(名字可能不一样)

leehq 2006-10-18
  • 打赏
  • 举报
回复
SendMessage( <按钮句柄>, BM_CLICK, 0, 0 );
飞哥 2006-10-17
  • 打赏
  • 举报
回复
呵呵
PostMessage
jixingzhong 2006-10-17
  • 打赏
  • 举报
回复
SendMessage
或者是
PostMessage
Jokar 2006-10-17
  • 打赏
  • 举报
回复
SendMessage(hWnd, WM_LBUTTONUP, 0, 0);
Jokar 2006-10-17
  • 打赏
  • 举报
回复
SendMessage(hWnd, WM_LBUTTONDOWN, 0, 0);
doudouHuY 2006-10-17
  • 打赏
  • 举报
回复
在窗体消息处理函数中捕获对应消息,进行处理

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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