Delpi在MDI窗口中顯示圖形控件及指定背景的解決方案



    在使用MDI介面時,有時候需要在MDI客戶窗口中顯示一些圖形或軟件封面,使得軟件介面不會顯得空曠,軟件功能也能一目瞭然。然而在Delphi中並沒有直接給出這些接口。在MDI窗體中放入任何圖形控件在運行時都不能顯示。因此需要對MDI窗體進行改造。
申明:
    本方案僅針對MDI窗體,如果應用在非MDI窗體中,後果難說,你自已試試吧。
    記住,窗體的FormStyle屬性要設置爲:fsMDIForm。
解決方案:
    1.    在MDI主窗體中無法接收到MDI客戶窗口的消息(Message),因此,需要自已定義客戶窗口的處理過程(Window Procedure),並接管MDI客戶窗口(需在重載的CreateWnd過程中實現):
        procedure TMDIForm.CreateWnd;
        begin
          inherited;
          FNewWndProc := MakeObjectInstance(ClientWndProc);
          FOldWndProc := Pointer(GetWindowLong(ClientHandle, GWL_WNDPROC));
          SetWindowLong(ClientHandle, GWL_WNDPROC, Longint(FNewWndProc));
        end;
        其中,ClientWndProc爲自定義的窗口過程:    procedure ClientWndProc(var Message: TMessage);
        FOldWndProc用來存放舊的窗口過程的指針。
    2.    實現自已的客戶窗口過程:
        procedure TMDIForm.ClientWndProc(var Message: TMessage);
        var
          R: TRECT;
       
          procedure Default;
          begin
            with Message do
              Result := CallWindowProc(FOldWndProc, ClientHandle, Msg, wParam, lParam);
          end;
        var
          PS: TPaintStruct;
        begin
          R := ClientRect;
          case Message.Msg of
          WM_PAINT:
            begin
              BeginPaint(ClientHandle,PS);
              try
                Canvas.Lock;
                try
                  Canvas.Handle := PS.hdc;
                  try
                    Paint;
                    if ControlCount > 0 then
                      PaintControls(PS.hdc,Controls[0]);
                  finally
                    Canvas.Handle := 0;
                  end;
                finally
                  Canvas.Unlock;
                end;
              finally
                EndPaint(ClientHandle,PS);
              end;
            end;
          WM_ERASEBKGND:
            begin
              DrawBG(TWMEraseBkGnd(Message).DC);
              Message.Result := 1;
            end;
          WM_VSCROLL,WM_HSCROLL:
            begin
              InvalidateRect(ClientHandle,@R,true);
              Default;
            end;
          WM_SIZE:
            begin
              InvalidateRect(ClientHandle,@R,true);
              Default;
            end;
          else
            Default;
          end;
        end;
        上面的DrawBG是用於畫窗口背景的。
    3.    實現窗口背景。
        爲了可以讓繼承者也能定義自已的背景,故此過程說明爲virtual:
          protected
            procedure DrawBG(DC: HDC); virtual;
        在此,DrawBG過程只是簡單的填充窗口背景:
        procedure TMDIForm.DrawBG(DC: HDC);
        begin
          if Brush.Color <> clNone then
            FillRect(DC, ClientRect, Brush.Handle);
        end;
    4.    綜上所述,總結TMDIFrom類定義如下:
          TMDIForm = class(TForm)
          private
            FOldWndProc: TFarProc;
            FNewWndProc: TFarProc;
            procedure ClientWndProc(var Message: TMessage);
          protected
            procedure DrawBG(DC: HDC);virtual;
            procedure CreateWnd; override;
          end;
    5.    經過以上改造後,就可以在DrawBG中畫出指定的背景(需直接調用Windows 的GUI接口),或者直接使用圖形控件,或者實現窗體的OnPaint事件,MDI窗口從此多姿多彩。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章