读书人

messagebox有关问题

发布时间: 2012-03-17 19:06:28 作者: rapoo

messagebox问题
messagebox(self.Handle, '操作成功! ', '提示 ',MB_OK + MB_ICONASTERISK);

Application.MessageBox( '请设置共享路径! ', '提示 ',MB_OK);
有什么不同吗?

[解决办法]
MessageBox是Windows提供的
Application.MessageBox是Delphi封装的
Application.MessageBox调用了MessageBox函数
[解决办法]
没什么区别,不过application.messagebox不用传递句柄而已
[解决办法]
Application是Form单元的一个全局对象
具体实现是
----------------------
function TApplication.MessageBox(const Text, Caption: PChar; Flags: Longint): Integer;
var
ActiveWindow: HWnd;
WindowList: Pointer;
MBMonitor, AppMonitor: HMonitor;
MonInfo: TMonitorInfo;
Rect: TRect;
FocusState: TFocusState;
begin
ActiveWindow := GetActiveWindow;
MBMonitor := MonitorFromWindow(ActiveWindow, MONITOR_DEFAULTTONEAREST);
AppMonitor := MonitorFromWindow(Handle, MONITOR_DEFAULTTONEAREST);
if MBMonitor <> AppMonitor then
begin
MonInfo.cbSize := Sizeof(TMonitorInfo);
GetMonitorInfo(MBMonitor, @MonInfo);
GetWindowRect(Handle, Rect);
SetWindowPos(Handle, 0,
MonInfo.rcMonitor.Left + ((MonInfo.rcMonitor.Right - MonInfo.rcMonitor.Left) div 2),
MonInfo.rcMonitor.Top + ((MonInfo.rcMonitor.Bottom - MonInfo.rcMonitor.Top) div 2),
0, 0, SWP_NOACTIVATE or SWP_NOREDRAW or SWP_NOSIZE or SWP_NOZORDER);
end;
WindowList := DisableTaskWindows(0);
FocusState := SaveFocusState;
if UseRightToLeftReading then Flags := Flags or MB_RTLREADING;
try
Result := Windows.MessageBox(Handle, Text, Caption, Flags);
finally
if MBMonitor <> AppMonitor then
SetWindowPos(Handle, 0,
Rect.Left + ((Rect.Right - Rect.Left) div 2),
Rect.Top + ((Rect.Bottom - Rect.Top) div 2),
0, 0, SWP_NOACTIVATE or SWP_NOREDRAW or SWP_NOSIZE or SWP_NOZORDER);
EnableTaskWindows(WindowList);
SetActiveWindow(ActiveWindow);
RestoreFocusState(FocusState);
end;
end;
---------------------------------

MessageBox是Windows单元的函数
可以看到Application.MessageBox调用了Windows.MessageBox

编译时要包含相应的单元
比如用到Application.Messagebox就包含Form
用到MessageBox要包含WIndows

读书人网 >.NET

热点推荐