Egen centraliseret Message(Box,Dlg)
I et andet spørgsmål (http://www.eksperten.dk/spm/710611) er 'kroning' kommet med et glimrnede svar på hvordan man får vist en Message midt på skærmbilledet fremfor midt på skærmen.Jeg har rettet lidt i eksemplet, og har til nedenstående kode (som dog virker) et par spørgsmål:
1) Den Message der fremkommer har disabled [X] (luk) knappen, jeg forsøger at løse dette i toppen af ChangeMessageBoxPosition men uden held!?
2) Fremfor implementation af MessageDlg kunne det være rart at bygge på MessageDlg fremfor MessageBox, kan dette lade sig gøre? Jeg har ikke kunnet 'fange handlen' til denne i ChangeMessageBoxPosition (se evt de 3 udkommenterede linier!)
3) Jeg har forsøgt at gøre MyMessageBox abstract og virtual for at kunne bruge denne function mange steder, det virker selvfølgelig ikke da 'Handel' så ikke kan bruges ... kan dette mon løses!?
Ja det var mange spørgsmål, men der er da også lidt point! (-;
Tak for eventuel hjælp eller afklaring.
Kode:
type
TfMyMessage = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
p_msgCaption: PChar;
procedure ChangeMessageBoxPosition(var Msg: TMessage); message mbMessage;
PUBLIC
function MyMessageBox(Text, Caption: String; Flags: Longint = MB_OK): Integer; virtual;
function MessageDlg(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint): word;
end;
implementation
uses uTreMain;
{$R *.dfm}
function TfMyMessage.MyMessageBox(Text, Caption: String; Flags: Longint = MB_OK): Integer;
begin
PostMessage(Handle, mbMessage, 0, 0);
p_msgCaption:=PChar(Caption);
// p_msgCaption:=PChar('Fejl'); //bare som eksempel, problematik (2)
result:=Application.MessageBox(PChar(Text), p_msgCaption, Flags);
// result:= MessageDlg('Fejl', mtError, [mbOK,mbCancel], 0); //bare som eksempel, problematik (2)
end;
procedure TfMyMessage.ChangeMessageBoxPosition(var Msg: TMessage);
var
skw, skh: integer;
MbHwndSk, MbHwndMes: longword;
MbRectSk, MbRectMes: TRect;
x, y, w, h, w2, h2: integer;
Point : TPoint;
hSysMenu: HMENU;
begin
MbHwndMes:=FindWindow( MAKEINTRESOURCE(WC_DIALOG), p_msgCaption);
if (MbHwndMes<>0) then
begin
//<< forsøger at løse disable problematikken (1)
hSysMenu:= GetSystemMenu(MbHwndMes, false);
if (hSysMenu <> 0) then
begin
EnableMenuItem(hSysMenu, SC_CLOSE, MF_ENABLED);
end;
//>> forsøger at løse disable problematikken (1)
MbHwndSk:= fTreeMain.Handle; //parent form
GetWindowRect(MBHWndSk, MBRectSk);
skw:=MbRectSk.Right-MbRectSk.Left;
skh:=MbRectSk.Bottom-MbRectSk.Top;
GetWindowRect(MBHWndMes, MBRectMes);
w:=MbRectMes.Right-MbRectMes.Left;
h:=MbRectMes.Bottom-MbRectMes.Top;
// center horzontal
x:={Point.X+}((skw-w) div 2);
// keep on screen
if x<0 then
x:=0
else
if x+w>Screen.Width then
x:=Screen.Width-w;
//center vertical
y:={Point.Y+}((skh-h) div 2);
// keep on screen
if y<0 then
y:=0
else
if y+h>Screen.Height then
y:=Screen.Height-h;
// set new windows position
SetWindowPos(MBHWndMes, 0, x, y, 0, 0, SWP_NOACTIVATE or SWP_NOSIZE or SWP_NOZORDER);
end;
end;
function TfMyMessage.MessageDlg(const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint): Word;
var BoxStyle: integer;
begin
case DlgType of
mtWarning: BoxStyle:= MB_ICONWARNING;
mtError: BoxStyle:= MB_ICONERROR;
mtInformation: BoxStyle:= MB_ICONINFORMATION;
mtConfirmation: BoxStyle:= MB_ICONQUESTION;
end;
if ((mbYes in Buttons) and (mbNo in Buttons) and (mbCancel in Buttons)) then
BoxStyle:= BoxStyle + MB_YESNOCANCEL
else if ((mbAbort in Buttons) and (mbRetry in Buttons) and (mbIgnore in Buttons)) then
BoxStyle:= BoxStyle + MB_RETRYCANCEL
else if ((mbYes in Buttons) and (mbNo in Buttons)) then
BoxStyle:= BoxStyle + MB_YESNO
else if ((mbRetry in Buttons) and (mbCancel in Buttons)) then
BoxStyle:= BoxStyle + MB_RETRYCANCEL
else if ((mbOk in Buttons) and (mbCancel in Buttons)) then
BoxStyle:= BoxStyle + MB_OKCANCEL
else if (mbOk in Buttons) then //MB_OK
BoxStyle:= BoxStyle + MB_OK;
MyMessageBox(Msg, 'abc', BoxStyle);
end;
