Simple implementation similar to VM menu (2)

Source: Internet
Author: User

The preceding implementation method is to bind the mousehover of the panel with the mouseleave event to achieve the effect similar to the VM menu.

However, this implementation has a disadvantage: if the Panel has a control, such as a button, when you move the mouse over the button, you have to respond to the Panel

Mouseleave event...

There are two solutions,

1. Rewrite the mouse binding event to add the same mousehover and mouseleave event to the Panel and all the controls above. This implementation is too troublesome. Reject...

2. Read the mouse position without stopping through a timer to determine whether the mouse is on the panel... This will have an impact on efficiency, but the effect is still unsatisfactory.

The Code is as follows:

// Put a button button1 on the panel

# Region mouse control title bar explicit
// Enter with the mouse
Private bool mentered = false;
Void timereffectick (Object sender, eventargs E)
{
Point pos2 = This. pointtoclient (cursor. position );
Point Pos = panel1.pointtoclient (cursor. position );
Bool entered = This. panel1.clientrectangle. Contains (POS );
If (entered! = Mentered)
{
Mentered = entered;
If (! Entered)
{
// Move the mouse away
Animation (false );
}
Else
{
// Hover the mouse over
If (! Ismoving)
If (this. panel1.location. Y <= 10-This. panel1.height)
Animation (true );
}
}
}
Bool updown = true;
Int adddistance = 3;
Bool ismoving = false;
System. Windows. Forms. Timer timer = new timer ();
Private void form2_load (Object sender, eventargs E)
{//
Panel1.margin = new system. Windows. Forms. Padding (0, 0, 0, 0 );
Panel1.location = new point (panel1.location. X, 10-panel1.height );
}
Private void animation (bool isdown)
{
Updown = isdown;
If (isanimation)
{
Ismoving = true;

Timer = new timer ();
Timer. interval = 50;
Timer. Tick + = new eventhandler (timer_tick );
Timer. Start ();
}
}

Void timer_tick (Object sender, eventargs E)
{
If (updown) // show
If (this. panel1.location. Y <0)
{
This. panel1.location = new point (this. panel1.location. X,
Math. Min (0, this. panel1.location. Y + adddistance ));
}
Else
{
//
Ismoving = false;
Timer. Dispose ();
}
Else // hide
{
If (this. panel1.location. Y> 10-panel1.height)
This. panel1.location = new point (this. panel1.location. X,
Math. Max (10-panel1.height, this. panel1.location. Y-adddistance ));
Else
{
Ismoving = false;
Timer. Dispose ();
}
}

}
Bool isanimation = true;
Private void button#click (Object sender, eventargs E)
{
Isanimation =! Isanimation;
}
# Endregion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.