Thứ Ba, 6 tháng 11, 2012

Cách làm hiệu ứng trượt khi mở Form C# Windows Form

Hôm nay ngồi coi lại cái tool viết hồi hơn 1 năm trước, tình cờ thấy lại đoạn code hồi xưa mình làm hiệu ứng trượt khi mở form con, sẵn tiện share cách làm cho ai có hứng thú.

Kéo vào Form con 1 Time Control sau đó khai báo các biến, hàm tạo và sự kiên Form Load như sau:
  1. private int _parentTop;  
  2. private int _parentLeft;  
  3. private int _parentWidth;  
  4. private int _parentHeight;  
  5.   
  6. public FrConnection(int parentTop, int parentLeft, int parentWidth, int parrentHeight)  
  7. {  
  8.     _parentTop = parentTop;  
  9.     _parentLeft = parentLeft;  
  10.     _parentWidth = parentWidth;  
  11.     _parentHeight = parrentHeight;  
  12.     InitializeComponent();  
  13. }  
  14.   
  15. private void FrConnection_Load(object sender, EventArgs e)  
  16. {  
  17.     Left = _parentLeft - 50;// vị trí khởi tạo của Form  
  18.     Top = ((_parentTop + _parentHeight / 2)) - (Height / 2);  
  19.     timer.Start();  
  20. }  
Ở sự kiện time_Tick khai báo:
  1. private void timer_Tick(object sender, EventArgs e)  
  2. {  
  3.     Left += 50;// tốc độ trượt của Form  
  4.     if (Left >= ((_parentLeft + _parentWidth / 2) - Width / 2))  
  5.     {  
  6.         Left = ((_parentLeft + _parentWidth / 2)) - (Width / 2);  
  7.         Top = ((_parentTop + _parentHeight / 2)) - (Height / 2);  
  8.         timer.Stop();  
  9.     }  
  10. }  
Ở Form cha, khi open Form con sẽ cần truyền các tham số top,left, width, heigh của mình vào Form con:
  1. private void newToolStripMenuItem_Click(object sender, EventArgs e)  
  2. {  
  3.     FrConnection fr = new FrConnection(this.Top, this.Left, this.Width, this.Height);  
  4.     fr.ShowDialog();  
  5. }  
Vị trí của Form con lúc bắt đầu khởi tạo:
 Sau đó Form sẽ trượt ra chính giữa:

Có thể tùy chỉnh quãng đường Form sẽ trượt tại sự kiện Form Load và tốc độ trượt của Form tại sự kiện time_Tick.

2 nhận xét :

  1. Nhận xét này đã bị quản trị viên blog xóa.

    Trả lờiXóa
    Trả lời
    1. với thể loại như bạn mình xin phép xóa comment nhé ^^!

      Xóa