CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2010
    Location
    .NET 3.5 / VS2008
    Posts
    7

    Window Form don't lose focus

    Is there a way to make my Window Form not to lose focus to other windows in my application or other applicaitons ?

    I want to create an alert window that when it shows the user must close it in order to switch to other application or othe windows in my application - like global dialog box.

    I did full screen, top most, no border window but Alt+tab switch to other applications and let the user type information in them even when he don't see the application's window - is there a way to grab focus when lost ?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Window Form don't lose focus

    Please rethink this, that behavior is simply obnoxious. Unless this is some highly specialized application which is meant to be run as a kiosk type app (and has a very good *business* reason to do so, which I doubt), let the user do what they want, you don't own their computer. If you want to do this to work around some bug/issue in *your* app, please fix *your* bug instead of offloading it to your users.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  3. #3
    Join Date
    Apr 2010
    Location
    .NET 3.5 / VS2008
    Posts
    7

    Re: Window Form don't lose focus

    I know it not something you will do with "normal" application but I really need that behavior.
    I am writting an application for experiment - it shows few windows to the user and after given time it will pop up another window to do soemthing else. During the time he show view the pop up and need to do the taks there we want to provent him from going back to IE or other open documents. After he finish with the pop up task he can go back to the other windows / applications.
    And those users will have to do as we asked them to because they get paid for participate.

    So please any help will be wellcome (already tried few Win API on disactive event but none did what I want).
    Last edited by SAAM007; April 26th, 2010 at 04:24 AM.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Window Form don't lose focus

    You can make a 'sentinal' thread that uses EnumWindows to make sure the top level window is always a window of your application. You can use the same thread to call SetForegroundWindow to return focus to your app.

    Take a look at pinvoke.net to see how to call these Win32 api's in your C# app.

  5. #5
    Join Date
    Apr 2010
    Location
    .NET 3.5 / VS2008
    Posts
    7

    Re: Window Form don't lose focus

    Thank you for your answer.

    I did put the window in top most and it solves the problem that it is always on top (rest of my windows in the application are normal and the rest of the application installed on the computer (Word/IE/Acrobat Reader) are always under it.
    The problem is getting the keyboard focus when I popup my special window (it isn't triggered by the user - it is triggered by timer).

    When I pop the window I don't get keyboard focus, I tried:
    GetFocus / SetActiveWindow / SetForgroundWindow
    with the events: shown & deactivate.
    And that cause the window to blink in the task bar but the keyboard focus didn't "moved" to my window.

    I didn't try to make a thread that always check it for keyboard focus and on top but I will try this solution - it might work.

    Any idea how to grab keyboard focus ?

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Window Form don't lose focus

    That is how Windows applications are supposed to work. However, in C# you can get around this by setting the Form.WindowState property to WindowState.Normal. I don't know if you will be able to use that in its simplest form, but there is surely some Win32 analog.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured