Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a Windows form to be bottommost

Tags:

c#

.net

winforms

Some background

One of my current clients runs a chain of Internet points where customers an access the net through PC:s set up as "kiosks" (a custom-built application "locks" the computer until a user has signed in, and the running account is heavily restricted through the Windows group policy). Currently, each computer is running Windows XP and uses Active Desktop to display advertisements in the background. However, since my client has got problems with Active Desktop crashing on a daily basis (in addition to generally slowing down the computer) I have been asked to develop an application that replaces it.

The problem

I am trying to investigate whether it is possible to build a Windows forms application (using C#) that always stays in the background. The application should lie above the desktop (so that it covers any icons, files etc) but always behind all other running applications. I guess I'm really looking for a BottomMost property of the Form class (which doesn't exist, of course).

Any tips or pointers on how to achieve this would be highly appreciated.

like image 935
Anders Fjeldstad Avatar asked Jan 08 '10 12:01

Anders Fjeldstad


People also ask

How do I resize a Windows Form?

By dragging either the right edge, bottom edge, or the corner, you can resize the form. The second way you can resize the form while the designer is open, is through the properties pane. Select the form, then find the Properties pane in Visual Studio. Scroll down to size and expand it.

How do I change the color of a form in Windows?

Set the background in the Windows Forms DesignerOpen the project in Visual Studio and select the Panel control. In the Properties window, click the arrow button next to the BackColor property to display a window with three tabs. Select the Custom tab to display a palette of colors.

Can you change the background Colour of the form window?

Explanation: Open the form for editing. Then go to Format -> Page -> Area and choose a background color.


1 Answers

This isn't directly supported by the .NET Form class, so you have two options:

1) Use the Win32 API SetWindowPos function.

pinvoke.net shows how to declare this for use in C#:

[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_NOACTIVATE = 0x0010;

So in your code, call:

SetWindowPos(Handle, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

As you commented, this moves the form to the bottom of the z-order but doesn't keep it there. The only workaround I can see for this is to call SetWindowPos from the Form_Load and Form_Activate events. If your application is maximized and the user is unable to move or minimise the form then you might get away with this approach, but it's still something of a hack. Also the user might see a slight "flicker" if the form gets brought to the front of the z-order before the SetWindowPos call gets made.


2) subclass the form, override the WndProc function and intercept the WM_WINDOWPOSCHANGING Windows message, setting the SWP_NOZORDER flag (taken from this page).

like image 53
Richard Ev Avatar answered Oct 12 '22 15:10

Richard Ev