How to make form system modal using C#?

2019-01-23 18:32发布

问题:

I need to show form as top level system-wide, e.g. over /all/ other windows on screen. I do realize this is usually /bad UI practice/, but I have very specific scenario in mind.

We intend to use normal Windows PCs for POS cash registrators. There is an option on the screen to open cash drawer. It would be rather bad for someone just to press something on a screen and get access to money when clerk isn't looking. So we equiped PCs with RFID readers and each clerk has his/her own RFID card which will be used for authentication.

I need however an mechanism to lock the computer (or make it unusable) when clerk goes away. Logging off seems too much of a nuisance.

Any ideas welcome.

LP, Dejan

回答1:

Well, after a day of trial and error I came to sort of solution.

It involves the following steps:

1. When "Lock" button is pressed new (empty) /desktop/ is created. Program is run in this desktop with full screen form and login procedure. There is nothing else to switch to or run on this desktop.

2. Task manager is disabled via registry. Of course, somebody uninvited can still access the Ctrl-Alt-Delete menu, but there is nothing of particular harm he can do there.

3. Alt-F4 and such are disabled.

4. When authentication is made, program switches back to original desktop and everything proceeds as normal.

There is some P/Invoking required, of course. If someone wants to do something similar, perhaps s/he will find my bare bones example helpful - link text

LP, Dejan



回答2:

I think you'll need to look into calling down to the Win32 API to achieve this.

You'll need to look into:

ShowWindow and SetWindowPos

and invoke them with code similar to the following (note this is pseudo-code):

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

static void ShowTopmost(Form frm)
{
     ShowWindow(frm.Handle, SW_SHOWMAXIMIZED);
     SetWindowPos(frm.Handle.ToInt32(), HWND_TOPMOST,
        0, 0, [width of desktop], [height of desktop],
        SWP_SHOWWINDOW);
}


回答3:

Form has a TopMost property.

set Form.TopMost = true



标签: c# .net security