在MDI程序中,主窗口如何监控子窗体的打开与关闭操作?(80分)

  • 主题发起人 主题发起人 driayu
  • 开始时间 开始时间
D

driayu

Unregistered / Unconfirmed
GUEST, unregistred user!
为了实现某功能,需要要子窗口的建立及关闭前执行某些操作,但又不想在每个子窗体的Create及Close事件中写代码,有没有更简单的方法能实现此功能呢?
 
建一个窗体基类,在Create和Close事件中写好操作,然后每一个窗口都继承这个窗体基类。
 
if MDICHILDCOUNT&gt;0 THEN<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ActiveMDIChild.Close ;<br>&nbsp; &nbsp; end;
 
unit &nbsp; Unit1; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; interface &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; uses &nbsp; <br>&nbsp; &nbsp; &nbsp; Windows, &nbsp; Messages, &nbsp; SysUtils, &nbsp; Variants, &nbsp; Classes, &nbsp; Graphics, &nbsp; Controls, &nbsp; Forms, &nbsp; <br>&nbsp; &nbsp; &nbsp; Dialogs, &nbsp; Menus; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; type &nbsp; <br>&nbsp; &nbsp; &nbsp; TForm1 &nbsp; = &nbsp; class(TForm) &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MainMenu1: &nbsp; TMainMenu; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; New1: &nbsp; TMenuItem; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procedure &nbsp; New1Click(Sender: &nbsp; TObject); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procedure &nbsp; FormDestroy(Sender: &nbsp; TObject); &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procedure &nbsp; FormCreate(Sender: &nbsp; TObject); &nbsp; <br>&nbsp; &nbsp; &nbsp; private &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; Private &nbsp; declarations &nbsp; } &nbsp; <br>&nbsp; &nbsp; &nbsp; public &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; Public &nbsp; declarations &nbsp; } &nbsp; <br>&nbsp; &nbsp; &nbsp; end; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; var &nbsp; <br>&nbsp; &nbsp; &nbsp; Form1: &nbsp; TForm1; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; implementation &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; {$R &nbsp; *.dfm} &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; uses &nbsp; Unit2; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; var &nbsp; <br>&nbsp; &nbsp; &nbsp; lpfnClient: &nbsp; Pointer; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; function &nbsp; HookClientProc(hWnd: &nbsp; HWND; &nbsp; uMsg: &nbsp; UINT; &nbsp; wParam: &nbsp; WPARAM; &nbsp; lParam: &nbsp; LPARAM): &nbsp; LRESULT; &nbsp; stdcall; &nbsp; <br>&nbsp; begin &nbsp; <br>&nbsp; &nbsp; &nbsp; if &nbsp; uMsg &nbsp; = &nbsp; WM_MDIDESTROY &nbsp; then &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowMessage('WM_MDIDESTROY'); &nbsp; <br>&nbsp; &nbsp; &nbsp; Result &nbsp; := &nbsp; CallWindowProc(Pointer(lpfnClient), &nbsp; hWnd, &nbsp; uMsg, &nbsp; wParam, &nbsp; lParam) &nbsp; <br>&nbsp; end; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; procedure &nbsp; TForm1.New1Click(Sender: &nbsp; TObject); &nbsp; <br>&nbsp; begin &nbsp; <br>&nbsp; &nbsp; &nbsp; with &nbsp; TForm2.Create(Self) &nbsp; do &nbsp; <br>&nbsp; &nbsp; &nbsp; begin &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Visible &nbsp; := &nbsp; True; &nbsp; <br>&nbsp; &nbsp; &nbsp; end; &nbsp; <br>&nbsp; end; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; procedure &nbsp; TForm1.FormDestroy(Sender: &nbsp; TObject); &nbsp; <br>&nbsp; begin &nbsp; <br>&nbsp; &nbsp; &nbsp; SetWindowLong(Self.ClientHandle, &nbsp; GWL_WNDPROC, &nbsp; Integer(lpfnClient)); &nbsp; <br>&nbsp; end; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; procedure &nbsp; TForm1.FormCreate(Sender: &nbsp; TObject); &nbsp; <br>&nbsp; begin &nbsp; <br>&nbsp; &nbsp; &nbsp; lpfnClient &nbsp; := &nbsp; Pointer(GetWindowLong(Self.ClientHandle, &nbsp; GWL_WNDPROC)); &nbsp; <br>&nbsp; &nbsp; &nbsp; SetWindowLong(Self.ClientHandle, &nbsp; GWL_WNDPROC, &nbsp; LPARAM(@HookClientProc)); &nbsp; <br>&nbsp; end; &nbsp; <br>&nbsp; &nbsp; <br>&nbsp; end.<br>关闭我测试过,绝对可以。打开只要检索uMsg值对应打开值应该也是可以的。
 
可以采用基类的方法实现。<br>如果打开/关闭的代码一致,可以先在基类实现,如果个别不一致,则可以重载实现。<br><br><br>------------------------------------<br>更多问题尽在 http://www.cnhup.net/bbs
 
后退
顶部