making a window topmost in the system (similar to Task Manager)

J

jake

I need to make a window topmost in the system even if other apps are
active on top of mine. The window should not have focus (I managed to
do that much) but try as I may, I can't make it come up on top of
other apps. I tried setting the form's TopMost property to true,
BringToFront() method, SetWindowPos() API and a couple of other
things, but still fruitless.
Any help will be greatly appreciated.
jake
 
S

Scott Seligman

jake said:
I need to make a window topmost in the system even if other apps are
active on top of mine. The window should not have focus (I managed to
do that much) but try as I may, I can't make it come up on top of
other apps.

Here's a sample app that does what you're after (the buttons just
increment a number to show that you can click on them without giving
the form focus from whoever has focus).

namespace SampleApp
{
class NoActivateForm : Form
{
const int WS_EX_NOACTIVATE = 0x8000000;

public NoActivateForm()
{
TopMost = true;
Text = "No Activate Form";

for (int i = 0; i < 10; i++)
{
Button button = new Button();
button.Text = "0";
button.Click += new EventHandler(Button_Click);
button.Top = button.Height * i;
Controls.Add(button);
}
}

void Button_Click(object sender, EventArgs e)
{
((Button)sender).Text =
(int.Parse(((Button)sender).Text) + 1).ToString();
}

protected override CreateParams CreateParams
{
get
{
CreateParams ret = base.CreateParams;
ret.ExStyle |= WS_EX_NOACTIVATE;
return ret;
}
}
}

static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new NoActivateForm());
}
}
}
 
C

con-man-jake


Phil,
Maybe I missed something, but while the three links are actually very
useful to read, they, however, describe stealing and shifting focus
back and forth and the proper way to send the parent handle to a modal
dialog box. My application has neither a modal window nor does it
need focus. My fault though, my written word is not as clear as it is
in my mind. But I'll give another shot.
I am trying to create an alert box that fades in and out at the bottom-
right corner of the screen when a certain business event occurs. It
should stay on top of whatever application that is running at the time
but not steal the focus away from the running app. Much the same way
Thunderbird (Firefox's email client program) alerts the user of new
messages. I managed to do everything (including not stealing the
focus) except that the alert box does not come up on top of other
windows.
The reference to the Task Manager (with its default settings) was only
there to give an example of how I need the alert window to display,
i.e. on top of other windows even if it has no focus.
But I'll read the pages at the end of these links again to make doubly
sure that I did not miss my solution that may be hidden there
somewhere.
Regards,
jake
 
C

con-man-jake

jake said:
I need to make a window topmost in the system even if other apps are
active on top of mine.  The window should not have focus (I managed to
do that much) but try as I may, I can't make it come up on top of
other apps.

Here's a sample app that does what you're after (the buttons just
increment a number to show that you can click on them without giving
the form focus from whoever has focus).

namespace SampleApp
{
    class NoActivateForm : Form
    {
        const int WS_EX_NOACTIVATE = 0x8000000;

        public NoActivateForm()
        {
            TopMost = true;
            Text = "No Activate Form";

            for (int i = 0; i < 10; i++)
            {
                Button button = new Button();
                button.Text = "0";
                button.Click += new EventHandler(Button_Click);
                button.Top = button.Height * i;
                Controls.Add(button);
            }
        }

        void Button_Click(object sender, EventArgs e)
        {
            ((Button)sender).Text =
                (int.Parse(((Button)sender).Text) + 1).ToString();
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams ret = base.CreateParams;
                ret.ExStyle |= WS_EX_NOACTIVATE;
                return ret;
            }
        }
    }

    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new NoActivateForm());
        }
    }

}

Scott,
Your example works exactly as it is intended and it is exactly what I
want. I just don't seem to be able to make it work in my application
for some reason. I tried your example (as I said) and it works like a
charm. I implemented the same idea into my application, to no avail.
But, I will hammer at it until I get it to work.
My solution is in your example somewhere. I just need to make sure I
am doing it properly.
Thank you very much.
Regards,
jake
 
W

Wilson, Phil

I'm thinking you should be looking at NotifyIcon, balloons, balloon tips
instead of a Window. Something like NotifyIcon's ShowBalloonTip (fade out
time).
--
Phil Wilson
The Definitive Guide to Windows Installer
http://www.apress.com/book/view/1590592972



Phil,
Maybe I missed something, but while the three links are actually very
useful to read, they, however, describe stealing and shifting focus
back and forth and the proper way to send the parent handle to a modal
dialog box. My application has neither a modal window nor does it
need focus. My fault though, my written word is not as clear as it is
in my mind. But I'll give another shot.
I am trying to create an alert box that fades in and out at the bottom-
right corner of the screen when a certain business event occurs. It
should stay on top of whatever application that is running at the time
but not steal the focus away from the running app. Much the same way
Thunderbird (Firefox's email client program) alerts the user of new
messages. I managed to do everything (including not stealing the
focus) except that the alert box does not come up on top of other
windows.
The reference to the Task Manager (with its default settings) was only
there to give an example of how I need the alert window to display,
i.e. on top of other windows even if it has no focus.
But I'll read the pages at the end of these links again to make doubly
sure that I did not miss my solution that may be hidden there
somewhere.
Regards,
jake
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top