InputDlg.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <VCLCommon.h>
  5. #include <Windows.hpp>
  6. #include <Consts.hpp>
  7. #include <HistoryComboBox.hpp>
  8. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. //---------------------------------------------------------------------------
  11. TPoint __fastcall GetAveCharSize(TCanvas* Canvas)
  12. {
  13. Integer I;
  14. Char Buffer[52];
  15. TSize Result;
  16. for (I = 0; I <= 25; I++) Buffer[I] = (Char)('A' + I);
  17. for (I = 0; I <= 25; I++) Buffer[I+26] = (Char)('a' + I);
  18. GetTextExtentPoint(Canvas->Handle, Buffer, 52, &Result);
  19. return TPoint(Result.cx / 52, Result.cy);
  20. }
  21. //---------------------------------------------------------------------------
  22. bool __fastcall InputDialog(const AnsiString ACaption,
  23. const AnsiString APrompt, AnsiString & Value, TStrings * History)
  24. {
  25. TForm * Form;
  26. TLabel * Prompt;
  27. TEdit * Edit;
  28. THistoryComboBox * HistoryCombo;
  29. TPoint DialogUnits;
  30. int ButtonTop, ButtonWidth, ButtonHeight;
  31. bool Result = False;
  32. Form = new TForm(Application);
  33. try
  34. {
  35. UseSystemSettings(Form);
  36. Form->Canvas->Font = Form->Font;
  37. DialogUnits = GetAveCharSize(Form->Canvas);
  38. Form->BorderStyle = bsDialog;
  39. Form->Caption = ACaption;
  40. Form->ClientWidth = MulDiv(220, DialogUnits.x, 4);
  41. Form->ClientHeight = MulDiv(63, DialogUnits.y, 8);
  42. Form->Position = poMainFormCenter;
  43. Prompt = new TLabel(Form);
  44. Prompt->Parent = Form;
  45. Prompt->AutoSize = True;
  46. Prompt->Left = MulDiv(8, DialogUnits.x, 4);
  47. Prompt->Top = MulDiv(8, DialogUnits.y, 8);
  48. Prompt->Caption = APrompt;
  49. TWinControl * EditControl;
  50. if (History == NULL)
  51. {
  52. Edit = new TEdit(Form);
  53. Edit->Parent = Form;
  54. Edit->Text = Value;
  55. Edit->SelectAll();
  56. Edit->MaxLength = 255;
  57. EditControl = Edit;
  58. }
  59. else
  60. {
  61. HistoryCombo = new THistoryComboBox(Form);
  62. HistoryCombo->Parent = Form;
  63. HistoryCombo->Text = Value;
  64. HistoryCombo->SelectAll();
  65. HistoryCombo->Items = History;
  66. HistoryCombo->MaxLength = 255;
  67. EditControl = HistoryCombo;
  68. }
  69. EditControl->Left = Prompt->Left;
  70. EditControl->Top = MulDiv(19, DialogUnits.y, 8);
  71. EditControl->Width = MulDiv(204, DialogUnits.x, 4);
  72. Prompt->FocusControl = EditControl;
  73. ButtonTop = MulDiv(41, DialogUnits.y, 8);
  74. ButtonWidth = MulDiv(50, DialogUnits.x, 4);
  75. ButtonHeight = MulDiv(14, DialogUnits.y, 8);
  76. TButton * Button;
  77. Button = new TButton(Form);
  78. Button->Parent = Form;
  79. Button->Caption = Consts_SMsgDlgOK;
  80. Button->ModalResult = mrOk;
  81. Button->Default = True;
  82. Button->SetBounds(MulDiv(58, DialogUnits.x, 4), ButtonTop, ButtonWidth,
  83. ButtonHeight);
  84. Button = new TButton(Form);
  85. Button->Parent = Form;
  86. Button->Caption = Consts_SMsgDlgCancel;
  87. Button->ModalResult = mrCancel;
  88. Button->Cancel = True;
  89. Button->SetBounds(MulDiv(112, DialogUnits.x, 4), ButtonTop, ButtonWidth,
  90. ButtonHeight);
  91. if (Form->ShowModal() == mrOk)
  92. {
  93. if (History != NULL)
  94. {
  95. HistoryCombo->SaveToHistory();
  96. History->Assign(HistoryCombo->Items);
  97. Value = HistoryCombo->Text;
  98. }
  99. else
  100. {
  101. Value = Edit->Text;
  102. }
  103. Result = true;
  104. }
  105. }
  106. __finally
  107. {
  108. delete Form;
  109. }
  110. return Result;
  111. }
  112. //---------------------------------------------------------------------------