程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 利用Visual C# 2005制作簡單動畫效果(3)

利用Visual C# 2005制作簡單動畫效果(3)

編輯:關於C語言

如果您要使用 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);
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved