我在BCB中实现消息处理的简单代码--XJQ2003
2008-11-05 15:33
411 查看
一-//初始化
void INITMessage(HWND pWnd,UINT uMsgID)
{
pWnd= Form1->Handle;
uMsgID=WM_FILEREADY;
pWnd=FindWindow("TForm1","Form1");//这个好象可以不要
if(pWnd==NULL)
return;
}
二、//接受和处理消息
1,首先在头文件里定义个函数
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormCreate(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall AppMessage(tagMSG &Msg, bool &Handled);//就是这个函数
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
2,在ONCREAT事件中加上
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Application->OnMessage = AppMessage;
}
3,在AppMessage处理消息
void __fastcall TForm1::AppMessage(tagMSG &Msg, bool &Handled)
{
String ss;
if (Msg.message == WM_FILEREADY)
{
ss="收到消息:"+IntToStr(Msg.wParam);
Memo1->Lines->Add(ss);
Handled = true;
}
/* for all other messages, Handled remains False so that other message handlers can respond */
}
三、消息发送
void SendMyMsg(unsigned int wParam)
{
HWND MypWnd;
UINT MyMsgID;
//------消息句柄有效判断---------
MypWnd=pWnd;
MyMsgID=uMsgID;
if(pWnd==NULL) return -3;
if(uMsgID<=1024)return -4;
try
{
PostMessage(MypWnd,MyMsgID,wParam,0);
}
catch(...)
{
}
}
/*
BOOL PostMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to post
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
Parameters
hWnd
Identifies the window whose window procedure is to receive the message. Two values have special meanings:
Value Meaning
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
Specifies the message to be posted.
wParam
Specifies additional message-specific information.
lParam
Specifies additional message-specific information.
Return Values
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.
*/
正个过程比较简单,因为我实现的功能也比较少,就是简单的发送消息和接受处理消息数据。要感谢网友的帮助,使我更好更轻松的工作,学习更多的知识。
如果要截获所有的键盘消息,可以这样:
void __fastcall TForm1::AppMessage(tagMSG &Msg, bool &Handled)
{
if(Msg.Message == WM_KEYDOWN)
{
switch(Msg.CharCode)
{
case VK_F1:
break;
default:
break;
}
}
Handle = false;
}
然后在Form的OnCreate事件中写如下代码:
Application->OnMessage = AppMessage;
这样就能收到键盘的消息了,case后面跟的就是按键的虚拟表的其中一个,它代表你按下了F1键,其余的自己查帮助好了
void INITMessage(HWND pWnd,UINT uMsgID)
{
pWnd= Form1->Handle;
uMsgID=WM_FILEREADY;
pWnd=FindWindow("TForm1","Form1");//这个好象可以不要
if(pWnd==NULL)
return;
}
二、//接受和处理消息
1,首先在头文件里定义个函数
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormCreate(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall AppMessage(tagMSG &Msg, bool &Handled);//就是这个函数
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
2,在ONCREAT事件中加上
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Application->OnMessage = AppMessage;
}
3,在AppMessage处理消息
void __fastcall TForm1::AppMessage(tagMSG &Msg, bool &Handled)
{
String ss;
if (Msg.message == WM_FILEREADY)
{
ss="收到消息:"+IntToStr(Msg.wParam);
Memo1->Lines->Add(ss);
Handled = true;
}
/* for all other messages, Handled remains False so that other message handlers can respond */
}
三、消息发送
void SendMyMsg(unsigned int wParam)
{
HWND MypWnd;
UINT MyMsgID;
//------消息句柄有效判断---------
MypWnd=pWnd;
MyMsgID=uMsgID;
if(pWnd==NULL) return -3;
if(uMsgID<=1024)return -4;
try
{
PostMessage(MypWnd,MyMsgID,wParam,0);
}
catch(...)
{
}
}
/*
BOOL PostMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to post
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
Parameters
hWnd
Identifies the window whose window procedure is to receive the message. Two values have special meanings:
Value Meaning
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
Specifies the message to be posted.
wParam
Specifies additional message-specific information.
lParam
Specifies additional message-specific information.
Return Values
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.
*/
正个过程比较简单,因为我实现的功能也比较少,就是简单的发送消息和接受处理消息数据。要感谢网友的帮助,使我更好更轻松的工作,学习更多的知识。
如果要截获所有的键盘消息,可以这样:
void __fastcall TForm1::AppMessage(tagMSG &Msg, bool &Handled)
{
if(Msg.Message == WM_KEYDOWN)
{
switch(Msg.CharCode)
{
case VK_F1:
break;
default:
break;
}
}
Handle = false;
}
然后在Form的OnCreate事件中写如下代码:
Application->OnMessage = AppMessage;
这样就能收到键盘的消息了,case后面跟的就是按键的虚拟表的其中一个,它代表你按下了F1键,其余的自己查帮助好了
相关文章推荐
- 哈希表的【构造方法】【冲突处理方法】及【哈希拉链法的简单代码实现】
- PHP中利用redis实现消息队列处理高并发请求--简洁代码实现效果
- 视频二值化处理基本方法以及简单代码实现
- Android编程基础之消息处理机制的简单实现
- C#实现数据库事务处理的简单示例代码
- java简单实现八叉树图像处理代码示例
- C#实现数据库事务处理的简单示例代码
- ZooKeeper应用场景之消息发布订阅的简单代码实现
- ASP.NET 定制简单的错误处理页面实现代码
- 简单的中文信息处理C#实现代码片段
- 简单消息处理的实现--观察者模式应用
- Java消息队列的简单实现代码
- ROS学习笔记(2):在ROS中使用OpenCV进行简单的图像处理---代码实现篇
- spring boot整合spring-kafka实现发送接收消息实例代码
- CERTI之RTIG消息处理模块实现细节的举例说明(一)
- c++ 与C# 单例代码简单实现
- PHP ORM框架与简单代码实现(转)
- live555代码解读之三:SETUP和PLAY请求消息处理过程