InQuery
Utseende
Visar en meddelanderuta med valbar text på knapparna och ett textfält som användaren anmodas att skriva i.
Denna funktion finns inte färdig i Delphi, utan är men modifierad variant av InputQuery. |
InQuery källkod
[redigera]function InQuery(const ACaption, APrompt: string; var Value: string; btnOK, btnCancel: String): Boolean; function GetAveCharSize(Canvas: TCanvas): TPoint; var I: Integer; Buffer: array[0..51] of Char; begin for I := 0 to 25 do Buffer[I] := Chr(I + Ord('A')); for I := 0 to 25 do Buffer[I + 26] := Chr(I + Ord('a')); GetTextExtentPoint(Canvas.Handle, Buffer, 52, TSize(Result)); Result.X := Result.X div 52; end; var AForm: TForm; Prompt: TLabel; Edit: TEdit; DialogUnits: TPoint; ButtonTop, ButtonWidth, ButtonHeight: Integer; begin Result := False; AForm := TForm.Create(Application); with AForm do try Canvas.Font := Font; DialogUnits := GetAveCharSize(Canvas); BorderStyle := bsDialog; Caption := ACaption; ClientWidth := MulDiv(180, DialogUnits.X, 4); Position := poScreenCenter; Prompt := TLabel.Create(AForm); with Prompt do begin Parent := AForm; Caption := APrompt; Left := MulDiv(8, DialogUnits.X, 4); Top := MulDiv(8, DialogUnits.Y, 8); Constraints.MaxWidth := MulDiv(164, DialogUnits.X, 4); WordWrap := True; end; Edit := TEdit.Create(AForm); with Edit do begin Parent := AForm; Left := Prompt.Left; Top := Prompt.Top + Prompt.Height + 5; Width := MulDiv(164, DialogUnits.X, 4); MaxLength := 255; Text := Value; SelectAll; end; ButtonTop := Edit.Top + Edit.Height + 15; ButtonWidth := MulDiv(50, DialogUnits.X, 4); ButtonHeight := MulDiv(14, DialogUnits.Y, 8); with TButton.Create(AForm) do begin Parent := AForm; Caption := btnOk; ModalResult := mrOk; Default := True; SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth, ButtonHeight); end; with TButton.Create(AForm) do begin Parent := AForm; Caption := btnCancel; ModalResult := mrCancel; Cancel := True; SetBounds(MulDiv(92, DialogUnits.X, 4), Edit.Top + Edit.Height + 15, ButtonWidth, ButtonHeight); AForm.ClientHeight := Top + Height + 13; end; if ShowModal = mrOk then begin Value := Edit.Text; Result := True; end; finally AForm.Free; end; end;
Exempel
[redigera]procedure TForm1.Button1Click(Sender: TObject); var S: string; begin S := 'Sven'; If InQuery('Hallå!','Är detta ditt namn?',S,'Ja','Nej') then ShowMessage('Hej '+S+'!') else ShowMessage('Ursäkta, mitt misstag!'); end;