Some time ago, I made a project to add buttons and the dynamic effects of picturebox. So I made a class by myself to realize the free scaling and jitter of controls. There are two ways to scale freely: 1. after the mouse enters the control, the control gradually becomes larger. After the mouse is removed, the control gradually returns to the original size. 2. when the mouse is pressed on the control, the return size of the control is reduced when the mouse is up. The free jitter of the control is also divided into two types: 1. after the mouse enters the control, the control suddenly jumps up and then has a tremor effect. After the mouse moves away, the control falls back to the original position. 2. When the mouse enters the control, the control changes back and forth, and the mouse is removed, and the control returns to rest. In summary, this class provides four dynamic effects. The source code is as follows:
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. Timers;
Namespace controlchangedemo
{
Public class controlchange
{
# Region attributes and constructor
Private string filename; // image path
Private control; // Control
Private size osize; // the original size of the control.
Private size nsize; // the new size and size of the control
Private point eachchangep; // the size of each move
Private int time; // number of changes
Private int timetick; // clock Interval
Private int COUNT = 0; // number of changes recorded
Private system. Timers. Timer timer; // clock
Private point controllocation; // the start position of the control.
Public controlchange (control con, string filename)
{
// Set the property of the asynchronous call control to false.
Control. checkforillegalcrossthreadcils = false;
// Controls to be controlled in this class
This. control = con;
// Control's background image path
This. filename = filename;
Con. backgroundimage = image. fromfile (filename );
// The image is stretched along with the widget size
This. Control. backgroundimagelayout = imagelayout. Stretch;
// Original widget position
This. controllocation = This. Control. location;
}
Public controlchange (control con)
{
// Set the property of the asynchronous call control to false.
Control. checkforillegalcrossthreadcils = false;
This. control = con;
This. Control. backgroundimagelayout = imagelayout. Stretch;
This. controllocation = This. Control. location;
// The default number of changes is 8.
This. Time = 8;
}
Public int gettime ()
{
Return this. Time;
}
Public void settime (int t)
{
This. Time = T;
}
# Endregion
# Region controls the widget size
# Region calls this function, but when the mouse moves over the control, the Controller gradually increases. when the control is removed, the Controller gradually returns to the original size.
Public void picturebecomebigger (INT timetick, int time, size OS, size NS)
{
// Responds to the mouse Entry Event
This. Control. mouseenter + = new eventhandler (picturebiggerevent );
// Responds to the mouse exit event
This. Control. mouseleave + = new eventhandler (picturelittlerevent );
This. timetick = timetick;
This. Time = time;
This. osize = OS;
This. nsize = ns;
}
Private void picturebiggerevent (Object sender, eventargs E)
{
Picturebigging ();
}
Private void picturelittlerevent (Object sender, eventargs E)
{
If (this. Timer. Enabled = true)
{
This. Timer. Close ();
}
Litterchange ();
}
# Endregion
# Region calls this function. When you click a control, the widget gradually decreases and then gradually returns the original size.
Public void picturebecomelitter (INT timetick, int time, size OS, size NS)
{
This. Count = time;
This. Control. mousedown + = new mouseeventhandler (lbmousedownevent );
This. Control. mouseup + = new mouseeventhandler (lbmouseupevent );
This. timetick = timetick;
This. Time = time;
This. osize = OS;
This. nsize = ns;
}
Private void lbmousedownevent (Object S, mouseeventargs E)
{
This. Count = this. Time;
// Call the litterchange function to gradually decrease the control.
Litterchange ();
}
Private void lbmouseupevent (Object S, mouseeventargs E)
{
// This. Count = this. Time;
//// Call the litterchange function to control the widget.
// Litterchange ();
If (this. Timer. Enabled = true)
{
This. Timer. Close ();
// Call the picturesbigging function to control the original size of the control.
Picturebigging ();
}
}
# Endregion
Private void picturebigging ()
{
Eachchangep = new point (nsize. Width-osize. width)/time, (nsize. Height-osize. Height)/time );
Timer = new system. Timers. Timer (timetick );
Timer. elapsed + = new system. Timers. elapsedeventhandler (picturebigertimer );
Timer. Start ();
}
Private void picturebigertimer (Object source, system. Timers. elapsedeventargs E)
{
If (this. Count> = this. Time)
{
This. Timer. Close ();
}
Else
{
This. Control. Location = new point (this. Control. Location. X-eachchangep. X/2, this. Control. Location. Y-eachchangep. Y/2 );
This. Control. Size + = new size (eachchangep. X, eachchangep. y );
This. Count + = 1;
}
}
Private void litterchange ()
{
Eachchangep = new point (nsize. Width-osize. width)/time, (nsize. Height-osize. Height)/time );
Timer = new system. Timers. Timer (timetick );
Timer. elapsed + = new system. Timers. elapsedeventhandler (picturelittertimer );
Timer. Start ();
}
Private void picturelittertimer (Object source, system. Timers. elapsedeventargs E)
{
Try
{
If (this. Count <= 0)
{
This. Timer. Close ();
This. Count = 0;
}
Else
{
This. Control. Location = new point (this. Control. Location. x + eachchangep. X/2, this. Control. Location. Y + eachchangep. Y/2 );
This. Control. Size-= new size (eachchangep. X, eachchangep. y );
This. Count-= 1;
}
}
Catch
{
}
}
# Endregion
# Region jitter control
Public void shakecontrol (INT Ti, int titick) // time indicates the number of changes;
{
This. Time = Ti;
This. timetick = titick;
This. Control. mouseenter + = new eventhandler (shakemouseenterevent );
This. Control. mouseleave + = new eventhandler (shakemouseleaveevent );
}
Private void shakemouseenterevent (Object source, eventargs E)
{
This. Count = 0;
Timer = new system. Timers. Timer (this. timetick );
Timer. elapsed + = new elapsedeventhandler (shakemouseentertimer );
Timer. Start ();
}
Private void shakemouseleaveevent (Object source, eventargs E)
{
If (this. Timer. Enabled = true)
{
This. Timer. Close ();
}
This. Count = 0;
Control. Location = controllocation;
}
Private void shakemouseentertimer (Object source, system. Timers. elapsedeventargs E)
{
Int changelength = This. Control. Size. Height/80*2 ^ (count + 1 );
If (this. Count % 2 = 0)
{
If (this. Count = 0)
{
This. Control. Location = new point (this. Control. Location. X, this. Control. Location. Y-changeLength-10 );
}
Else
{
This. Control. Location = new point (this. Control. Location. X, this. Control. Location. Y-changelength );
}
}
Else
{
This. Control. Location = new point (this. Control. Location. X, this. Control. Location. Y + changelength );
}
Count = count + 1;
If (count> = Time)
{
This. Timer. Close ();
This. Count = 0;
}
}
# Endregion
# Region make the control shake back and forth
Public void movecontrol (INT change, int titick)
{
This. Time = change;
This. timetick = titick;
This. Control. mouseenter + = new eventhandler (movemouseenterevent );
This. Control. mouseleave + = new eventhandler (movemouseleaveevent );
}
Private void movemouseenterevent (Object source, eventargs E)
{
This. Count = 0;
Timer = new system. Timers. Timer (this. timetick );
Timer. elapsed + = new elapsedeventhandler (movemouseentertimer );
Timer. Start ();
}
Private void movemouseleaveevent (Object source, eventargs E)
{
If (this. Timer. Enabled = true)
{
This. Timer. Close ();
}
This. Count = 0;
Control. Location = controllocation;
}
Private void movemouseentertimer (Object source, system. Timers. elapsedeventargs E)
{
If (count <this. Time * 2)
{
Int I = count/2;
If (0 = count % 2)
{
This. Control. Location = new point (this. controllocation. X, this. controllocation. Y-this. Time + I );
}
Else
{
This. Control. Location = new point (this. controllocation. X, this. controllocation. Y + this. Time-I );
}
}
Else
{
This. Timer. Close ();
}
++ Count;
}
# Endregion
}
}
You only need to initialize a Class Object and call a function in picturebecomebigger, picturebecomelitter, shakecontrol, or movecontrol to achieve a dynamic effect. For your better use of this class, I also wrote a demo program to demonstrate these four dynamic effects, you can use the title "C # control dynamic effect instance" on msdn to search for the source code and download it.