Use a timer to make a form TopMost or BottomMost in C#


The example Make a form TopMost or BottomMost in C# uses one method for making a form bottommost. It overrides the form’s WndProc method and looks for the message WM_WINDOWPOSCHANGING. When it finds that message, the program uses the SetWindowPos API function to tell the form to move to the bottom of the window z-order.

As I mentioned in that post, there’s one situation that the program doesn’t handle: if you click on the form’s title bar, and then click on it again, the form moves to the top.

This example uses a different approach. It uses a timer to move the form to the bottom of the z-order periodically.

When you click one of the form’s buttons, the following code executes.

private void radOption_Click(object sender, EventArgs e)
    this.TopMost = radTopmost.Checked;
    tmrMoveToBottom.Enabled = radBottommost.Checked;

If the Topmost radio button is checked, the code sets the form’s TopMost property to true. The code enables the tmrMoveToBottom timer if the Bottommost radio button is checked.

The following code shows the timer’s Tick event handler.

// Move the form to the bottom.
private void tmrMoveToBottom_Tick(object sender, EventArgs e)

This code simply calls the following MoveToBottom method to move the form to the bottom of the stacking order.

private void MoveToBottom()
    UInt32 flags = SWP_NOSIZE | SWP_NOMOVE;
    if (!SetWindowPos(this.Handle, HWND_BOTTOM, 0, 0, 0, 0, flags))
        Console.WriteLine("Error in SetWindowPos");

This method uses the SetWindowPos API function to set the form’s position as described in the previous post.

This approach is simpler than the one used by the previous post, although it is a bit heavy-handed. At design time I set the timer’s Interval property to 500 so it fires twice per second. You can make it fire more or less frequently if you like. Firing more often makes the form pop down to the bottom more quickly, but it also uses more system resources.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in API, controls, forms, user interface and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Use a timer to make a form TopMost or BottomMost in C#

  1. uncleJam says:

    DL-Link should be …

Comments are closed.