2011-10-13 36 views

回答

16

您可以使用delphi的CreateMessageDialog函數創建自己的自定義對話框。下面

實施例:

var 
    Dlg: TForm; 
begin 
    Dlg := CreateMessageDialog('message', mtInformation, [mbOk], mbOK); 
    // Treat Dlg like any other form 

    Dlg.Caption := 'Hello World'; 

    try 
    // The message label is named 'message' 
    with TLabel(Dlg.FindComponent('message')) do 
    begin 
     Font.Style := [fsUnderline]; 

     // extraordinary code goes here 
    end; 

    // The icon is named... icon 
    with TPicture(Dlg.FindComponent('icon')) do 
    begin 
     // more amazing code regarding the icon 
    end; 

    Dlg.ShowModal; 
    finally 
    Dlg.Free; 
    end; 

,當然可以插入其他部件藏漢成形式動態。

+1

+1這似乎與Shirish正在尋找的最佳匹配 –

+0

@aldo感謝您的想法 – Shirish11

+0

我從來不知道FindComponent存在。 +1! –

5

該對話框將使用Application.Title的內容作爲標題。所以你可以在致電ShowMessage之前設置它。

但是,如果要顯示具有不同標題的多個對話框,調用Windows MessageBox函數會更方便。當然,如果你有一箇舊版本的Delphi,這會讓你的對話更加原生。

procedure MyShowMessage(const Msg, Caption: string); 
begin 
    MessageBox(GetParentWindowHandleForDialog, PChar(Msg), PChar(Caption), MB_OK); 
end; 

function GetParentWindowHandleForDialog: HWND; 
begin 
    //we must be careful that the handle we use here doesn't get closed while the dialog is showing 
    if Assigned(Screen.ActiveCustomForm) then begin 
    Result := Screen.ActiveCustomForm.Handle; 
    end else if Assigned(Application.MainForm) then begin 
    Result := Application.MainFormHandle; 
    end else begin 
    Result := Application.Handle; 
    end; 
end; 

如果你想控制顏色和大小,然後最明顯的選擇是創建自己的對話框爲TForm後代。

+0

我在BDS 2006找不到'MianWindowHandle' 有一個自定義窗體作爲對話框似乎是一個選項 – Shirish11

+0

我誤解了。它是Delphi中的MainFormHandle。我正在回答太多的WinForms問題! –

+0

但它違反了'Showmessages'的'Showmodal'屬性。 – Shirish11

0

這是我寫的一段代碼,你可能想用它做筆記。

function SetHook(Code : Integer; wparam : Integer; LParam : Integer) : Longint; stdcall; 
function HookWndProc(wnd : HWND ;uMsg : UINT; wParam : WPARAM; lParam : LPARAM) : LRESULT; stdcall; 
var 
    CaptHook : HHOOK; 
    GHookProc : TFNWndProc; 
    GOldHookProc : TFNWndProc; 
implementation 

uses Messages, Types, Graphics; 

    function SetHook(Code : Integer; wparam : Integer; LParam : Integer) : Longint; stdcall; 
var 
    pwp : CWPSTRUCT; 
begin 
if Code = HC_ACTION then 
begin 
    pwp := CWPStruct(Pointer(LParam)^); 
    if pwp.message = WM_INITDIALOG then 
    begin 
    GOldHookProc := TFnWndProc(SetWindowLong(pwp.hwnd, GWL_WNDPROC, LongInt(GHookProc))); 
    end; 
    end; 

result := CallNextHookEx(CaptHook, Code, wparam, lparam); 

end; 

function HookWndProc(wnd : HWND ;uMsg : UINT; wParam : WPARAM; lParam : LPARAM) : LRESULT; 
var 
    DC : HDC; 
    WndRect : Trect; 
    BR: HBRUSH; 
    WndText : array[1..20] of char; 
begin 

result := CallWindowProc(GOldHookProc, wnd, uMsg, wParam, lParam); 
if uMsg = WM_ERASEBKGND then 
begin 
    GetWindowText(wnd, @wndText, 20); 

    //do stuff here (I colored the button red) 
    DC := GetDC(wnd); 
    WndRect := Rect(0, 0, 200,200); 
    BR := CreateSolidBrush(clRed); 
    FillRect(dc, WndRect, BR); 
    DeleteObject(BR); 
    ReleaseDC(wnd, dc); 
end; 
end; 

...

把這個在你的窗體創建要做出時髦的消息框

uses windows; 

...

CaptHook := SetWindowsHookEx(WH_CALLWNDPROC, @SetHook, 0, GetCurrentThreadId); 
GHookProc := @HookWndProc; 

那麼,這是什麼不會掛鉤到Windows的對話框彈出功能中,你可以獲取對話框的上下文並繪製它。

+0

這是幹什麼的? –

+0

@David我不得不爲此編寫內部應用程序,因爲這裏的一些技術人員想要在重要警告消息上使用紅色按鈕。它本質上是一個可以在彈出的對話框的「畫布」上繪製的鉤子。你可以在評論的地方畫畫。 –