我正在尝试学习如何在VCL表单应用程序中传递和处理消息。我在网上搜索了一段时间,发现了这个
假设我有一个进度条,我想用消息更新(顺便说一句,如果有其他更好的方法,我很想听(所以我做了一个简单的项目来测试这些东西,下面是我所拥有的(RECEIVER是一个带有进度条的表单的名称,SENDER是一个用于发送消息的按钮,updBar是一个更新进度条的函数,123456是我想使用的消息ID(:单元1.cpp:
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TRECIEVER *RECIEVER;
//---------------------------------------------------------------------------
__fastcall TRECIEVER::TRECIEVER(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TRECIEVER::barUPD(TMessage& msg){
BAR->StepIt();
}
//---------------------------------------------------------------------------
void __fastcall TRECIEVER::SENDERClick(TObject *Sender)
{
//BAR->StepIt();
PostMessage(FindWindow(0,(wchar_t*)"RECIEVER"),123456,0,0);
}
第一单元.h:
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ComCtrls.hpp>
//---------------------------------------------------------------------------
class TRECIEVER : public TForm
{
__published: // IDE-managed Components
TButton *SENDER;
TProgressBar *BAR;
void __fastcall SENDERClick(TObject *Sender);
private: // User declarations
public: // User declarations
void __fastcall barUPD(TMessage& msg);
__fastcall TRECIEVER(TComponent* Owner);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(123456,TMessage,barUPD);
END_MESSAGE_MAP(TForm)
};
//---------------------------------------------------------------------------
extern PACKAGE TRECIEVER *RECIEVER;
//---------------------------------------------------------------------------
#endif
正如您所看到的,我已经为我的消息定义了处理函数和适当的消息处理程序。但是,当我通过调试器查看它时(在使用按钮发送消息之后(,执行点似乎从来没有到达我的函数或处理程序行。提前感谢
您的代码有两个问题:
1( 123456(0x1E240(不是有效的用户级消息ID。0xFFFF以上的值由操作系统保留。自定义消息必须在WM_USER
(0x0400-0x7FFF(、WM_APP
(0x8000-0xBFFF(或RegisterWindowMessage()
(0xC000-0xFFFF(范围内。
2( 您正在向FindWindow()
传递一个错误的字符串指针。您正在将char[]
类型转换为wchar_t*
,这是一个无效的类型转换。要指定字符串文字应使用wchar_t
而不是char
,必须在文字前面加上L
说明符。或者更一般地,当使用任何TCHAR
敏感的API(如FindWindow()
(时,请改用TEXT()
宏。
此外,虽然不是严格意义上的错误,但应该使用VCL_MESSAGE_HANDLER()
而不是MESSAGE_HANDLER()
,因为ATL对MESSAGE_HANDLER()
的定义不同。如果您在项目中不使用ATL,您不会遇到问题,但最好使用VCL_MESSAGE_HANDLER()
来确保绝对正确,并记录代码使用的是MESSAGE_HANDLER()
的VCL版本,而不是其他版本。
试试这个:
第一单元.h:
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ComCtrls.hpp>
//---------------------------------------------------------------------------
#define WM_BAR_STEP_IT (WM_USER+1)
//---------------------------------------------------------------------------
class TRECIEVER : public TForm
{
__published: // IDE-managed Components
TButton *SENDER;
TProgressBar *BAR;
void __fastcall SENDERClick(TObject *Sender);
private: // User declarations
void __fastcall barUPD(TMessage&);
public: // User declarations
__fastcall TRECIEVER(TComponent* Owner);
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_BAR_STEP_IT, TMessage, barUPD);
END_MESSAGE_MAP(TForm)
};
//---------------------------------------------------------------------------
extern PACKAGE TRECIEVER *RECIEVER;
//---------------------------------------------------------------------------
#endif
单元1.cpp:
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TRECIEVER *RECIEVER;
//---------------------------------------------------------------------------
__fastcall TRECIEVER::TRECIEVER(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TRECIEVER::barUPD(TMessage&)
{
BAR->StepIt();
}
//---------------------------------------------------------------------------
void __fastcall TRECIEVER::SENDERClick(TObject *Sender)
{
// this assumes the Form's Caption is set to "RECEIVER"
// also specifying the class type for good measure...
PostMessage(FindWindow(TEXT("TRECEIVER"), TEXT("RECIEVER")), WM_BAR_STEP_IT, 0, 0);
//Alternatively:
//PostMessage(FindWindowW(ClassName().c_str(), Caption.c_str()), WM_BAR_STEP_IT, 0, 0);
}
//---------------------------------------------------------------------------
话虽如此,由于消息对应用程序是私有的,因此根本不需要使用FindWindow()
,而是使用TForm::Handle
属性。我甚至会更进一步,完全去掉MESSAGE_HANDLER()
。该消息对于TRECEIVER
的内部是私有的,因此它应该留在那里:
第一单元.h:
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ComCtrls.hpp>
//---------------------------------------------------------------------------
class TRECIEVER : public TForm
{
__published: // IDE-managed Components
TButton *SENDER;
TProgressBar *BAR;
void __fastcall SENDERClick(TObject *Sender);
private: // User declarations
protected:
void __fastcall WndProc(TMessage& Message);
public: // User declarations
__fastcall TRECIEVER(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TRECIEVER *RECIEVER;
//---------------------------------------------------------------------------
#endif
单元1.cpp:
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TRECIEVER *RECIEVER;
#define WM_BAR_STEP_IT (WM_USER+1)
//---------------------------------------------------------------------------
__fastcall TRECIEVER::TRECIEVER(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TRECIEVER::WndProc(TMessage& Message)
{
if (Message.Msg == WM_BAR_STEP_IT)
BAR->StepIt();
else
TForm::WndProc(Message);
}
//---------------------------------------------------------------------------
void __fastcall TRECIEVER::SENDERClick(TObject *Sender)
{
PostMessage(Handle, WM_BAR_STEP_IT, 0, 0);
}
//---------------------------------------------------------------------------
如果你想让你的应用程序的其他部分向Revceiver发布消息,你可以公开一个公共方法:
第一单元.h:
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ComCtrls.hpp>
//---------------------------------------------------------------------------
class TRECIEVER : public TForm
{
__published: // IDE-managed Components
TButton *SENDER;
TProgressBar *BAR;
void __fastcall SENDERClick(TObject *Sender);
private: // User declarations
protected:
void __fastcall WndProc(TMessage& Message);
public: // User declarations
__fastcall TRECIEVER(TComponent* Owner);
void __fastcall PostBarStepIt();
};
//---------------------------------------------------------------------------
extern PACKAGE TRECIEVER *RECIEVER;
//---------------------------------------------------------------------------
#endif
单元1.cpp:
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TRECIEVER *RECIEVER;
#define WM_BAR_STEP_IT (WM_USER+1)
//---------------------------------------------------------------------------
__fastcall TRECIEVER::TRECIEVER(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TRECIEVER::WndProc(TMessage& Message)
{
if (Message.Msg == WM_BAR_STEP_IT)
BAR->StepIt();
else
TForm::WndProc(Message);
}
//---------------------------------------------------------------------------
void __fastcall TRECIEVER::SENDERClick(TObject *Sender)
{
PostBarStepIt();
}
//---------------------------------------------------------------------------
void __fastcall TRECIEVER::PostBarStepIt()
{
PostMessage(Handle, WM_BAR_STEP_IT, 0, 0);
}
//---------------------------------------------------------------------------
SomeOtherFile.cpp:
#include "Unit1.h"
void __fastcall TSomeOtherClass::SomeMethod()
{
RECIEVER->PostBarStepIt();
}