2008-09-16 10 views
3

Genel giderlerini minimumda tutmak isterim. Şu anda var:Yönetilen C++ uygulamasında özel düğme metnine sahip bir MessageBox oluşturmanın kolay yolu nedir?

// Launch a Message Box with advice to the user 
DialogResult result = MessageBox::Show("This may take awhile, do you wish to continue?", "Warning", MessageBoxButtons::YesNo, MessageBoxIcon::Exclamation); 

// The test will only be launched if the user has selected Yes on the Message Box 
if(result == DialogResult::Yes) 
{ 
    // Execute code 
} 

Maalesef müvekkilim tercih ederim "Devam" ve varsayılan "Evet" ve "Hayır" düğme metni yerine "İptal" olacaktır. Bunu yapmanın kolay bir yolu olmalı gibi görünüyor.

cevap

2
inanmıyorum gibi, yeni bir form oluşturmak zorunda kalacak "OK" ve bunun MessageBoxButtons::OKCancel

MessageBoxButtons Enum

Short ile MessageBoxButtons::YesNo yerine koyarak

"İptal" kullanabilirsiniz

Enum uzatılabilir.

0

Bulabildiğim her şeyden Corin doğru görünüyor. İşte asıl hedefi başarmak için kullandığım kod. Genellikle bir Yönetilen C++ programcısı değilim, bu yüzden herhangi bir düzenleme takdir edilecektir.

CustomMessageBox.h:

using namespace System::Windows::Forms; 

/// <summary> 
/// A message box for the test. Used to ensure user wishes to continue before starting the test. 
/// </summary> 
public ref class CustomMessageBox : Form 
{ 
private: 
    /// Used to determine which button is pressed, default action is Cancel 
    static String^ Button_ID_ = "Cancel"; 

    // GUI Elements 
    Label^ warningLabel_; 
    Button^ continueButton_; 
    Button^ cancelButton_; 

    // Button Events 
    void CustomMessageBox::btnContinue_Click(System::Object^ sender, EventArgs^ e); 
    void CustomMessageBox::btnCancel_Click(System::Object^ sender, EventArgs^ e); 

    // Constructor is private. CustomMessageBox should be accessed through the public ShowBox() method 
    CustomMessageBox(); 

public: 
    /// <summary> 
    /// Displays the CustomMessageBox and returns a string value of "Continue" or "Cancel" 
    /// </summary> 
    static String^ ShowBox(); 
}; 

CustomMessageBox.cpp:

#include "StdAfx.h" 
#include "CustomMessageBox.h" 

using namespace System::Windows::Forms; 
using namespace System::Drawing; 

CustomMessageBox::CustomMessageBox() 
{ 
    this->Size = System::Drawing::Size(420, 150); 
    this->Text="Warning"; 
    this->AcceptButton=continueButton_; 
    this->CancelButton=cancelButton_; 
    this->FormBorderStyle= ::FormBorderStyle::FixedDialog; 
    this->StartPosition= FormStartPosition::CenterScreen; 
    this->MaximizeBox=false; 
    this->MinimizeBox=false; 
    this->ShowInTaskbar=false; 

    // Warning Label 
    warningLabel_ = gcnew Label(); 
    warningLabel_->Text="This may take awhile, do you wish to continue?"; 
    warningLabel_->Location=Point(5,5); 
    warningLabel_->Size=System::Drawing::Size(400, 78); 
    Controls->Add(warningLabel_); 

    // Continue Button 
    continueButton_ = gcnew Button(); 
    continueButton_->Text="Continue"; 
    continueButton_->Location=Point(105,87); 
    continueButton_->Size=System::Drawing::Size(70,22); 
    continueButton_->Click += gcnew System::EventHandler(this, &CustomMessageBox::btnContinue_Click); 
    Controls->Add(continueButton_); 

    // Cancel Button 
    cancelButton_ = gcnew Button(); 
    cancelButton_->Text="Cancel"; 
    cancelButton_->Location=Point(237,87); 
    cancelButton_->Size=System::Drawing::Size(70,22); 
    cancelButton_->Click += gcnew System::EventHandler(this, &CustomMessageBox::btnCancel_Click); 
    Controls->Add(cancelButton_); 
} 

/// <summary> 
/// Displays the CustomMessageBox and returns a string value of "Continue" or "Cancel", depending on the button 
/// clicked. 
/// </summary> 
String^ CustomMessageBox::ShowBox() 
{ 
    CustomMessageBox^ box = gcnew CustomMessageBox(); 
    box->ShowDialog(); 

    return Button_ID_; 
} 

/// <summary> 
/// Event handler: When the Continue button is clicked, set the Button_ID_ value and close the CustomMessageBox. 
/// </summary> 
/// <param name="sender">The source of the event.</param> 
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 
void CustomMessageBox::btnContinue_Click(System::Object^ sender, EventArgs^ e) 
{ 
    Button_ID_ = "Continue"; 
    this->Close(); 
} 

/// <summary> 
/// Event handler: When the Cancel button is clicked, set the Button_ID_ value and close the CustomMessageBox. 
/// </summary> 
/// <param name="sender">The source of the event.</param> 
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 
void CustomMessageBox::btnCancel_Click(System::Object^ sender, EventArgs^ e) 
{ 
    Button_ID_ = "Cancel"; 
    this->Close(); 
} 

Ve sonra nihayet orijinal koduna modifikasyon:

// Launch a Message Box with advice to the user 
String^ result = CustomMessageBox::ShowBox(); 

// The test will only be launched if the user has selected Continue on the Message Box 
if(result == "Continue") 
{ 
    // Execute Code 
} 
0

Değişim aşağıda mesaj. Bu düşündüğüm en basit yol olabilir.

DialogResult result = MessageBox::Show(
     "This may take awhile, do you wish to continue?**\nClick Yes to continue.\nClick No to cancel.**", 
     "Warning", 
     MessageBoxButtons::YesNo, 
     MessageBoxIcon::Exclamation 
); 
İlgili konular