Taskbar behavior and appearance

M

michael sorens

(1) By setting an icon in my Windows Form properties, I successfully
display my custom icon when my application is executed. However, as soon
as I open the Options form in my application--now having two windows
open--they are grouped on the taskbar, but with a generic icon. Both my
main form and my Option form are using the same icon. How can I use my
custom icon instead of the generic icon in a .Net 2.0 C# application?

(2) A standard Windows behavior is that TaskBar items will flash when
requesting user attention. How do I create this behavior (make it flash)?
 
L

Linda Liu [MSFT]

Hi Michael,
Thank you for posting.
My answers to your two questions are in the below.
(1) In order to use your custom icon in a winform, you should set the icon
property of each winform to your custom icon separately. There isn't a
global property for a winform project to set winform icon to your custom
icon.

(2) There're two WinAPI functions you can use to enable a Taskbar item
flash when requesting user attention. They are GetForgroundWindow and
FlashWindow.
The GetForegroundWindow function returns the handle to the foreground
window ¡ª the window with which the user is currently working.And the
FlashWindow function flashes the specified window one time. It does not
change the active state of the window.

The syntax of GetForegroundWindow function is:
HWND GetForegroundWindow(VOID);
The return value is a handle to the foreground window.

And the syntax of FlashWindow function is:
BOOL FlashWindow( HWND hWnd, BOOL bInvert);
Parameters:
hWnd
Handle to the window to be flashed. The window can be either open or
minimized.
bInvert
If this parameter is TRUE, the window is flashed from one state to the
other. If it is FALSE, the window is returned to its original state (either
active or inactive).

Return Values:
The return value specifies the window's state before the call to the
FlashWindow function. If the window caption was drawn as active before the
call, the return value is nonzero. Otherwise, the return value is zero.

Since the specified window flashes only once using FlashWindow function,
you should call this function in a timer's tick event handling method in
the specified window.
The following is a sample.
// state the two API functions in Class1
public class Class1
{
[DllImport("user32.dll")]
public extern static bool FlashWindow(IntPtr hwnd,bool bInvert);
[DllImport("user32.dll")]
public extern static IntPtr GetForegroundWindow();
}

public class Form1
{
private System.Windows.Forms.Timer timer1;
public Form1()
{
this.timer1 = new
System.Windows.Forms.Timer(this.components);
this.timer1.Interval = 1000;
this.timer1.Tick += new
System.EventHandler(this.timer1_Tick);
}

private void Form1_Load(object sender, System.EventArgs e)
{
this.timer1.Start();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
IntPtr foregroundWin = Class1.GetForegroundWindow();
// if the current foreground window isn't
this window, flash this window in task bar once every 1 second
if (foregroundWin != this.Handle)
{
Class1.FlashWindow(this.Handle,true);
}
// if this window is the current foreground
window, stop the timer and don't flash this window any more
else
{
this.timer1.Stop();
}
}
}

I hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to tell me.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
M

michael sorens

You misunderstood my first question. Let me restate it. I know that each
winform must have the icon property set and I have done that. Let me
itemize what works and what does not in my application. I have a main
winform and a child winform (as in an Options window):

In the winforms themselves:
Main winform -- icon OK
Child winform -- icon OK

On the taskbar
Main winform by itself -- icon OK
Main winform and child winform open, but not grouped on taskbar -- icon OK
on both
Main winform and child winform open, grouped on taskbar -- icon BAD (i.e.
generic icon for the group), but clicking on the group icon opens a
"dropdown" showing the individual winforms, both of which have the correct
icon
 
L

Linda Liu [MSFT]

Hi Michael,

Thank you for your reply. I am sorry that I misunderstood your first
question.Now I see.

If the main form and the child form open and are grouped on taskbar, the
group icon displayed in the taskbar is the icon of the project.
You can set the project icon using the following steps.
1. Open the project property pages(To open the property pages, select the
menu Project and Properties item, or right-click the project in the
Solution Explorer and select Properties item)
2. Select Common Properties in the left panel and select General item.
There's a Application Icon item in Application group in the right panel.
You can set the Application Icon to your custom icon.

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to tell me.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
L

Linda Liu [MSFT]

Hi Michael,

You're welcome! It is always our pleasure to be of assistance.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 

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