unit _myMessageDlg; interface uses Dialogs; function gfniMessageDlg(sMsg, sTitle: WideString; Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn): Word; overload; procedure gpcShowMessage(sMsg: WideString; sTitle: WideString = ''); function gfniMessageBoxYesNo (sMsg: WideString; sTitle: WideString = ''; DefaultButton: TMsgDlgBtn = mbYes): Word; function gfniMessageBoxYesNoCancel(sMsg: WideString; sTitle: WideString = ''; DefaultButton: TMsgDlgBtn = mbYes): Word; overload; implementation uses Windows, Classes, Controls, Forms, StdCtrls, SysUtils, { myFile, myMonitor, myNum, mySize, myWindow } general ; //============================================================================== function gfnMonitorGet(APoint : TPoint) : TMonitor; {2011-09-02: APointのあるモニターを取得 } var li_Index : Integer; begin //念のためモニター情報が変わっていたらモニター情報を再作成しておく gpcMonitorInfoUpdate; li_Index := gfniNumLimit(gfniMonitorIndexGet(APoint), 0, Screen.MonitorCount -1); Result := Screen.Monitors[li_Index]; end; //Rectの変形・移動 function gfnrcRectCenter(rcParent, rcChild: TRect): TRect; {2007-09-26: rcChildがrcParentの真ん中にくるようなRectを返す。 } var li_Left, li_Top, li_Width, li_Height: Integer; begin li_Width := gfniRectWidth (rcChild); li_Height := gfniRectHeight(rcChild); li_Left := rcParent.Left + (gfniRectWidth (rcParent) - li_Width) div 2; li_Top := rcParent.Top + (gfniRectHeight(rcParent) - li_Height) div 2; Result := Rect(li_Left, li_Top, li_Left + li_Width, li_Top + li_Height); end; //------------------------------------------------------------------------------ type TMyMessageDlgEvent = class(TObject) public procedure DoFormShow(Sender: TObject); end; { var F_MessageDlgEvent : TMyMessageDlgEvent; const F_ciDEFAULT_MB_YES = 0; //mbYes: ls_Caption := 'はい(Y)'; F_ciDEFAULT_MB_NO = 1; //mbNo: ls_Caption := 'いいえ(N)'; F_ciDEFAULT_MB_OK = 2; //mbOK: ls_Caption := 'OK'; F_ciDEFAULT_MB_CANCEL = 3; //mbCancel: ls_Caption := 'Cancel'; F_ciDEFAULT_MB_ABORT = 4; //mbAbort: ls_Caption := '中止(A) '; F_ciDEFAULT_MB_RETRY = 5; //mbRetry: ls_Caption := '再試行(R)'; F_ciDEFAULT_MB_IGNORE = 6; //mbIgnore: ls_Caption := '無視(I)'; F_ciDEFAULT_MB_ALL = 7; //mbAll: ls_Caption := 'すべて'; F_ciDEFAULT_MB_NOTOALL = 8; //mbNoToAll: ls_Caption := 'すべてにいいえ'; F_ciDEFAULT_MB_YESTOALL = 9; //mbYesToAll: ls_Caption := 'すべてにはい'; F_ciDEFAULT_MB_HELP = 10; //mbHelp: ls_Caption := 'ヘルプ(H)'; F_ciDEFAULT_MB_CLOSE = 11; //mbClose: ls_Caption := '閉じる(C)'; } procedure TMyMessageDlgEvent.DoFormShow(Sender: TObject); function _ButtonGet(AForm: TForm): HWND; var i : Integer; l_Button : TButton; l_List : TStringList; begin Result := 0; l_List := nil; try l_List := TStringList.Create; for i := 0 to AForm.ComponentCount -1 do begin if (AForm.Components[i] is TButton) then begin l_Button := TButton(AForm.Components[i]); //Leftの順に並べ替えるため4桁の幅の数値にしてしまう l_List.AddObject(Format('%.4d', [l_Button.Left]), TObject(l_Button.Handle)); end; end; l_List.Sort; if (AForm.Tag >= 0) and (AForm.Tag < l_List.Count) then begin Result := HWND(l_List.Objects[AForm.Tag]); end; finally l_List.Free; end; end; var l_Pos : TPoint; l_Monitor : TMonitor; l_Form : TForm; lh_Handle : HWND; lb_Bool : LongBool; l_Cursor : TRect; begin if not(Sender is TForm) then begin Exit; end; l_Form := TForm(Sender); // l_Pos := gfnptMousePosGet; l_Monitor := gfnMonitorGet(l_Pos); SetWindowPos( l_Form.Handle, 0, l_Monitor.Left + ((l_Monitor.Width - l_Form.Width) div 2), l_Monitor.Top + ((l_Monitor.Height - l_Form.Height) div 2), 0, 0, SWP_NOSIZE or SWP_NOZORDER ); //カーソルをOKボタンの上へ SystemParametersInfo(SPI_GETSNAPTODEFBUTTON, 0, @lb_Bool, 0); if (lb_Bool) then begin lh_Handle := _ButtonGet(l_Form); if (lh_Handle <> 0) then begin GetWindowRect(lh_Handle, l_Cursor); l_Cursor := gfnrcRectCenter(l_Cursor, Rect(0,0,0,0)); SetCursorPos(l_Cursor.Left, l_Cursor.Top); end; end; end; function gfniMessageDlg(sMsg, sTitle: WideString; Buttons: TMsgDlgButtons; DefaultButton: TMsgDlgBtn): Word; {2013-12-05: } var i : TMsgDlgBtn; li_Count : Integer; l_Buttons : TMsgDlgButtons; l_MsgForm : TForm; l_FormShow : TMyMessageDlgEvent; begin l_MsgForm := nil; try if (DefaultButton in Buttons) then begin l_MsgForm := CreateMessageDialog(sMsg, mtCustom, Buttons, DefaultButton); l_MsgForm.Tag := -1; li_Count := 0; l_Buttons := []; for i := Low(TMsgDlgBtn) to High(TMsgDlgBtn) do begin if (i in Buttons) then begin if (i = DefaultButton) then begin l_MsgForm.Tag := li_Count; Break; end; Inc(li_Count); end; end; end else begin l_MsgForm := CreateMessageDialog(sMsg, mtCustom, Buttons); l_MsgForm.Tag := -1; end; if (sTitle = '') then begin sTitle := gfnsProductNameGet; end; l_MsgForm.Caption := sTitle; Beep; l_FormShow := nil; try l_FormShow := TMyMessageDlgEvent.Create; l_MsgForm.OnShow := l_FormShow.DoFormShow; Result := l_MsgForm.ShowModal; finally FreeAndNil(l_FormShow); end; finally l_MsgForm.Release; end; end; procedure gpcShowMessage(sMsg: WideString; sTitle: WideString = ''); begin gfniMessageDlg(sMsg, sTitle, [mbOk], mbOk); end; function gfniMessageBoxYesNo(sMsg: WideString; sTitle: WideString = ''; DefaultButton: TMsgDlgBtn = mbYes): Word; begin Result := gfniMessageDlg(sMsg, sTitle, [mbYes, mbNo], DefaultButton); end; function gfniMessageBoxYesNoCancel(sMsg: WideString; sTitle: WideString = ''; DefaultButton: TMsgDlgBtn = mbYes): Word; begin Result := gfniMessageDlg(sMsg, sTitle, [mbYes, mbNo, mbCancel], DefaultButton); end; //------------------------------------------------------------------------------ { initialization F_MessageDlgEvent := TMyMessageDlgEvent.Create; finalization FreeAndNil(F_MessageDlgEvent); } end.