tags:

views:

117

answers:

4

I am writing an IM program, and I have the method to make a form flash and stop flashing... question is, how do I implement it?

When a message arrives, I can set the window flashing, but I need to make sure it doesn't have focus. Checking the focued method always seems to return false and so it flashes even when the form is open.

Also, which event to I need to handle to stop it flashing? When the user clicks the form to make it maximise, or switches focus to the form, I need a way of stopping it.

What's the best way?

A: 

Checking if the form is minimized or not:

if (this.WindowState == FormWindowState.Minimized)
{
    MakeFormFlash();
}
else
{
    MakeFormStopFlash();
}

Event to trigger when the form is activated by user or code:

this.Activated += new EventHandler(Form_Activated);
Phoexo
This doesn't at all mitigate the problems the OP mentions with Form focusing.
Joey
Doesn't it? Can you explain OP's problem if I have misunderstood it?
Phoexo
As far as I understood it, (a) the flashing needs to stop when activating the window (this might be done already by the window manager) and (b) it should flash when unfocused, instead of only when minimized.
Joey
+1  A: 

You can handle the Activated and Deactivate events of your Form, and use them to change a Form-level boolean that will tell your code whether your form has the focus or not, like this:

private bool _IsActivated = false;
private void Form1_Activated(object sender, EventArgs e)
{
    _IsActivated = true;
    // turn off flashing, if necessary
}
private void Form1_Deactivate(object sender, EventArgs e)
{
    _IsActivated = false;
}

When a message arrives, you check _IsActivated to determine if your Form is already the active window, and turn on flashing if it isn't. In the Activated event, you would turn off the flashing if it's on.

The Focused property of your form will always return false if it has any controls on it. This property refers to whether the control in question (the form, in this case) has the focus within your application's form, not whether the application itself has the focus within Windows.

MusiGenesis
Theres no need to create a private variable for the activate. Form.ActiveForm will give you the current active form for the application.
James
@James: I would still use a private variable in this case. Otherwise, you have to do something like "if (Form.ActiveForm == this) {}" to determine if your application has the Windows focus. It's half of one, six dozen of the other.
MusiGenesis
Or you could just create a public readonly property of the form called IsActivated and the result is exactly the code you mentioned :)
James
@James: zzzzzzzz. :)
MusiGenesis
A: 

Well Focused should be the property to check, so you need to try and work out why that is always returning false.

As for what event to listen to, probably the GotFocus event, though that may not work until you can work out what is wrong with the Focused property.

ICR
A: 

There are a number of ways you can handle this. Probably the easiest would be to have a flag that you set whenever the form is flashing so this can be reset on re-activation of the form e.g.

Code for base IM window form

private bool IsFlashing;
....

// Code for IM windows
public void OnActivate(EventArgs e)
{
    if (IsFlashing)
    {
        // stop flash
        IsFlashing = false;
    }
}

public void Flash()
{
    // make flash
    IsFlashing = true;
}

Then wherever you do your code to handle the new message you would just need to check that the particular conversation window (if you handle multiple ones) that the message is directed at is the current active one:

public void OnNewMessage(AMessage msg)
{
    Form convoWindow = FindConvoWindow(msg.Sender);
    if (Form.ActiveForm == convoWindow)
    {
        // update the conversation text
    }
    else
    {
        convoWindow.Flash();
    }
}
James