Click here to Skip to main content
Click here to Skip to main content

Creating a Form That Doesn't Take Focus

, 10 Apr 2010
Rate this:
Please Sign up or sign in to vote.
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:

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.

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)

Share

About the Author

Diamonddrake
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

 
Questionalso the popup form is shown in taskbar Pinmemberhambor30-Aug-11 17:42 
QuestionFlags Pinmemberdevelophope8-Jul-11 9:20 
GeneralThe popup form is shown in taskbar Pinmemberdanbenedek16-Apr-10 1:25 
GeneralRe: The popup form is shown in taskbar PinmemberDiamonddrake16-Apr-10 11:09 
GeneralRe: The popup form is shown in taskbar Pinmemberdanbenedek16-Apr-10 21:22 
GeneralRe: The popup form is shown in taskbar PinmemberDiamonddrake17-Apr-10 16:12 
GeneralRe: The popup form is shown in taskbar Pinmemberdanbenedek17-Apr-10 20:53 
GeneralRe: The popup form is shown in taskbar PinmemberDiamonddrake18-Apr-10 5:33 
GeneralThanks Pinmemberpappukanghi11-Apr-10 22:04 
GeneralUsefull Pinmembermartyn_mcfly10-Apr-10 6:51 
GeneralAttachment link broken Pinmemberknoami9-Apr-10 22:41 
GeneralRe: Attachment link broken Pinmemberreflex@codeproject10-Apr-10 3:38 
GeneralRe: Attachment link broken PinmemberDiamonddrake10-Apr-10 6:17 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 10 Apr 2010
Article Copyright 2010 by Diamonddrake
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid