Disabled form : label color

Stonkie

Well-known member
Joined
Sep 12, 2007
Messages
279
Programming Experience
1-3
I am doing an application which displays a popup preview near the mouse to show some information about the hovered item.

I made the form topmost and borderless, but it kept getting the focus when the preview appeared (preventing mouse wheel events, etc.). To prevent this, I set the form's enabled property to false. However, that sets the color of any text Label to a disabled gray.

I tried setting the Label's ForeColor property back to Black (or the normal system color), and it sets the property, but it has no effect. I also tried setting the label's enabled state to true independently from the owning form, but that has no effect (doesn't even set the property).

I would rather avoid creating a custom control to draw simple text (I actually have one for gradient background effect and stuff) as this preview must load as fast as possible (it already has to access the database and decrypt some images!).

Is there some way to tell the Label not to draw that way? Or is there a better way to make popup previews?
 
Yep, a couple of things to do here. First use ShowWindow API with The SW_NOACTIVATE flag set to show the preview to start with. Or you could even use AnimateWindow to make it all funky.

Then override the forms WndProc and intercept the WM_MOUSEACTIVATE message and set the result to MA_NOACTIVATEANDEAT. This flag will not actviate the form and will also force the form to discard all mouse messages.
 
The ShowWindow with SW_NOACTIVATE worked just fine! thanks!

As for disabling mouse events, I don't think I'll need to because the form is repositioned when the mouse moves on the control (and the popup is hidden when the mouse leaves the control if the user is fast enough to put his mouse on the popup).

If this may help someone else, there is a new property in .NET 2.0 called ShowWithoutActivation which you may override so it returns true (without using any unmanaged referenced) as documented here : Form.ShowWithoutActivation Property (System.Windows.Forms)

Yet, it doesn't work on topmost forms like in my case : https://connect.microsoft.com/Visua...Feedback.aspx?FeedbackID=142514&wa=wsignin1.0

Also, I found in my searches that there is a Popup class in .NET 3.0 and 3.5. It does not exist in .NET 2.0 though so here's my code (that's C#, but the VB is similar) :

VB.NET:
        [DllImport("user32")]
        private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

        private const int SW_NOACTIVATE = 4;

Then a bit further I got a method on my popup form's class that looks like this :

VB.NET:
    Public Sub ShowNoFocus()
        ShowWindow(Me.Handle, SW_NOACTIVATE)
    End Sub

And I inherit from that class when I need a popup and call this ShowNoFocus method instead of simply Show. Note that you cannot use the Visible property to show your form anymore.
 
Back
Top