首定定义两个消息常量
const
WM_ChildFormClosed = WM_USER + $0001;
WM_ActiveChildFormChanged = WM_USER + $0002;
然后在主窗体中定义两个消息处理过程
procedure ChildFormClosed(var msg: TMessage); message WM_ChildFormClosed;
procedure ActiveChildFormChanged(var msg: TMessage); message WM_ActiveChildFormChanged;
procedure TfrmMain.ChildFormClosed(var msg: TMessage);
var
MenuItem: TMenuItem;
begin
if msg.Msg = WM_ChildFormClosed then begin
MenuItem := MainMenu1.Items.Find(String(msg.WParam));
MainMenu1.Items.Delete(MainMenu1.Items.IndexOf(MenuItem));
end;
end;
procedure TfrmMain.ActiveChildFormChanged(var msg: TMessage);
var
MenuItem: TMenuItem;
begin
if msg.Msg = WM_ActiveChildFormChanged then begin
MenuItem := MainMenu1.Items.Find(String(msg.WParam));
MenuItem.Checked := True;
end;
end;
//===========================
子窗体中加入以下代码
procedure TfrmMDIChildBase.FormClose(Sender: TObject; var Action: TCloseAction);
var
s: String;
begin
s := Self.Caption;
PostMessage(MainFormHandle, WM_ChildFormClosed, Integer(s), 0);
Application.ProcessMessages;
Action := caFree;
end;
procedure TfrmMDIChildBase.FormActivate(Sender: TObject);
var
s: String;
begin
s := Self.Caption;
PostMessage(MainFormHandle, WM_ActiveChildFormChanged, Integer(s), 0);
Application.ProcessMessages;
end;