2014-03-19 1 views
0

사용자가 마우스를 사용하여 이동할 때까지 페이드 아웃하려는 로그인 양식 (Form1)이 있으며 사용자가 창을 떠날 때 초기 투명도로 돌아갑니다.C# VS2010에서 마우스 위로 마우스를 가져 가면 페이드 인/아웃 효과를 만드는 방법은 무엇입니까?

나는 MouseHover 및 MouseLeave 이벤트를 시도했지만 원하는 효과를 얻을 수 없으므로 나를 도와 줄 수 있습니까?

+0

이벤트가 컨트롤에 대해 실행되므로 마우스를 컨트롤 위에 놓으면 폼에 이벤트가 수신되지 않습니다. 1) 모든 컨트롤 (및 그 자손)에서 이벤트를 구독하거나 2) 마우스 훅을 사용할 수 있습니다. Google에 문의하십시오 ... –

+0

@Adriano 또는 3) 마우스가 양식 영역 위에 있는지 확인하려면 타이머를 사용하십시오. – LarsTech

+0

@ LarsTech 맞아! –

답변

1
public partial class Form1 : Form 
{ 
    private Panel panel1; 
    private PictureBox pictureBox1; 
    private Timer timer; 
    private double opacity_increment; 
    private bool mouse_over; 
    public Form1() 
    { 
     InitializeComponent(); 
     opacity_increment = 0.1d; 
     timer = new Timer(); 
     timer.Interval = 100; 
     timer.Tick += new EventHandler(timer_Tick); 
     timer.Enabled = true; 

     panel1 = new Panel(); 
     pictureBox1 = new PictureBox(); 

     ((System.ComponentModel.ISupportInitialize)(pictureBox1)).BeginInit(); 
     SuspendLayout(); 

     panel1.Dock = System.Windows.Forms.DockStyle.Fill; 
     panel1.Location = new System.Drawing.Point(0, 0); 
     panel1.Name = "panel1"; 
     panel1.BackColor = Color.Black; 
     panel1.Size = new System.Drawing.Size(322, 180); 
     panel1.TabIndex = 0; 
     panel1.MouseEnter += new EventHandler(MouseEnterLogic); 
     panel1.MouseLeave += new EventHandler(MouseLeaveLogic); 

     pictureBox1.BackColor = System.Drawing.Color.Maroon; 
     pictureBox1.Location = new System.Drawing.Point(0, 0); 
     pictureBox1.Name = "pictureBox1"; 
     pictureBox1.Size = new System.Drawing.Size(100, 50); 
     pictureBox1.TabIndex = 0; 
     pictureBox1.TabStop = false; 
     pictureBox1.MouseEnter += new EventHandler(MouseEnterLogic); 
     pictureBox1.MouseLeave += new EventHandler(MouseLeaveLogic); 

     Controls.Add(panel1); 
     panel1.Controls.Add(pictureBox1); 

     ((System.ComponentModel.ISupportInitialize)(pictureBox1)).EndInit(); 
     ResumeLayout(); 
    } 
    private void timer_Tick(object sender, EventArgs e) 
    { 
     if (mouse_over) 
     { 
      if (Opacity <= 1) 
      { 
       Opacity += opacity_increment; 
      } 
     } 
     else 
     { 
      if (Opacity >= 0.5d) 
      { 
       Opacity -= opacity_increment; 
      } 
     } 
    } 
    private void MouseLeaveLogic(object sender, EventArgs e) 
    { 
     if (Cursor.Position.X > this.Location.X + this.Width - 10 || Cursor.Position.Y > this.Location.Y + this.Height - 10 || Cursor.Position.X < this.Location.X + 10 || Cursor.Position.Y < this.Location.Y + 30) 
      mouse_over = false; 
    } 
    private void MouseEnterLogic(object sender, EventArgs e) 
    { 
     mouse_over = true; 
    } 
    // You don't need overrides because if you have docked panel on the form thus the cursor will never firing these methods (cursor never overlaps the form). 
    //protected override void OnMouseEnter(EventArgs e) 
    //{ 
    // MouseEnterLogic(this, e); 
    // base.OnMouseEnter(e); 
    //} 
    //protected override void OnMouseLeave(EventArgs e) 
    //{ 
    // MouseLeaveLogic(this, e); 
    // base.OnMouseEnter(e); 
    //} 
} 
+0

마우스가 양식의 컨트롤 위로 이동하면 코드가 사라집니다. 다른 문제 중 ... – LarsTech

+0

OnMouseLeave 메서드를 편집하여 지금 시도해보십시오. – Wallstrider

+0

이제 폼 가장자리에 컨트롤을 배치하십시오. 양식의 MouseEnter 및 MouseLeave 이벤트는 마우스가 해당 컨트롤의 경계에있을 때 실행되지 않습니다. :-) – LarsTech

관련 문제