VC中查找窗口并向其发送消息

 

VC中查找窗口并向其发送消息


1、查找窗口句柄的方法:

方法1:FindWindow方法,如下:

[cpp]  view plain  copy
  1. //先找到窗口  
  2. //假设要找窗口的名字为TEST  
  3. //根据窗口名来查找  
  4. CWnd *pWnd = CWnd::FindWindow(NULL,L"TEST");  
  5. if (!pWnd)  
  6. {  
  7.     MessageBox(L"无效窗口")  
  8. }  
  9. /***********还没试验成功**********************/  
  10. //->求救  
  11. //也可以根据窗口类来查找  
  12. //假设要找的窗口类为CTest  
  13. CWnd *pWnd = CWnd::FindWindow(CTest,NULL);  
  14. if (!pWnd)  
  15. {  
  16.     MessageBox(L"无效窗口")  
  17. }  

 

方法2:如果知道窗口是父窗口那么就可以用GetParent()方法获得窗口句柄

 

2、向窗口发送消息

[c-sharp]  view plain  copy
  1. pWnd->SendMessage(UM_USE_MESSAGE, NULL, NULL);  

 

3、消息的定义格式

首先定义UM_USE_MESSAGE

[c-sharp]  view plain  copy
  1. #define UM_USE_MESSAGE  WM_USER+1  

 

在.h文件中声明消息:

[c-sharp]  view plain  copy
  1. afx_msg LRESULT OnUseMessage(WPARAM, LPARAM);  

在.CPP文件中映射消息

[cpp]  view plain  copy
  1. BEGIN_MESSAGE_MAP(CTest, CDialog)  
  2. .......  
  3. ....  
  4. ON_MESSAGE(UM_USE_MESSAGE, &CTest::OnUseMessage)  
  5. .....  
  6. ........  
  7. END_MESSAGE_MAP()  

在.cpp文件中实现消息函数

[cpp]  view plain  copy
  1. LPESULT CTest::OnUseMessage(WPARAM wParam, LPARAM lParam)  
  2. {  
  3.    ............  
  4.    return NULL;  
  5. }  


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值