2013-05-02 41 views
1

我正在學習C#,作爲它的一部分,我編寫了一個小型應用程序,它包含兩組按鈕,每組按鈕各有兩個按鈕。還添加了關閉應用程序的按鈕。一切正常。還有MessageBox,顯示應用程序將被關閉。 這裏是一個小問題,我錯誤:該消息框內的確定按鈕不是水平居中。我想有一種方法來對齊該按鈕,但是讓我感到困惑的是爲什麼它不是默認居中?作爲解說這裏是截圖:在winforms的MessageBox中居中確定按鈕

Form with radio buttons

MessageBox with OK button

這裏是代碼,以及:

using System; 
using System.Drawing; 
using System.Windows.Forms; 

public class myForm : Form 
{ 
    private GroupBox gboxGrp1; 
    private GroupBox gboxGrp2; 

    private RadioButton butn1a; 
    private RadioButton butn1b; 
    private RadioButton butn2a; 
    private RadioButton butn2b; 
    private Button btnClose; 

    public myForm() 
    { 
     InitializeComponent(); 
    } 

    private void InitializeComponent() 
    { 
     this.btnClose = new Button(); 

     this.gboxGrp1 = new GroupBox(); 
     this.gboxGrp2 = new GroupBox(); 

     this.butn1a = new RadioButton(); 
     this.butn1b = new RadioButton(); 
     this.butn2a = new RadioButton(); 
     this.butn2b = new RadioButton(); 

     //myForm 
     this.Text = "My Form"; 
     this.StartPosition = FormStartPosition.CenterScreen; 
     this.Height = 350; 
     this.Width = 200; 

     //btnClose 
     this.Controls.Add(btnClose); 
     this.btnClose.Text = "Close"; 
     this.btnClose.Location = new Point(60, 260); 
     this.btnClose.Click += new EventHandler(btnClose_Click); 

     //gboxgrp1 
     this.gboxGrp1.Location = new Point(20, 20); 
     this.gboxGrp1.Text = "Group Box 1"; 
     this.gboxGrp1.Width = 150; 
     this.gboxGrp1.Height = 100; 

     //gboxgrp2 
     this.gboxGrp2.Text = "Group Box 2"; 
     this.gboxGrp2.Location = new Point(20, 130); 
     this.gboxGrp2.Width = 150; 
     this.gboxGrp2.Height = 100; 

     //Radio buttons 
     this.butn1a.Text = "Radio 1a"; 
     this.butn1a.Location = new Point(30, 30); 
     this.butn1a.Size = new Size(90, 15); 

     this.butn1b.Text = "Radio 1b"; 
     this.butn1b.Location = new Point(30, 60); 
     this.butn1b.Size = new Size(90, 15); 

     this.butn2a.Text = "Radio 2a"; 
     this.butn2a.Location = new Point(30, 30); 
     this.butn2a.Size = new Size(90, 15); 

     this.butn2b.Text = "Radio 2b"; 
     this.butn2b.Location = new Point(30, 70); 
     this.butn2b.Size = new Size(90, 15); 

     //Controls 
     this.Controls.Add(gboxGrp1); 
     this.Controls.Add(gboxGrp2); 

     this.gboxGrp1.Controls.Add(butn1a); 
     this.gboxGrp1.Controls.Add(butn1b); 
     this.gboxGrp2.Controls.Add(butn2a); 
     this.gboxGrp2.Controls.Add(butn2b); 
    } 

    private void btnClose_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show("Closing Application"); 
     Application.Exit(); 
    } 
} 

public class MyApp 
{ 
    public static void Main() 
    { 
     Application.Run(new myForm()); 
    } 
} 
+1

'MessageBox'類在內部調用一些您無法控制的Windows API。我認爲沒有辦法改變這一點。 – 2013-05-02 15:17:27

+1

我想,如果你要在Windows XP中運行你的程序,它將被集中。我認爲(可能是錯誤的),但如果你要改變.Net框架,你的應用程序的目標是舊版本,它將集中。它基本上管理什麼微軟喜歡的外觀 – Sayse 2013-05-02 15:19:05

+1

檢查[如何自定義消息框](http://stackoverflow.com/questions/3734796/how-to-customize-message-box) – Damith 2013-05-02 15:23:45

回答

2

不能restyle默認的MessageBox因爲這是完全依賴於Windows操作系統主題。不過,您可以通過創建一個新窗體來創建自己的消息框,然後使用newMessagebox.ShowDialog()來調用它。