Einzelnen Beitrag anzeigen

NicoDE
(Gast)

n/a Beiträge
 

Re: [Artikel] Den ExitCode eines Dialogfensters nutzen

  Alt 4. Mai 2006, 19:17
Hier das angekündigte Beispiel (schnell runtergeschrieben, statt eines Ressourcenskriptes habe ich DialogBoxIndirect verwendet (einfach um das Beispiel auf den Quellcode zu beschränken))
Delphi-Quellcode:
unit FooDlg;

interface

{$MINENUMSIZE 4}

uses
  Windows;

type
{$IFDEF UNICODE}
  TextString = System.WideString;
{$ELSE !UNICODE}
  TextString = System.AnsiString;
{$ENDIF UNICODE}

type
  TFooModalResult = (
    foomrNone,
    foomrOk,
    foomrCancel
  );

type
  TFooDialogParam = record
    Parent: HWND; // in
    Text : TextString; // in/out
  end;

type
  TFooDialog = class
  private
    FText: TextString;
  public
    class function ShowModal(var AParam: TFooDialogParam): TFooModalResult;
  end;

implementation

{$EXTENDEDSYNTAX ON}

uses
  Messages;

const
  FooDialogTemplate: packed record
    Dialog: packed record
      Header : TDlgTemplate;
      Menu : array [0..0] of WideChar;
      WndClass: array [0..0] of WideChar;
      Title : array [0..3] of WideChar;
      Points : Word;
      Weight : Word;
      Italic : Byte;
      Charset : Byte;
      FontFace: array [0..12] of WideChar;
    end;
    __align0: Word;
    Edit: packed record
      Header : TDlgItemTemplate;
      WndClass : array [0..1] of WideChar;
      Title : array [0..0] of WideChar;
      ExtraCount: Word;
    end;
    __align1: Word;
    ButtonOk: packed record
      Header : TDlgItemTemplate;
      WndClass : array [0..1] of WideChar;
      Title : array [0..3] of WideChar;
      ExtraCount: Word;
    end;
    ButtonCancel: packed record
      Header : TDlgItemTemplate;
      WndClass : array [0..1] of WideChar;
      Title : array [0..7] of WideChar;
      ExtraCount: Word;
    end;
  end = (
    Dialog: (
      Header: (
        style : WS_VISIBLE or WS_POPUP or WS_CAPTION or WS_SYSMENU or
                         DS_MODALFRAME or DS_SETFONT or DS_CENTER;
        dwExtendedStyle: 0;
        cdit : 3;
        x : 0;
        y : 0;
        cx : 4 + 100 + 4;
        cy : 4 + 60 + 2 + 12 + 4
      );
      Menu : (#0);
      WndClass: (#0);
      Title : ('F', 'o', 'o', #0);
      Points : 8;
      Weight : FW_NORMAL;
      Italic : Ord(False);
      Charset : DEFAULT_CHARSET;
      FontFace: ('M', 'S', ' ', 'S', 'h', 'e', 'l', 'l', ' ', 'D', 'l', 'g', #0)
    );
    Edit: (
      Header: (
        style : WS_VISIBLE or WS_CHILD or WS_TABSTOP or
                         ES_MULTILINE or ES_WANTRETURN or
                         WS_VSCROLL or WS_HSCROLL or
                         ES_AUTOVSCROLL or ES_AUTOHSCROLL;
        dwExtendedStyle: 0;
        x : 4;
        y : 4;
        cx : 100;
        cy : 60;
        id : 100
      );
      WndClass : (#$FFFF, #$0081);
      Title : (#0);
      ExtraCount: 0
    );
    ButtonOk: (
      Header: (
        style : WS_VISIBLE or WS_CHILD or WS_TABSTOP or
                         BS_DEFPUSHBUTTON;
        dwExtendedStyle: 0;
        x : 4 + 100 - 38 - 2 - 38;
        y : 4 + 60 + 2;
        cx : 38;
        cy : 12;
        id : ID_OK;
      );
      WndClass : (#$FFFF, #$0080);
      Title : ('&', 'O', 'K', #0);
      ExtraCount: 0
    );
    ButtonCancel: (
      Header: (
        style : WS_VISIBLE or WS_CHILD or WS_TABSTOP or
                         BS_PUSHBUTTON;
        dwExtendedStyle: 0;
        x : 4 + 100 - 38;
        y : 4 + 60 + 2;
        cx : 38;
        cy : 12;
        id : ID_CANCEL;
      );
      WndClass : (#$FFFF, #$0080);
      Title : ('&', 'C', 'a', 'n', 'c', 'e', 'l', #0);
      ExtraCount: 0;
    )
  );

function FooDialogProc(ADlg: HWND; AMsg: UINT; WParam: WPARAM; LParam: LPARAM): Integer stdcall;
const
  PropName = 'Instance';
var
  PropInst: TFooDialog;
  EditText: HWND;
begin
  Result := Ord(True);
  case AMsg of
    WM_INITDIALOG:
      begin
        PropInst := TFooDialog(LParam);
        SetProp(ADlg, PropName, THandle(PropInst));
        if Assigned(PropInst) then
        begin
          EditText := GetDlgItem(ADlg, FooDialogTemplate.Edit.Header.id);
          if EditText <> 0 then
            SetWindowText(EditText, PChar(PropInst.FText));
        end;
      end;
    WM_NCDESTROY:
      RemoveProp(ADlg, PropName);
    WM_CLOSE:
      EndDialog(ADlg, Integer(foomrNone));
    WM_COMMAND:
      case HiWord(WParam) of
        BN_CLICKED:
          begin
            case LoWord(WParam) of
              ID_OK:
                begin
                  PropInst := TFooDialog(GetProp(ADlg, PropName));
                  if Assigned(PropInst) then
                  begin
                    EditText := GetDlgItem(ADlg, FooDialogTemplate.Edit.Header.id);
                    if EditText <> 0 then
                    begin
                      SetLength(PropInst.FText,
                        GetWindowTextLength(EditText) + 1);
                      SetLength(PropInst.FText,
                        GetWindowText(EditText, PChar(PropInst.FText),
                        Length(PropInst.FText)));
                    end;
                  end;
                end;
            end;
            EndDialog(ADlg, LoWord(WParam));
          end;
      else
        Result := Ord(False);
      end;
  else
    Result := Ord(False);
  end;
end;

class function TFooDialog.ShowModal(var AParam: TFooDialogParam): TFooModalResult;
var
  FooDialog: TFooDialog;
begin
  FooDialog := TFooDialog.Create();
  try
    FooDialog.FText := AParam.Text;
    Result := TFooModalResult(
      DialogBoxIndirectParam(HInstance, FooDialogTemplate.Dialog.Header,
        AParam.Parent, TFNDlgProc(@FooDialogProc), Integer(FooDialog)));
    AParam.Text := FooDialog.FText;
  finally
    FooDialog.Free();
  end;
end;

end.
Der Aufruf sieht dann etwa so aus:
Delphi-Quellcode:
var
  Param: TFooDialogParam;
begin
  Param.Parent := ParentHandle;
  Param.Text := 'Foo'#13#10'Bar';
  case TFooDialog.ShowModal(Param) of
    foomrOk:
      MessageBox(Param.Parent, PChar(Param.Text), '[OK]', MB_OK);
    foomrCancel:
      MessageBox(Param.Parent, PChar(Param.Text), '[Cancel]', MB_OK);
  else
    MessageBox(Param.Parent, PChar(Param.Text), 'Dialog closed', MB_OK);
  end;
end;
  Mit Zitat antworten Zitat