gpt4 book ai didi

c++ - 自定义消息Dlg

转载 作者:行者123 更新时间:2023-11-30 03:09:12 28 4
gpt4 key购买 nike

我正在尝试在 borland c++ builder 中制作自定义 messagedlg。但是下面的代码不是更改按钮标题,而是在对话框窗体的左上角创建新按钮。

#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);

}
//---------------------------------------------------------------------------

最佳答案

它创建新按钮,因为它实际上是在创建新按钮,在行中:

DlgButton = new TButton( aMsgDlg->Components[m] );

您设置了 WidthHeight,但没有设置 TopLeft 属性,因此它们将是默认情况下设置为 0 - 即父组件的左上角。

如果您尝试更改现有按钮的标题,而不是制作新按钮,只需将组件转换为按钮(您刚刚检查过,您可以在 if 语句,因为 dynamic_cast 执行运行时检查以查看类是否相关,如果不能安全地转换则返回 NULL)并改为修改它,如下所示:

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];
}

这将允许您修改现有按钮而不是创建新按钮。

关于c++ - 自定义消息Dlg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4318183/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com