Search code examples
c#.netwinformswindowform-control

Windows Form Window Always Focused?


I know there are a few threads about this already. What I need is to have a windows form window always focused meaning if I click on notepad or whatever program it will not enter any data in it and only enter data in my windows form textbox.

I found this code which works somewhat will explain more

    //Delegates for safe multi-threading.
    delegate void DelegateGetFocus();
    private DelegateGetFocus m_getFocus;
    Thread newThread;

    public MemberLogin()
    {

        m_getFocus = new DelegateGetFocus(this.getFocus);  
        InitializeComponent();
        spawnThread(keepFocus);
        toggleFocusButton.Text = "OFF";
        timer1.Interval = 2000;
        textBox1.Select();
    }

    //test focus stuff
    //Spawns a new Thread.
    private void spawnThread(ThreadStart ts)
    {
        try
        {
            newThread = new Thread(ts);
            newThread.Start();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message, "Exception!", MessageBoxButtons.OK,
                MessageBoxIcon.Error);
        }
    }

    //Continuously call getFocus.
    private void keepFocus()
    {
        while (true)
        {
            getFocus();
        }
    }

    //Keeps Form on top and gives focus.
    private void getFocus()
    {
        //If we need to invoke this call from another thread.
        if (this.InvokeRequired)
        {
            this.Invoke(m_getFocus, new object[] { });
        }
        //Otherwise, we're safe.
        else
        {
            //having this seemed to have kept my windows onTop at all times even when off 
           // this.TopMost = true;
            this.TopMost = true;
            this.Activate();
            this.textBox1.Select();
            this.textBox1.Focus();

        }
    }

this code seems to work well only when my project is opened meaning when my Visual Studio Project is closed the window is top most but doesn't have focus meaning I can type inside other programs. Weird thing I found is that both notepad and my textbox had blinking line thing telling where you write the text. If I run my app from the Visual Studio project everything works as it should and when I try to click on other windows it won't let me access which is what I want.

So I'm a bit confused on why it only works properly with the project opened

Also note as long as the project is opened then even .exe and other copys I made work properly I close the project solution and the program does what I explained above.

Just did some more testing and it seems to only work properly when this process is running vhost.exe which is the Visual Studio hosting process. I disabled it in the settings, and when I launch from VS it works fine but when I run just the exe in the bin folder I still get the weird results

EDIT

Here's a quick video I made with my results http://www.youtube.com/watch?v=1ozpHSRGnMo

New Edit

What I did to fix this was set my app in a fullscreen mode kinda by doing this so the user can go clicking on other windows without closing this one first

this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;

Solution

  • You cannot create a Windows Forms application that can stop other programs from gaining focus. Windows itself will not allow this. The only way around this is to create an OpenGL or DirectX application in full screen mode. This will stop the user seeing any other application. But even with this, the user can Alt+Tab their way back to other applications.