继续上节,我们还是用工程文件创建一个MDI窗口。代码如下:
program MDIApp; uses Windows, Messages; var TheMessage:TMsg; FrameWindow,ClientWindow,ChildWindow:HWND; const IDCHILDWND = 100; //第一个MDI子窗口ID {$R *.RES} //回调函数 function FrameWindowProc(TheFrameWindow:HWND;TheMessage,WParam,LParam:LongInt):LongInt;stdcall; var ClientStruct:TClientCreateStruct; begin case TheMessage of WM_CREATE: begin ClientStruct.hWindowMenu:=0; ClientStruct.idFirstChild:=IDCHILDWND; ClientWindow := CreateWindowEx(0, 'MDICLIENT', nil, WS_CHILD or WS_CLIPCHILDREN or WS_VISIBLE, 0, 0, 0, 0, TheFrameWindow, 0, HInstance, @ClientStruct); if ClientWindow = 0 then begin MessageBox(0,'createwindow faild',nil,MB_OK); Exit; end; end; WM_DESTROY: begin PostQuitMessage(0); Exit; end; end; Result := DefFrameProc(TheFrameWindow,ClientWindow,TheMessage,WParam,LParam); end; //注册主窗口 function RegisterFrameClass:Boolean; var WindowClass:TWndClass; begin WindowClass.style:=CS_HREDRAW or CS_VREDRAW; WindowClass.lpfnWndProc:=@FrameWindowProc; WindowClass.cbClsExtra := 0; WindowClass.cbWndExtra := 0; WindowClass.hInstance := HInstance; WindowClass.hIcon := LoadIcon(0,IDI_WINLOGO); WindowClass.hCursor:= LoadCursor(0,IDC_ARROW); WindowClass.hbrBackground:= COLOR_WINDOW; WindowClass.lpszMenuName:= nil; WindowClass.lpszClassName:= 'FrameClass'; Result := windows.RegisterClass(WindowClass)<>0; end; //注册子窗口 function RegisterChildClass: Boolean; var WindowClass:TWndClass; begin WindowClass.style := CS_HREDRAW or CS_VREDRAW; WindowClass.lpfnWndProc:= @DefMDICHildProc; WindowClass.cbClsExtra := 0; WindowClass.cbWndExtra := 0; WindowClass.hInstance := HInstance; WindowClass.hIcon := LoadIcon(0,IDI_APPLICATION); WindowClass.hCursor := LoadCursor(0,IDC_ARROW); WindowClass.hbrBackground := COLOR_WINDOW; WindowClass.lpszMenuName := nil; WindowClass.lpszClassName := 'ChildClass'; Result := windows.RegisterClass(WindowClass)<>0; end; //开始主程序 begin if not RegisterFrameClass then begin MessageBox(0,'RegisterFrameClass faild',nil,MB_OK); Exit; end; FrameWindow := CreateWindowEx(0, 'FrameClass', 'Frame Window', WS_OVERLAPPEDWINDOW or WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, HInstance, nil); if FrameWindow <> 0 then begin ShowWindow(FrameWindow,SW_SHOWNORMAL); UpdateWindow(FrameWindow); end else begin MessageBox(0,'CreateFrameWindow failed',nil,MB_OK); Exit; end; if not RegisterChildClass then begin MessageBox(0,'RegisterChildClass failed',nil,MB_OK); Exit; end; ChildWindow:= CreateMDIWindow('ChildClass', 'Child Window', WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, ClientWindow, HInstance, 0); if ChildWindow <> 0 then begin ShowWindow(ChildWindow,SW_SHOWNORMAL); UpdateWindow(ChildWindow); end else begin MessageBox(0,'CreateChildWindow falid',nil,MB_OK); Exit; end; while GetMessage(TheMessage,0,0,0) do begin TranslateMessage(TheMessage); DispatchMessage(TheMessage); end; end.
效果如下: