本人所知道两种可以按时间自动关闭的SHOWMESSAGE弹出对话框的方法,现和大家共享一下
1,利用TIMER控件计算时间,FINDWINDOW查找窗体关闭的方法
2,用WINDOWS隐藏API,显示有时间控制功能的SHOWMESSAGE
第一种方法:
1
procedure
TForm1.Button1Click(Sender: TObject);
2 begin
3 Timer1.Enabled : = True;
4 ShowMessage( ' hello world ' );
5 end ;
6
7 procedure TForm1.Timer1Timer(Sender: TObject);
8 var
9 Handle : THandle;
10 begin
11 Handle : = FindWindow( ' TMessageForm ' , PChar(Application.Title)); // 注意FINDWINDOW的第一个参数
12 if Handle > 0 then
13 begin
14 SendMessage(Handle, WM_CLOSE, 0 , 0 );
15 end ;
16 Self.SetFocus;
17 (Sender as TTimer).Enabled : = False; // 倒计时时间为TIMER控件的Interval属性
18 end ;
19
20 这种方法比较灵活,也可以使用在MessageBox或者自定义的窗体,只需要知道FINDWINDOW的两个参数就可以了
2 begin
3 Timer1.Enabled : = True;
4 ShowMessage( ' hello world ' );
5 end ;
6
7 procedure TForm1.Timer1Timer(Sender: TObject);
8 var
9 Handle : THandle;
10 begin
11 Handle : = FindWindow( ' TMessageForm ' , PChar(Application.Title)); // 注意FINDWINDOW的第一个参数
12 if Handle > 0 then
13 begin
14 SendMessage(Handle, WM_CLOSE, 0 , 0 );
15 end ;
16 Self.SetFocus;
17 (Sender as TTimer).Enabled : = False; // 倒计时时间为TIMER控件的Interval属性
18 end ;
19
20 这种方法比较灵活,也可以使用在MessageBox或者自定义的窗体,只需要知道FINDWINDOW的两个参数就可以了
第2种方法
1
function
MessageTimeOut(aCaption, aText : String; Interval : Integer):Boolean;
2 type
3 TMsgBoxTimeOut = function (hWnd:HWND;lpText:PChar;lpCaption:PChar;uType:UINT;wLanguageId:WORD;dwMilliseconds:DWORD): Integer;Stdcall;
4 var
5 LibHandle : THandle;
6 MsgForm : TMsgBoxTimeOut;
7 begin
8 Result : = False;
9 LibHandle : = LoadLibrary( ' user32.dll ' );
10 try
11 @MsgForm : = GetProcAddress(LibHandle, ' MessageBoxTimeoutA ' );
12 if not (@MsgForm = nil ) then
13 begin
14 Result : = True;
15 MsgForm(Application.Handle, PChar(aText), PChar(aCaption), MB_OK, 0 , Interval);//参数1是句柄,参数2是标题,参数3是显示的文字内容
16 end ; //参数4是按钮类型,参数5直接带0,参数6就是结束时间单位是毫秒
17 finally
18 FreeLibrary(LibHandle);
19 end ;
20 end ;
21
22 procedure TForm1.Button2Click(Sender: TObject);
23 begin
24 MessageTimeOut('OK', ' hello world ' , 2000 );
25 end ;
26 这种方法需要调用user32.dll
27
2 type
3 TMsgBoxTimeOut = function (hWnd:HWND;lpText:PChar;lpCaption:PChar;uType:UINT;wLanguageId:WORD;dwMilliseconds:DWORD): Integer;Stdcall;
4 var
5 LibHandle : THandle;
6 MsgForm : TMsgBoxTimeOut;
7 begin
8 Result : = False;
9 LibHandle : = LoadLibrary( ' user32.dll ' );
10 try
11 @MsgForm : = GetProcAddress(LibHandle, ' MessageBoxTimeoutA ' );
12 if not (@MsgForm = nil ) then
13 begin
14 Result : = True;
15 MsgForm(Application.Handle, PChar(aText), PChar(aCaption), MB_OK, 0 , Interval);//参数1是句柄,参数2是标题,参数3是显示的文字内容
16 end ; //参数4是按钮类型,参数5直接带0,参数6就是结束时间单位是毫秒
17 finally
18 FreeLibrary(LibHandle);
19 end ;
20 end ;
21
22 procedure TForm1.Button2Click(Sender: TObject);
23 begin
24 MessageTimeOut('OK', ' hello world ' , 2000 );
25 end ;
26 这种方法需要调用user32.dll
27