我正在尝试学习如何在VCL表单应用程序中传递和处理消息。 我已经挖了一段时间互联网,发现this
假设我有一个进度条我想用消息更新(顺便说两句,如果还有其他更好的方式,我很想听到它) 所以我做了一个简单的项目来测试这些东西,这就是我所拥有的(RECEIVER是带有进度条的表单的名称,SENDER是用于发送消息的按钮,updBar是用于更新进度条的函数,123456是一条消息ID我想用): Unit1.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);
}
Unit1.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
正如您所看到的,我已经为我的消息定义了处理函数和适当的消息处理程序。但是当我通过调试器查看它(在使用按钮发送消息之后)时,执行点似乎永远不会转到我的函数和处理程序行。 提前致谢
答案 0 :(得分:2)
您的代码存在两个问题:
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()
来确保,并记录代码使用的是VCL版本的{{ 1}}而不是其他版本。
试试这个:
Unit1.h:
MESSAGE_HANDLER()
Unit1.cpp:
#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
话虽如此,由于该消息是应用程序的私有消息,因此根本不需要使用#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()
的内部是私有的,因此它应该保留在哪里:
Unit1.h:
TRECEIVER
Unit1.cpp:
#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
如果您希望应用的其他部分向Revceiver发布消息,您可以为此公开一种公共方法:
Unit1.h:
#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);
}
//---------------------------------------------------------------------------
Unit1.cpp:
#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
SomeOtherFile.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);
}
//---------------------------------------------------------------------------