我發現用戶無意中點擊了主表單的系統選單“X”并關閉了程式。
為了緩解這個問題,我添加了代碼來詢問用戶是否確定要關閉程式。這樣做解決了人們在進行高壓力事件登記時不得不尋求幫助重新啟動該程式的問題。該計劃一路獲勝。
但是,每當我在主表單的 OnCloseQuery 事件中使用 MessageDlg 函式時,就會發出煩人的叮聲。
使用 Delphi 10.4 專業版
File->New->"Windows VCL Application - Delphi" 在OnCloseQuery事件中放置如下代碼
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
iOkToClose: integer;
begin
iOkToClose
:= MessageDlg('Do you wish to close the DertSQL program?',
mtConfirmation, [mbYes, mbNo], 0, mbNo);
if iOkToClose <> mrYes then
CanClose := FALSE
else CanClose := TRUE;
end;
編譯運行程式然后戳系統選單“X”
問:如何去掉MessageDlg函式引起的叮叮聲?
uj5u.com熱心網友回復:
首先,讓我們注意你的代碼
if iOkToClose <> mrYes then
CanClose := False
else
CanClose := True
寫成更好
CanClose := iOkToClose = mrYes
事實上,您甚至不需要變數。您的整個事件處理程式可以簡單地撰寫
CanClose :=
MessageDlg(
'Do you wish to close the DertSQL program?',
mtConfirmation, [mbYes, mbNo], 0, mbNo
) = mrYes;
現在,您聽到的蜂鳴聲與圖示相關聯,因此消除蜂鳴聲的一種方法是移除圖示:
CanClose :=
MessageDlg(
'Do you wish to close the DertSQL program?',
TMsgDlgType.mtCustom, [mbYes, mbNo], 0, mbNo
) = mrYes;
此外,在 VCL 中顯示訊息框的方法有很多種。
您目前正在使用MessageDlg我個人不太喜歡的功能。
一種替代方法是標準的 Win32MessageBox函式:
CanClose :=
MessageBox(
Handle,
'Do you want to close this application?',
'My App',
MB_ICONQUESTION or MB_YESNO
) = ID_YES;
這里沒有嗶聲。
但使用任務對話框更好:
var dlg := TTaskDialog.Create(Self);
try
dlg.Caption := 'My App';
dlg.Title := 'Do you want to exit the application?';
dlg.MainIcon := tdiNone;
dlg.CommonButtons := [tcbYes, tcbNo];
CanClose := dlg.Execute and (dlg.ModalResult = mrYes);
finally
dlg.Free;
end;
這特別好,因為您可以按照 Win32 UI 指南使用藍色的主要說明進行操作:

但是正如您所看到的,這是很多代碼。您可能想根據任務對話框制作自己的訊息對話框功能,就像我在任務對話框訊息框中所做的那樣:
CanClose := TD('Do you want to exit the application?')
.YesNo
.Execute(Self) = mrYes;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/536571.html
標籤:德尔福留言箱
下一篇:使用查找表計算CRC-6GSM
