在 Delphi 中,您能否更改
同样,我能否更改相同对话框的背景颜色和大小?
ShowMessage
对话框的标题?因为默认情况下它会取我的 exe 名称。同样,我能否更改相同对话框的背景颜色和大小?
ShowMessage
对话框的标题?因为默认情况下它会取我的 exe 名称。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;
当然,您也可以动态地将其他组件插入到该表单中。
Application.Title
的内容作为标题。因此,在调用ShowMessage
之前,您可以设置这个属性。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
的派生类。
MianWindowHandle
,似乎将自定义窗体作为对话框是一个选项。 - Shirish11MessageBox
也是模态的。通过使用ShowModal
方法,TForm的派生类可以设置为模态。我提供的两个选项都可以以模态方式运行。 - David Heffernan这是我写的一段代码,你可能想逐字使用它。
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;
FindComponent
的存在。+1! - David