C# Programming > Windows Forms

C# Aero Shake

C# detect Form shake

Detect Form Shake

Implementing the new Windows 7 Aero Shake feature in C# is a neat feature for Windows Forms. A combination of .NET code techniques will allow our Windows Forms to detect when they are shaken.

Notice that parts of the source code will be similar to the Window 7 Snap feature C# implementation.

Form Metrics

Detecting when a Windows Form is shook is a matter of measuring several properties. If the values fall within a certain range, we can assume the window was shaken. The range approach also gives developers flexibility to adjust the sensitivity of the window-shaking.

Before we can detect anything, the C# application must know when the user clicked on the titlebar and when the user let go. This is easy to accomplish by overriding WndProc instead of working with .NET events. On the other hand, keeping track of when the Form's location changes, using .NET events will be better. It is a combination of the two coding techniques.

The first metric to detect is the "speed" of the shake. This is done by saving the time when the user starts shaking the window and finishes shaking the Form. A short time span means the shake was done quickly.

The second metric to detect is "how much" the window was shaken. The trick is to store the positions of the Windows Form into a List during the shaking action. After the action is completed, the Count .NET property of the list gives an idea of "how much" the window changed positions. A long list means the window was well shaken, so to speak.

THe last metric we will check is the shift of the Form. This is done by taking the average of all the points that were stored in the C# List and finding the X and Y difference from the current window's position. The resulting point gives a general idea of how far the Form moved after all the movement. This prevents a shake from being detected during normal window dragging.

Minimizing and Maximizing Windows

Once we can detect when a C# Form was shaken, the only thing left is to perform an action. In the case of Aero Shake, we want all other windows to minimize or maximize depending on whether it is the first shake or the second.

The sample application includes the minimizing and maximizing source code. An explanation of the C# code will be found in a future article.

Sample Application

The C# sample application is an example of implementing the Aero Shake feature in C#.

Back to C# Article List