MFC——对话框间信息传递函数

1.PostMessage
This function places a message in the message queue associated with the thread that created the specified window and then returns without waiting for the thread to process the message. Messages in a message queue are retrieved by calls to the GetMessage or PeekMessage function.
这个函数将一个消息放入窗口的消息队列,然后直接返回,并不等待对应的窗口处理消息。消息队列中的消息是通过调用Windows的GetMessage或PeekMessage函数来获得的。
可以通过Windows的PostMessage函数来访问其它应用程序。

prototype:
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. Two values have special meanings. 
	Msg 
		[in] Specifies the message to be posted. 
	wParam 
		[in] Specifies additional message-specific information. 
	lParam 
		[in] Specifies additional message-specific information. 
	Return Values
		Nonzero indicates success. 
		Zero indicates failure. 
		To get extended error information, call GetLastError.

注意:只能使用wParam或lParam来传递全局指针,否则在堆/栈上的指针很容易被收回而成为非法指针。

2.SendMessage
This function sends the specified message to a window or windows. SendMessage calls the window procedure for the specified window and does not return until the window procedure has processed the message. The PostMessage function, in contrast, posts a message to a thread’s message queue and returns immediately.
这个函数向窗口发送指定的消息。SendMessage成员函数直接调用窗口过程并在窗口过程处理了消息以后才返回。这与PostMessage成员函数形成对比,该函数将消息放入窗口的消息队列并立即返回。

	prototype:
	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 Values
The return value specifies the result of the message processing and depends on the message sent.

3.另外说一下,在多线程中经常会遇到在某一线程中动态申请new了某空间并将返回值赋给指针,但该内存空间还需要使用,需要在另一线程中释放delete该内存空间的情况。这时候new和delete就不能保证在一块出现了,但堆上申请的空间仍然是需要释放的。这时候就需要保证,在delete p时,p指向的空间地址必须是new出来的堆上的空间地址,否则在delete时就会出错,还可能造成内存泄漏。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值