如果您要使用 Visual C# 來制作「關於」對話框,建議先使用Visual Studio 2005所提供的模板來產生關於對話框窗體,然後再自訂窗體所要呈現的內容(如圖表4所示)。在此,我們選擇將組件的相關信息填入窗體對應的控件,請於「關於」對話框窗體的 Load 事件處理例程中撰寫下列程序代碼:
private void AboutBox_Load(object sender, EventArgs e)
{
AssemblyInfoClass myAssembly = new AssemblyInfoClass();
labelProductName.Text = "產品名稱:" + myAssembly.Product;
labelVersion.Text = "版本:" + myAssembly.Version;
labelCopyright.Text = "版權宣告:" + myAssembly.Copyright;
labelCompanyName.Text = "公司名稱:" + myAssembly.Company;
textBoxDescription.Text = "細部描述:" +
myAssembly.Description;
}
要顯示「關於」對話框,請替「說明」菜單項目的Click事件處理例程中撰寫下列程序代碼:
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
// 顯示關於對話框。
AboutBox MyAboutBox = new AboutBox();
// 設定關於對話框的啟始位置。
MyAboutBox.StartPosition = FormStartPosition.CenterScreen;
MyAboutBox.Show();
}
當用戶點選不同的選項按鈕時,將會執行下列程序代碼來顯示不同的動畫效果。這些程序代碼撰寫於選項按鈕的 CheckedChanged 事件處理函式中,如下所列:
private void RadioButtons_CheckedChanged(object sender,
EventArgs e)
{
if(optWink.Checked)
{
tmrAnimation.Interval = WINK_TIMER_INTERVAL;
}
else if(optBall.Checked)
{
tmrAnimation.Interval = BALL_TIMER_INTERVAL;
}
else if(optText.Checked)
{
tmrAnimation.Interval = TEXT_TIMER_INTERVAL;
}
OnResize(EventArgs.Empty);
}
自訂函式 RadioButtons_CheckedChanged 會叫用 OnResize 函式來產生不同的圖形,請大家注意,我們系使用 Graphics 類別的 FillEllipse 方法來繪制球形,程序代碼如下所列:
protected override void OnResize(EventArgs ea)
{
if (optWink.Checked)
{
Graphics grfx = CreateGraphics();
// 重繪窗體。
this.Refresh();
}
else if (optBall.Checked)
{
Graphics grfx = CreateGraphics();
grfx.Clear(BackColor);
double dblRadius = Math.Min(ClientSize.Width / grfx.DpiX,ClIEntSize.Height / grfx.DpiY) / intBallSize;
intBallRadiusX = (int)(dblRadius * grfx.DpiX);
intBallRadiusY = (int)(dblRadius * grfx.DpiY);
intBallMoveX = (int)(Math.Max(1, intBallRadiusX / intMoveSize));
intBallMoveY = (int)(Math.Max(1, intBallRadiusY / intMoveSize));
intBitmapWidthMargin = intBallMoveX;
intBitmapHeightMargin = intBallMoveY;
intBallBitmapWidth = 2 * (intBallRadiusX + intBitmapWidthMargin);
intBallBitmapHeight = 2 * (intBallRadiusY + intBitmapHeightMargin);
bitmap = new Bitmap(intBallBitmapWidth, intBallBitmapHeight);
grfx = Graphics.FromImage(bitmap);
grfx.Clear(BackColor);
// 繪制球形。
grfx.FillEllipse(Brushes.Red, new Rectangle(intBallMoveX,intBallMoveY, 2 * intBallRadiusX, 2 * intBallRadiusY));
intBallPositionX = (int)(ClIEntSize.Width / 2);
intBallPositionY = (int)(ClIEntSize.Height / 2);
}
else if (optText.Checked)
{
Graphics grfx = CreateGraphics();
grfx.Clear(BackColor);
}
}