Reputation: 43
I'm trying to make a custom messagedlg in borland c++ builder. But the code bellow instead to change button captions, creates new buttons on left upper corner of dialog form.
#include <vcl.h>
#pragma hdrstop
#include "frmMsgDlg.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
int __fastcall MsgDlg( const String Msg, TMsgDlgType DlgType, TMsgDlgButtons Buttons, const String* Captions, int Captions_maxidx )
{
int m = 0;
TButton *DlgButton;
int CaptionIndex = 0;
TForm* aMsgDlg = CreateMessageDialog( Msg, DlgType, Buttons );
aMsgDlg->Color = TColor(clWindow);
for ( m = 0; m <= aMsgDlg->ComponentCount - 1; m++)
{
if ( dynamic_cast< TButton *>( aMsgDlg->Components[m] ) )
{
DlgButton = new TButton( aMsgDlg->Components[m] );
DlgButton->Parent = aMsgDlg;
if ( CaptionIndex > Captions_maxidx /*# High(Captions) */ ) break;
DlgButton->WordWrap = true;
DlgButton->Caption = Captions[CaptionIndex];
DlgButton->Width = 56;
DlgButton->Height= 28;
DlgButton->Cancel = false;
CaptionIndex++;
}
}
return aMsgDlg->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
int msg;
String capt[2] = {"PO","JO"};
msg = MsgDlg("Tre tentime dështuan, ju lutem startoni programin nga ikona!!!", mtInformation,TMsgDlgButtons() << mbOK <<mbCancel,capt,2);
}
//---------------------------------------------------------------------------
Upvotes: 1
Views: 3131
Reputation: 26
I have killed a bit of time to expand on your original message dialog button modifier. You can just cut an paste this all and it will run.
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "frmMsgDlg.h"
#include <memory>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
int MsgDlg( const String Msg, TMsgDlgType DlgType, TMsgDlgButtons Buttons, TStringList* Captions);
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
int MsgDlg( const String Msg, TMsgDlgType DlgType, TMsgDlgButtons Buttons, TStringList* Captions)
{
TForm* aMsgDlg = CreateMessageDialog(Msg, DlgType, Buttons);
int i, j;
TButton *DlgButton;
aMsgDlg->Color = clWindow;
for (i = 0; i <= aMsgDlg->ComponentCount - 1; i++) {
DlgButton = dynamic_cast<TButton*>(aMsgDlg->Components[i]);
if (DlgButton) {
switch (DlgButton->ModalResult) {
case mrOk:
j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbOK));
break;
case mrCancel:
j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbCancel));
break;
case mrAbort:
j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbAbort));
break;
case mrRetry:
j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbRetry));
break;
case mrIgnore:
j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbIgnore));
break;
case mrYes:
j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbYes));
break;
case mrNo:
j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbNo));
break;
case mrAll:
j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbAll));
break;
case mrNoToAll:
j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbNoToAll));
break;
case mrYesToAll:
j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbYesToAll));
break;
case mrClose:
j = Captions->IndexOfObject(reinterpret_cast<TObject *>(mbClose));
break;
default:
j = -1;
}
if (j != -1) {
DlgButton->WordWrap = true;
DlgButton->Caption = Captions->Strings[j];
DlgButton->Width = 56;
DlgButton->Height = 28;
DlgButton->Cancel = false;
}
}
}
return aMsgDlg->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
int rc;
std::auto_ptr<TStringList>capt(new TStringList());
capt->AddObject("mbYes", reinterpret_cast<TObject *>(mbYes));
capt->AddObject("mbNo", reinterpret_cast<TObject *>(mbNo));
capt->AddObject("mbOK", reinterpret_cast<TObject *>(mbOK));
capt->AddObject("mbCancel", reinterpret_cast<TObject *>(mbCancel));
capt->AddObject("mbAbort", reinterpret_cast<TObject *>(mbAbort));
capt->AddObject("mbRetry", reinterpret_cast<TObject *>(mbRetry));
capt->AddObject("mbIgnore", reinterpret_cast<TObject *>(mbIgnore));
capt->AddObject("mbAll", reinterpret_cast<TObject *>(mbAll));
capt->AddObject("mbNoToAll", reinterpret_cast<TObject *>(mbNoToAll));
capt->AddObject("mbYesToAll", reinterpret_cast<TObject *>(mbYesToAll));
capt->AddObject("mbClose", reinterpret_cast<TObject *>(mbClose));
::MsgDlg("Tre tentime dështuan, ju lutem startoni programin nga ikona!!!",
mtInformation,
TMsgDlgButtons() << mbYes << mbNo << mbOK << mbCancel << mbAbort <<
mbRetry << mbIgnore << mbAll << mbNoToAll << mbYesToAll << mbClose,
capt.get());
Close();
}
//---------------------------------------------------------------------------
Upvotes: 0
Reputation: 13580
It creates new buttons because it actually is creating new buttons, in the line:
DlgButton = new TButton( aMsgDlg->Components[m] );
You set the Width
and Height
, but not the Top
or Left
properties, so they will be set to 0 by default - ie the top left of the parent component.
If you're trying to change the caption of an existing button, instead of making a new button simply cast the component to a button (which you've just checked you can safely do in the if
statement, because dynamic_cast
performs a runtime check to see if the classes are related and returns NULL
if it cannot safely cast) and modify it instead, like so:
if (dynamic_cast<TButton*>(aMsgDlg->Components[m])) {
DlgButton = dynamic_cast<TButton*>(aMsgDlg->Components[m]) // <- this is the key difference
if (CaptionIndex > Captions_maxidx /*# High(Captions) */) break;
DlgButton->WordWrap = true;
DlgButton->Caption = Captions[CaptionIndex];
}
That will allow you to modify existing buttons instead of creating new ones.
Upvotes: 3