Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C#

Creating a Form That Doesn't Take Focus

Rate me:
Please Sign up or sign in to vote.
4.50/5 (15 votes)
10 Apr 2010CPOL3 min read 51.2K   3K   58   16
Popup forms that won't take focus even if clicked on, and cannot be alt+tabbed onto
TextBoxWKeyBoardSrc&Demo

Introduction

I recently got interested in creating my own auto-complete drop-down/pop-up boxes. I refuse the method of using floating controls and rather enjoy the look and usefulness of small forms. Unfortunately I found that when used in conjunction with a textbox, there was trouble. When a form is shown, it's activated taking focus away from the textbox. This is NOT at all what I wanted. There are APIs that allow you to show a form without activating it. But that led to the problem of selecting data on the form which would then activate it. And I didn't like that behavior either. I was inspired by the Visual Studio Intellisense auto-complete box. It was a small, sizeable, bordered form with no title bar and no matter what you did, it never gained a focused appearance, nor did it take focus away from the IDE's text editor. THIS! THIS is what I wanted. Searching the net turned up some results, none of which worked correctly, some got close, and the ideas were there. I knew I needed to modify the style and extend style of the window. But what combination would yield the results. Google searching didn't help. Forums were a bust. So after reading the descriptions 100+ times, and a little trial and error, I found the combination.

Background

Back in the world of Win32 programming with C++, you had to manually set styles of windows either when creating them, or by calling the SetWindowLong API in C#, with the .NET forms wrappers built in. We get to enjoy the properties like ShowInTaskbar that perform these calls behind the scenes. But as I have said before in previous articles, these simpler interfaces hide a little capability and so I live to circumvent restrictions. Fortune makes this simple, just override the CreateParams property of the Form. You can preserve your other settings by modifying the existing values, or if you know what you want, you can just manually set them all! But I recommend the latter.

Here is the standard pattern:

C#
protected override CreateParams CreateParams
{
   get
   {
    CreateParams cp = base.CreateParams;
    cp.Style |= //Throw in some values!
    return cp;         
   } 
}

Using the Code

The trick to the Visual Studio style Intellisense form is the WindowsStyles WS_THICKFRAME and WS_CHILD in combination with the Extended Style WS_EX_NOACTIVATE and WS_EX_TOOLWINDOW. This combination gives you the ability to select data on the form using the mouse, but not worry about the form being activated.

C#
protected override CreateParams CreateParams
  {
   get
    {
     CreateParams ret = base.CreateParams;
     ret.Style = (int)Flags.WindowStyles.WS_THICKFRAME | 
		(int)Flags.WindowStyles.WS_CHILD;
     ret.ExStyle |= (int)Flags.WindowStyles.WS_EX_NOACTIVATE | 
		(int)Flags.WindowStyles.WS_EX_TOOLWINDOW;
     return ret;
   }
 } 

Points of Interest

There are a few twists. The Form doesn't always size correctly on start up. Adding size and location to the createparams tends to take care of the problem, but isn't necessary as sizing the form once its show seems to work fine as well.

The source code includes a textbox control that shows an onscreen keyboard when the textbox has focus and features a numeric input mode as well, where a number pad appears and the textbox only accepts digits and a decimal.

It is important to note that WS_EX_NOACTIVATE alone will keep the form from gaining focus, so long as it's the ONLY form in the application. Once another form without that extended style is set is focused, the form will act normal until the other forms are deactivated.

So when creating an app with only 1 form, WS_EX_NOACTIVATE is good enough. And when used as a pop up, it simply isn't enough.

Some of the code in the demo is a modified version of code found on DaniWeb forum, VBAcceorator, and MSDN. Those code snippets belong to their respected authors and is noted in the comments.

History

  • v1 - Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I am a part time programmer, The more I learn, the more I need to learn, the best of which I will share here on codeproject!

Visit my site and blog

Comments and Discussions

 
PraiseWorks Great! Pin
partha chakraborti25-Jul-17 20:20
partha chakraborti25-Jul-17 20:20 
QuestionSame thing under Delphi Pin
Member 413312716-Mar-17 1:07
Member 413312716-Mar-17 1:07 
Questionalso the popup form is shown in taskbar Pin
hambor30-Aug-11 17:42
hambor30-Aug-11 17:42 
QuestionFlags Pin
develophope8-Jul-11 9:20
develophope8-Jul-11 9:20 
GeneralThe popup form is shown in taskbar Pin
danbenedek16-Apr-10 1:25
danbenedek16-Apr-10 1:25 
GeneralRe: The popup form is shown in taskbar Pin
Diamonddrake16-Apr-10 11:09
Diamonddrake16-Apr-10 11:09 
GeneralRe: The popup form is shown in taskbar Pin
danbenedek16-Apr-10 21:22
danbenedek16-Apr-10 21:22 
GeneralRe: The popup form is shown in taskbar Pin
Diamonddrake17-Apr-10 16:12
Diamonddrake17-Apr-10 16:12 
GeneralRe: The popup form is shown in taskbar Pin
danbenedek17-Apr-10 20:53
danbenedek17-Apr-10 20:53 
GeneralRe: The popup form is shown in taskbar Pin
Diamonddrake18-Apr-10 5:33
Diamonddrake18-Apr-10 5:33 
The "windows styles" are just byte flags interpreted by the OS's forms engine. XP has an entirely different kernel than vista and win 7. So its liable to act differently. possibly making sure all your windows updates are up to date with service pack 3 might help. But IDK. I have a project I'm busy with right now but when I get a chance I will do some XP testing and if it doesn't work correctly i will try to solve the issue.
GeneralThanks Pin
Vijay Gill11-Apr-10 22:04
professionalVijay Gill11-Apr-10 22:04 
GeneralUsefull Pin
Artem S. Dmitriev10-Apr-10 6:51
Artem S. Dmitriev10-Apr-10 6:51 
GeneralAttachment link broken Pin
knoami9-Apr-10 22:41
knoami9-Apr-10 22:41 
GeneralRe: Attachment link broken Pin
reflex@codeproject10-Apr-10 3:38
reflex@codeproject10-Apr-10 3:38 
GeneralRe: Attachment link broken Pin
Diamonddrake10-Apr-10 6:17
Diamonddrake10-Apr-10 6:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.