.NET Framework - making a window topmost in the system (similar to Task Manager)
Asked By jake
07-Aug-09 05:03 AM
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 cannot 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
Windows Installer
(1)
SetWindowPos
(1)
ShowBalloonTip
(1)
NoActivateForm
(1)
BringToFront
(1)
Activate
(1)
Wilson, Phil replied...
This is not as easy as it appears, and according to some is the wrong thing
to do anyway.
http://blogs.msdn.com/oldnewthing/archive/2009/02/20/9435239.aspx
http://blogs.msdn.com/oldnewthing/archive/2007/07/27/4072156.aspx
http://blogs.msdn.com/oldnewthing/archive/2008/09/22/8960761.aspx
--
Phil Wilson
The Definitive Guide to Windows Installer
http://www.apress.com/book/view/1590592972
Scott Seligman replied...

Here is a sample app that does what you are 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 Seligman <scott at <firstname> and michelle dot net> ---------
One drink is just right. Two is too many, and three is not enough.
-- James Thurber
con-man-jake replied...

g
1590592972
o
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 will 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 will 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
con-man-jake replied...
_Click);
String();
-----
Scott,
Your example works exactly as it is intended and it is exactly what I
want. I just do not 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
Wilson, Phil replied...

I am 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 will 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 will 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
ContentAlignment (1) GraphicsUnit (1) EventHandler (1) FontStyle (1) CheckBox (1) TextBox (1) Is this a Windows Forms application? If so, there ishould be a region titled is not totally packed with InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(InvoiceForm)); this.textBox17 = new System.Windows.Forms.TextBox(); this.textBox15 = new System.Windows.Forms.TextBox(); this.textBox7 = new System.Windows.Forms.TextBox(); this.textBox6 = new System.Windows.Forms.TextBox(); this.textBox5 = new System.Windows.Forms.TextBox(); this.textBox4 = new System Windows.Forms.TextBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox1 = new System.Windows.Forms
Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using WIA; using System.Runtime.InteropServices; using System.Configuration; namespace Scan { public partial class ToFileTime().ToString(); string FileName = "c: \ tempimage_" + scanTime + ".jpg"; img[ScanCount].SaveFile(FileName); C# Discussions System.Windows.Forms.ButtonBase.WndProc (1) System.Windows.Forms.Control.WmMouseUp (1) System.Windows.Forms.Button.OnMouseUp (1) System.Windows.Forms.Control.WndProc (1) System.Windows.Forms.Control.OnClick (1) System.Windows.Forms.Button.WndProc (1) System Windows.Forms.Button.OnClick (1) System.Threading.ThreadHelper.ThreadStart (1) Hello, I know that WIA is
thrown at: System.NullReferenceException: Object reference not set to an instance of an object. System.Windows.Forms.dll! System.Windows.Forms.Control.InvokeMarshaledCallbackDo (System.Windows.Forms.Control.ThreadMethodEntry tme) + 0x1a5 bytes System.Windows.Forms.dll! System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(object obj) + 0x69 bytes mscorlib.dll!System.Threading.ExecutionContext.runTryCode(object userData ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x45 bytes System.Windows.Forms.dll! System.Windows.Forms.Control.InvokeMarshaledCallback (System.Windows.Forms.Control.ThreadMethodEntry tme) + 0x69 bytes System.Windows.Forms.dll! System.Windows.Forms.Control.InvokeMarshaledCallbacks
PropertyBag.PropertySpecDescriptor.GetValue(object component = {TERA.Controls.Tree.PropertyBag}) Line 796 + 0xe bytes C# System.Windows.Forms.dll!System.Windows.Forms.PropertyGridInternal.GridEntry.GetPropEntries(System.Windows.Forms.PropertyGridInternal.GridEntry peParent = {System.Windows.Forms.PropertyGridInternal.SingleSelectRootGridEntry TERA.Controls.Tree.PropertyBag}, object obj = {TERA.Controls.Tree.PropertyBag}, System.Type objType) + 0x38c bytes System.Windows.Forms.dll!System.Windows.Forms.PropertyGridInternal.GridEntry.CreateChildren(bool diffOldChildren = false) + 0xc6 bytes System.Windows.Forms.dll!System.Windows.Forms.PropertyGridInternal.SingleSelectRootGridEntry.CreateChildren() + 0x13 bytes System.Windows.Forms.dll!System.Windows.Forms.PropertyGridInternal.GridEntry
VS 2003 as follows. . . A first chance exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll Additional information: Object reference not set to an instance of an object. the call stack at this time looks like- (Word-wrap may make this look nasty) system.windows.forms.dll! System.Windows.Forms.ListView.ListViewItemCollection.get_Item(int displayIndex = 0x97) + 0x147 bytes system.windows.forms.dll! System.Windows.Forms.ListView.ListViewItemCollection.CopyTo (System.Array dest = {System.Array}, int index = 0x97) + 0x30 bytes system windows.forms.dll! System.Windows.Forms.ListView.OnHandleDestroyed(System.EventArgs e = {System.EventArgs}) + 0x12f bytes system.windows.forms.dll!System.Windows.Forms.Control.WmDestroy (System.Windows.Forms.Message m = {System.Windows.Forms