如何响应WM_DESTROY事件?内详(50分)

A

Action

Unregistered / Unconfirmed
GUEST, unregistred user!
test.h
//===================
//---------------------------------------------------------------------------

#ifndef testH
#define testH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class PACKAGE Ttest : public TControl
{
private:
void __fastcall FormDestory(TMessage &Msg);

protected:
public:
__fastcall Ttest(TComponent* Owner);

__published:


BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_DESTROY,TMessage,FormDestory);
END_MESSAGE_MAP(TControl);
};
//---------------------------------------------------------------------------
#endif
//================================================================

test.cpp
//
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "test.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//

static inline void ValidCtrCheck(Ttest *)
{
new Ttest(NULL);
}
//---------------------------------------------------------------------------
void __fastcall Ttest::FormDestory(TMessage &Msg)
{

ShowMessage("Close");

}

__fastcall Ttest::Ttest(TComponent* Owner)
: TControl(Owner)
{
}
//---------------------------------------------------------------------------
namespace Test
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(Ttest)};
RegisterComponents("v.Studio", classes, 0);
}
}
//---------------------------------------------------------------------------
//====================

我想做的是在窗口关闭时显示一个ShowMessage("Close");
但为什么没有响应?是不是我哪儿没做对。。请高手指点
 
if Message.Msg=WM_DESTROY then
 
可不可以换成
if
MessageDlg('You Close',mtInformation,[mbOK,mbCancel],0)=mtYes then
Action:=caFree
else
Action:=caNone;呢?
 
你应该拦截 WM_DESTROY 消息.
如(Delphi5):

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TForm1 = class(TForm)
private
{ Private declarations }
procedure MyDestroyPro(Var Msg :TMessage);message WM_DESTROY;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.MyDestroyPro(Var Msg :TMessage);
begin
MessageBox(0,pChar('Close Now!'),pChar('My Message'),MB_OK+MB_ICONASTERISK);
end;
end.

如果是 C + SDK, 在 WndProc(窗口过程) 中加入拦截 WM_DESTROY 消息的过程;
Swith(iMsg)
{
...
case WM_DESTROY :
.....
return 0;
...
}

 
C++Builder我不是很清楚,
不过从VCL来看,你的代码有两点错误:

(1)TControl是TWinControl/TGraphicControl的父类,
也就是说它可能并没有窗口属性.(至少我没从你的
代码中看到创建窗口的代码!)
既然没有窗口属性,,又如何接收消息呢?

(btw,尤其是那个hwnd,它的正确创建才能使windows
建立线程的消息队列,TWincontrol.create也不会创建hwnd,只有到
CreateWnd里面才能创建!也就是再通过CreateWnd后你的hwnd才能有.
)

(2)假定从主窗口中建立wmdestroy映射,那么你在这个映射中调用
showmessage是难以看到的,是芸花一现(386可能能看到呵呵),虽然
也会被执行,但是却不会停住!因为它创建的窗口是属于Application
对象的,它的消息派发也由application来进行.而application的run
其实就是主窗口的消息的派发过程,在收到wm_destroy后,run会结束.
所以application同时会销毁这个messageForm.

所以,如果你想让你的程序在此处显出dialog,必须用WindowsAPI
::Messagebox(0,"ok","caption",mb_ok)!
 
Action:如果你还要继续讨论请定期提前你的帖子,如果不想继续讨论请结束帖子。
请认真阅读大富翁论坛规则说明 http://www.delphibbs.com/delphibbs/rules.htm
 
如果你是想在CLOSE窗口时来一个的提示的话,那你完全可以在FORM的ONCLOSEQUERY事件
中加入如下代码即可:
if MessageDlg('确实需要退出吗?',mtInformation,[mbOK,mbCancel],0)=mtYes then
Action:=caFree
else
Action:=caNone;
 
接受答案了.
 
顶部