3

What is powerful way to force a form to bring front of all the other applications using windows c# application?

Echilon
  • 10,064
  • 33
  • 131
  • 217
Anuya
  • 8,082
  • 49
  • 137
  • 222
  • 2
    I'm intrigued by your use of 'powerful'...could you extrapolate why you need a 'powerful' bring-to-front over a normal, plain-jane bring-to-front? – jrista Aug 21 '09 at 02:47
  • 8
    I don't ever want your application to bring itself to the front. It's my #1 pet-peeve in windows. Blink the taskbar all you want, but if you take focus ... – Noon Silk Aug 21 '09 at 02:49
  • @jrista, i used the term powefull because, i already tried using Topmost = true. But still some third party applications are overriding this form and due to this my form is staying behind. – Anuya Aug 21 '09 at 02:51
  • The purpose of usisng this form is to block the user the view other applications. – Anuya Aug 21 '09 at 02:51
  • 2
    I 100% agree, Silky. Karthik, this is exactly what those third-party apps are doing - fighting for who gets to be on top, like kids wanting the top bunk. The users, on the other hand, get more and more irritated, and they will avoid the applications forcing focus at all costs! – user158017 Aug 21 '09 at 02:56
  • 2
    @karthik: Given your last comment, I respectfully refuse to answer. You are attempting to violate THE cardinal rule of writing a user-friendly application, and I can not, in good conscience, provide knowledge that will harm even one of the millions of wonderful end users out there who will inevitably cringe at your fascist application's takeover of their personal computers. – jrista Aug 21 '09 at 02:59
  • @jrista, It is not like it is a non user friendly application. I am doing that because the user shud be locked from the system after a specific point of time. The form which i block is the login screen. Its actuall the bussiness rule of my applicaiton and its all done for the security purpose of the end user. Thanks. – Anuya Aug 21 '09 at 03:11
  • 3
    Call LockWorkstation then. http://msdn.microsoft.com/en-us/library/aa376875%28VS.85%29.aspx – 1800 INFORMATION Aug 21 '09 at 03:18

6 Answers6

15

Powerfully force the user to click on your application window icon in the task-bar.

1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
  • Nope, the form show up automatically in order to block the user, so it shud be done on its own without any clicking, – Anuya Aug 21 '09 at 02:54
  • +69 We were all stuck trying to fix this for hours, thank you. –  Apr 21 '17 at 15:03
8

Set Form.TopMost to true

RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • i already tried using Topmost = true. But still some third party applications are overriding this form and due to this my form is staying behind. The purpose of usisng this form is to block the user the view other applications. – Anuya Aug 21 '09 at 02:52
  • 1
    Well, in response to that statement about other apps overriding the form. Maybe you could do a little research to find out "how to override Topmost = true" or "how to override this.bringToFront();" in C#... Maybe that's how these third-party apps do it. My logic is... If you do it the same way as them - you should be able to bring your form to the front, because you'll be overriding their too. – jay_t55 Aug 21 '09 at 07:44
6

Here is the code that worked for me:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace LicenseManager {

  public static class WinApi {

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

    static private IntPtr HWND_TOPMOST = new IntPtr( -1 );

    private const uint SWP_NOSIZE = 0x0001;
    private const uint SWP_NOMOVE = 0x0002;

    static public void MakeTopMost( Form f ) {
      SetWindowPos( f.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
    }

  }
}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
5

this.BringToFront();

It works good for me.

jay_t55
  • 11,362
  • 28
  • 103
  • 174
1

Some more information about this that may be useful. I used Charles Jenkins' answer to make my winforms app become topmost by calling the "MakeTopMost" function in the form.load event handler. I needed this function as my winforms app was launched by an MSI install and the Windows installer progress bar would show on top.

However, this then put the main form on top of other sub-forms that the main form wanted to show. To get around this I called my function MakeWindowNormal in the form.shown event handler to put the main form back as a normal window as it was now in front of the Windows installer progress bar as it had been loaded and activated (see http://msdn.microsoft.com/en-us/library/86faxx0d(v=vs.110).aspx for event order). This allows the sub-forms (and other windows if the user moves them) to now go in front of the main form.

static public void MakeWindowNormal(Form f)
{
    SetWindowPos(f.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
NJA
  • 31
  • 1
  • 4
1

Use SetWindowsPos() api function

Anuya
  • 8,082
  • 49
  • 137
  • 222