Click here to Skip to main content
Licence 
First Posted 2 Aug 2004
Views 285,042
Downloads 4,780
Bookmarked 202 times

NotifyWindow: A different MSN Messenger style notification window

By | 2 Aug 2004 | Article
Another MSN Messenger-like notification window, this one does its own drawing

Introduction

NotifyWindow displays an MSN Messenger-like notification window. If you want to display your own images in the notification window, you may prefer to use John O'Byrne's TaskbarNotifer. NotifyWindow may be easier to use if you just intend to display text. Animations are used in opening and closing the NotifyWindow, and the window is displayed TopMost while not stealing focus. The window will be shown for a default of 11 seconds, although this can be changed. NotifyWindow does all of its own drawing, so no extra image files are required.

Using the code

It is pretty simple to display text using NotifyWindow.

// Display the text "This is a sample NotifyWindow"
NotifyWindow nw = new NotifyWindow ("This is a sample NotifyWindow");
nw.Notify();

// The following two lines of code will display a window that 
// looks exactly like the one shown at the beginning of this article.
NotifyWindow nw = new NotifyWindow ("NotifyWindow", 
  "This is a sample notification created with NotifyWindow");
nw.Notify();

If desired, a variety of other options can be changed - such as fonts and colors. The included TestNotifyWindow application will let you play around with a few of the settings, but the code displayed here should serve as a more complete reference.

NotifyWindow nw = new NotifyWindow();

nw.Text = "This is the NotifyWindow text";
nw.Title = "Title Text";

// Change the background style.  Other valid 
// styles are Solid, VerticalGradient,
// HorizontalGradient and BackwardDiagonalGradient  
// (Default: VerticalGradient)
nw.BackgroundStyle = NotifyWindow.BackgroundStyles.ForwardDiagonalGradient;

// Change the background colors  
// (Default: BackColor=SteelBlue, GradientColor=WhiteSmoke)
nw.BackColor = Color.SpringGreen;
nw.GradientColor = Color.White;

// Change the text and title colors.  (Default: ControlText)
nw.TextColor = Color.Blue;
nw.TitleColor = Color.Black;

// Change the color displayed when the text is pressed.  (Default: Gray)
nw.PressedColor = Color.Red;

// Use non-default fonts.  If TitleFont is not set 
// by the user, nw.Font will be used.
nw.Font = new Font ("Tahoma", 8.25F);
nw.TitleFont = new Font ("Tahoma", 8.25F, FontStyle.Bold);

// Change NotifyWindow size.  (Default: 130, 110)
nw.SetDimensions (nwWidth, nwHeight);

// Do not close the NotifyWindow if the mouse 
// cursor is over the window.  (Default: true)
nw.WaitOnMouseOver = true;

// Set up an EventHandler to be called if the text or title are clicked.
nw.TextClicked += new System.EventHandler (nwTextClicked);
nw.TitleClicked += new System.EventHandler (nwTitleClicked);

// Display the window for 20 seconds, or 20000ms.  (Default: 11000ms)
nw.WaitTime = 20000;

// Now show the NotifyWindow.
nw.Notify();

This is how the NotifyWindow created with the above code will look:

Programmers can also use their own Blend (for the background) or StringFormat (which will be used when Text and Title are drawn) if desired by setting the nw.Blend and nw.StringFormat variables.

Points of Interest

NotifyWindow is unique because it does all of its own drawing. The background is drawn using Graphics.FillRectangle with either a LinearGradientBrush (default) or a SolidBrush. The borders are drawn using a series of calls to Graphics.DrawRectangle and Graphics.DrawLine. On Windows XP or higher systems with Visual Styles enabled, the close button is drawn using DrawThemeBackground() from UxTheme.dll - otherwise, ControlPaint.DrawCaptionButton is used.

An obstacle faced in both this and similar applications has been displaying the window on top without stealing focus. Both Form.Show() and setting TopMost = true individually activate the form, which steals focus. We get around this by calling ShowWindow() and SetWindowPos() with arguments instructing the operating system not to activate the window.

const Int32 HWND_TOPMOST = -1;
const Int32 SWP_NOACTIVATE = 0x0010;
const Int32 SW_SHOWNOACTIVATE = 4;
[DllImport ("user32.dll")]
protected static extern bool ShowWindow (IntPtr hWnd, Int32 flags);
[DllImport ("user32.dll")]
protected static extern bool SetWindowPos (IntPtr hWnd, 
  Int32 hWndInsertAfter, Int32 X, Int32 Y, Int32 cx, Int32 cy, uint uFlags);

...

// Show the window without activating it.
ShowWindow (this.Handle, SW_SHOWNOACTIVATE);

// Equivalent to setting TopMost = true, except don't activate the window.
SetWindowPos (this.Handle, HWND_TOPMOST, Left, Top, Width, Height, SWP_NOACTIVATE);

A similar NotifyWindow class was originally implemented for an open-source project called ChronosXP. When it became apparant that others might like to use this code outside of that project, it was modified, removing the ChronosXP-specific parts and making it more generic.

A class called NotifyWindow2000 is included with the distribution that will display the NotifyWindow indefinitely until there is mouse or keyboard activity, similar to balloon windows. It uses SetWindowsHookEx() with WH_KEYBOARD_LL/WH_MOUSE_LL to detect user activity, so it will only work with Windows 2000 or higher. If anyone knows how to do this on older versions of Windows I would like to hear about it.

History

  • Initial coding: July 28, 2004

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Robert Misiak

Web Developer

United States United States

Member

Robert Misiak is a C# developer, working at a credit card company, in Las Vegas, NV.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionNotifyWindows PinmemberPunisher937:32 5 Mar '12  
QuestionHeartly thank you PinmemberAnil B. Patil18:16 22 Feb '12  
GeneralMy vote of 5 Pinmemberamitmane55523:41 27 Feb '11  
GeneralNotification window won't close Pinmembermike Svidron6:12 30 Apr '10  
QuestionHow can i just show notification window not form window Pinmembername3222:56 1 Apr '10  
GeneralQuestion PinmemberBenjamin Unanka6:42 24 Jul '08  
QuestionVB.Net version? Pinmembercal192:08 18 Apr '08  
AnswerRe: VB.Net version? Pinmemberrzeylah0:29 6 Aug '08  
GeneralRe: VB.Net version? Pinmemberpaladinkerb9:01 30 Mar '11  
GeneralUnable to download NotifyWindow PinmemberGlauter4:12 7 Mar '08  
GeneralNotifyWindow on Vista Pinmemberpogarek22:13 15 Feb '07  
GeneralRe: NotifyWindow on Vista PinmemberRobert Misiak8:19 10 Jul '07  
QuestionHow to Add a Control Pinmemberraj_32720:30 22 Jan '07  
GeneralShow two NotifyWindows above each other PinmemberLpH19810:41 26 May '06  
GeneralRe: Show two NotifyWindows above each other Pinmemberronaldovn21:17 17 Mar '10  
GeneralRe: Show two NotifyWindows above each other PinmemberLucas heezen0:59 18 Mar '10  
GeneralRe: Show two NotifyWindows above each other Pinmemberronaldovn16:24 18 Mar '10  
GeneralRe: Show two NotifyWindows above each other PinmemberDekkie4:52 17 Feb '11  
QuestionUsing notify window in a ThreadFunction Pinmemberjis2:36 17 May '06  
AnswerRe: Using notify window in a ThreadFunction PinmemberRobert Misiak3:19 17 May '06  
GeneralRe: Using notify window in a ThreadFunction Pinmemberjis18:41 17 May '06  
GeneralRe: Using notify window in a ThreadFunction PinmemberManjit Sooch13:33 20 Jul '06  
GeneralRe: Using notify window in a ThreadFunction Pinmembershane19859:21 27 Sep '07  
GeneralRe: Using notify window in a ThreadFunction Pinmemberlxiongh4:03 3 Sep '09  
GeneralStacking windows code Pinmemberjdmwood0:13 29 Mar '06  
Hi there,
 
Nice code! Thanks very much. I have just amended it to allow stacking of messages above each other (i.e. upwards towards the top of the screen) like Messenger does
 
Apologies for the cut n' paste (not sure how else to send it.)
 
John Wood
 
// NotifyWindow.cs
// Copyright © 2004 by Robert Misiak
// All Rights Reserved.
//
// Permission is granted to use, modify and distribute this code, as long as credit is given to the original author, and the copyright notice
// is retained.
//
// Based on a similar implementation used in ChronosXP, an open-source project: http://chronosxp.sourceforge.net
//
// JW 29/03/2006: Added code to allow multiple windows to show in a stacked layout, each
// higher up than the other so they don't overlay each other anymore. This is
// how MSN Messenger shows them.
//
// Also there is a parameter to prevent ignore calls if there are already
// lots of windows being shown.
 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Reflection;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
namespace NotifyWindow
{
///
/// Display An MSN-Messenger-Style NotifyWindow.
///

public class NotifyWindow : System.Windows.Forms.Form
{

//Used to keep track of which windows are active
//This stores NotifyWindow objects.
static System.Collections.ArrayList activeWindows = new System.Collections.ArrayList();
 
//JW: The max number of windows to display at once
public int MaxActiveWindows= 6; //6 is about right for my screen
 
#region Public Variables
///
/// Gets or sets the title text to be displayed in the NotifyWindow.
///

public string Title;
///
/// Gets or sets the Font used for the title text.
///

public System.Drawing.Font TitleFont;
///
/// Gets or sets the Font used when the mouse hovers over the main body of text.
///

public System.Drawing.Font HoverFont;
///
/// Gets or sets the Font used when the mouse hovers over the title text.
///

public System.Drawing.Font TitleHoverFont;
///
/// Gets or sets the style used when drawing the background of the NotifyWindow.
///

public BackgroundStyles BackgroundStyle;
///
/// Gets or sets the Blend used when drawing a gradient background for the NotifyWindow.
///

public System.Drawing.Drawing2D.Blend Blend;
///
/// Gets or sets the StringFormat used when drawing text in the NotifyWindow.
///

public System.Drawing.StringFormat StringFormat;
///
/// Gets or sets a value specifiying whether or not the window should continue to be displayed if the mouse cursor is inside the bounds
/// of the NotifyWindow.
///

public bool WaitOnMouseOver;
///
/// An EventHandler called when the NotifyWindow main text is clicked.
///

public event System.EventHandler TextClicked;
///
/// An EventHandler called when the NotifyWindow title text is clicked.
///

public event System.EventHandler TitleClicked;
///
/// Gets or sets the color of the title text.
///

public System.Drawing.Color TitleColor;
///
/// Gets or sets the color of the NotifyWindow main text.
///

public System.Drawing.Color TextColor;
///
/// Gets or sets the gradient color which will be blended in drawing the background.
///

public System.Drawing.Color GradientColor;
///
/// Gets or sets the color of text when the user clicks on it.
///

public System.Drawing.Color PressedColor;
///
/// Gets or sets the amount of milliseconds to display the NotifyWindow for.
///

public int WaitTime;
///
/// Gets or sets the full height of the NotifyWindow, used after the opening animation has been completed.
///

public int ActualHeight;
///
/// Gets or sets the full width of the NotifyWindow.
///

public int ActualWidth;
 
public enum BackgroundStyles { BackwardDiagonalGradient, ForwardDiagonalGradient, HorizontalGradient, VerticalGradient, Solid };
public enum ClockStates { Opening, Closing, Showing, None };
public ClockStates ClockState;
#endregion
 
#region Protected Variables
protected bool closePressed = false, textPressed = false, titlePressed = false, closeHot = false, textHot = false, titleHot = false;
protected Rectangle rClose, rText, rTitle, rDisplay, rScreen, rGlobClose, rGlobText, rGlobTitle, rGlobDisplay;
protected System.Windows.Forms.Timer viewClock;
#endregion
 
#region Constructor
/// Title text displayed in the NotifyWindow
/// Main text displayedin the NotifyWindow
public NotifyWindow (string title, string text) : this() { Title = title; Text = text; }
/// Text displayed in the NotifyWindow
public NotifyWindow (string text) : this() { Text = text; }
public NotifyWindow()
{
SetStyle (ControlStyles.UserMouse, true);
SetStyle (ControlStyles.UserPaint, true);
SetStyle (ControlStyles.AllPaintingInWmPaint, true); // WmPaint calls OnPaint and OnPaintBackground
SetStyle (ControlStyles.DoubleBuffer, true);
 
ShowInTaskbar = false;
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
StartPosition = System.Windows.Forms.FormStartPosition.Manual;
 
// Default values
BackgroundStyle = BackgroundStyles.VerticalGradient;
ClockState = ClockStates.None;
BackColor = Color.SteelBlue;
GradientColor = Color.WhiteSmoke;
PressedColor = Color.Gray;
TitleColor = SystemColors.ControlText;
TextColor = SystemColors.ControlText;
WaitOnMouseOver = true;
ActualWidth = 130;
ActualHeight = 110;
WaitTime = 11000;
}
#endregion
 
#region Public Methods
///
/// Sets the width and height of the NotifyWindow.
///

public void SetDimensions (int width, int height)
{
ActualWidth = width;
ActualHeight = height;
}
 

//JW: How much to adjust the height by (allows stacking of boxes)
private int heightAdjustment = 0;
 
///
/// Displays the NotifyWindow.
///

public void Notify()
{
//JW: Prevent multiple displays
if (activeWindows.Count > MaxActiveWindows)
{
//Should we dispose now?
this.Dispose(true);
return;
}
 
if (Text == null || Text.Length < 1)
throw new System.Exception ("You must set NotifyWindow.Text before calling Notify()");
 
Width = ActualWidth;
rScreen = Screen.GetWorkingArea (Screen.PrimaryScreen.Bounds);
Height = 0;
Top = rScreen.Bottom;
Left = rScreen.Width - Width - 11;
 
//JW: Work out the stacking adjustment
//{{{
heightAdjustment = activeWindows.Count * this.ActualHeight;
Top -= heightAdjustment;
 
//Record that the window exists in the static member
activeWindows.Add(this);
//}}}

 
if (HoverFont == null)
HoverFont = new Font (Font, Font.Style | FontStyle.Underline);
if (TitleFont == null)
TitleFont = Font;
if (TitleHoverFont == null)
TitleHoverFont = new Font (TitleFont, TitleFont.Style | FontStyle.Underline);
if (this.StringFormat == null)
{
this.StringFormat = new StringFormat();
this.StringFormat.Alignment = StringAlignment.Center;
this.StringFormat.LineAlignment = StringAlignment.Center;
this.StringFormat.Trimming = StringTrimming.EllipsisWord;
}
 
rDisplay = new Rectangle (0, 0, Width, ActualHeight);
rClose = new Rectangle (Width - 21, 10, 13, 13);
 
int offset;
if (Title != null)
{
using (Graphics fx = CreateGraphics())
{
SizeF sz = fx.MeasureString (Title, TitleFont, ActualWidth - rClose.Width - 22, this.StringFormat);
rTitle = new Rectangle (11, 12, (int) Math.Ceiling (sz.Width), (int) Math.Ceiling (sz.Height));
offset = (int) Math.Max (Math.Ceiling (sz.Height + rTitle.Top + 2), rClose.Bottom + 5);
}
}
else
{
offset = rClose.Bottom + 1;
rTitle = new Rectangle (-1, -1, 1, 1);
}
 
rText = new Rectangle (11, offset, ActualWidth - 22, ActualHeight - (int)(offset * 1.5));
// rGlob* are Rectangle's Offset'ed to their actual position on the screen, for use with Cursor.Position.
rGlobClose = rClose;
rGlobClose.Offset (Left, rScreen.Bottom - ActualHeight);
rGlobText = rText;
rGlobText.Offset (Left, rScreen.Bottom - ActualHeight);
rGlobTitle = rTitle;
if (Title != null)
rGlobTitle.Offset (Left, rScreen.Bottom - ActualHeight);
rGlobDisplay = rDisplay;
rGlobDisplay.Offset (Left, rScreen.Bottom - ActualHeight);
rGlobClose = rClose;
rGlobClose.Offset (Left, rScreen.Bottom - ActualHeight);
rGlobDisplay = rDisplay;
rGlobDisplay.Offset (Left, rScreen.Bottom - ActualHeight);
 
// Use unmanaged ShowWindow() and SetWindowPos() instead of the managed Show() to display the window - this method will display
// the window TopMost, but without stealing focus (namely the SW_SHOWNOACTIVATE and SWP_NOACTIVATE flags)
ShowWindow (Handle, SW_SHOWNOACTIVATE);

//JW Note heightAdjustment
//was:
// SetWindowPos (Handle, HWND_TOPMOST, rScreen.Width - ActualWidth - 11, rScreen.Bottom, ActualWidth, 0, SWP_NOACTIVATE);
SetWindowPos (Handle, HWND_TOPMOST, rScreen.Width - ActualWidth - 11, rScreen.Bottom - heightAdjustment, ActualWidth, 0, SWP_NOACTIVATE);
 
viewClock = new System.Windows.Forms.Timer();
viewClock.Tick += new System.EventHandler (viewTimer);
viewClock.Interval = 1;
viewClock.Start();
 
ClockState = ClockStates.Opening;
}
#endregion
 
#region Drawing
protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
{
// Draw the close button and text.
drawCloseButton (e.Graphics);
 
Font useFont; Color useColor;
if (Title != null)
{
if (titleHot)
useFont = TitleHoverFont;
else
useFont = TitleFont;
if (titlePressed)
useColor = PressedColor;
else
useColor = TitleColor;
using (SolidBrush sb = new SolidBrush (useColor))
e.Graphics.DrawString (Title, useFont, sb, rTitle, this.StringFormat);
}
 
if (textHot)
useFont = HoverFont;
else
useFont = Font;
if (textPressed)
useColor = PressedColor;
else
useColor = TextColor;
using (SolidBrush sb = new SolidBrush (useColor))
e.Graphics.DrawString (Text, useFont, sb, rText, this.StringFormat);
}
 
protected override void OnPaintBackground (System.Windows.Forms.PaintEventArgs e)
{
// First paint the background
if (BackgroundStyle == BackgroundStyles.Solid)
{
using (SolidBrush sb = new SolidBrush (BackColor))
e.Graphics.FillRectangle (sb, rDisplay);
}
else
{
LinearGradientMode lgm;
switch (BackgroundStyle)
{
case BackgroundStyles.BackwardDiagonalGradient:
lgm = LinearGradientMode.BackwardDiagonal;
break;
case BackgroundStyles.ForwardDiagonalGradient:
lgm = LinearGradientMode.ForwardDiagonal;
break;
case BackgroundStyles.HorizontalGradient:
lgm = LinearGradientMode.Horizontal;
break;
default:
case BackgroundStyles.VerticalGradient:
lgm = LinearGradientMode.Vertical;
break;
}
using (LinearGradientBrush lgb = new LinearGradientBrush (rDisplay, GradientColor, BackColor, lgm))
{
if (this.Blend != null)
lgb.Blend = this.Blend;
e.Graphics.FillRectangle (lgb, rDisplay);
}
}
 
// Next draw borders...
drawBorder (e.Graphics);
}
 
protected virtual void drawBorder (Graphics fx)
{
fx.DrawRectangle (Pens.Silver, 2, 2, Width - 4, ActualHeight - 4);

// Top border
fx.DrawLine (Pens.Silver, 0, 0, Width, 0);
fx.DrawLine (Pens.White, 0, 1, Width, 1);
fx.DrawLine (Pens.DarkGray, 3, 3, Width - 4, 3);
fx.DrawLine (Pens.DimGray, 4, 4, Width - 5, 4);
 
// Left border
fx.DrawLine (Pens.Silver, 0, 0, 0, ActualHeight);
fx.DrawLine (Pens.White, 1, 1, 1, ActualHeight);
fx.DrawLine (Pens.DarkGray, 3, 3, 3, ActualHeight - 4);
fx.DrawLine (Pens.DimGray, 4, 4, 4, ActualHeight - 5);
 
// Bottom border
fx.DrawLine (Pens.DarkGray, 1, ActualHeight - 1, Width - 1, ActualHeight - 1);
fx.DrawLine (Pens.White, 3, ActualHeight - 3, Width - 3, ActualHeight - 3);
fx.DrawLine (Pens.Silver, 4, ActualHeight - 4, Width - 4, ActualHeight - 4);
 
// Right border
fx.DrawLine (Pens.DarkGray, Width - 1, 1, Width - 1, ActualHeight - 1);
fx.DrawLine (Pens.White, Width - 3, 3, Width - 3, ActualHeight - 3);
fx.DrawLine (Pens.Silver, Width - 4, 4, Width - 4, ActualHeight - 4);
}
 
protected virtual void drawCloseButton (Graphics fx)
{
if (visualStylesEnabled())
drawThemeCloseButton (fx);
else
drawLegacyCloseButton (fx);
}
 
///
/// Draw a Windows XP style close button.
///

protected void drawThemeCloseButton (Graphics fx)
{
IntPtr hTheme = OpenThemeData (Handle, "Window");
if (hTheme == IntPtr.Zero)
{
drawLegacyCloseButton (fx);
return;
}
int stateId;
if (closePressed)
stateId = CBS_PUSHED;
else if (closeHot)
stateId = CBS_HOT;
else
stateId = CBS_NORMAL;
RECT reClose = new RECT (rClose);
RECT reClip = reClose; // should fx.VisibleClipBounds be used here?
IntPtr hDC = fx.GetHdc();
DrawThemeBackground (hTheme, hDC, WP_CLOSEBUTTON, stateId, ref reClose, ref reClip);
fx.ReleaseHdc (hDC);
CloseThemeData (hTheme);
}
 
///
/// Draw a Windows 95 style close button.
///

protected void drawLegacyCloseButton (Graphics fx)
{
ButtonState bState;
if (closePressed)
bState = ButtonState.Pushed;
else // the Windows 95 theme doesn't have a "hot" button
bState = ButtonState.Normal;
ControlPaint.DrawCaptionButton (fx, rClose, CaptionButton.Close, bState);
}
 
///
/// Determine whether or not XP Visual Styles are active. Compatible with pre-UxTheme.dll versions of Windows.
///

protected bool visualStylesEnabled()
{
try
{
if (IsThemeActive() == 1)
return true;
else
return false;
}
catch (System.DllNotFoundException) // pre-XP systems which don't have UxTheme.dll
{
return false;
}
}
#endregion
 

 
#region Timers and EventHandlers
protected void viewTimer (object sender, System.EventArgs e)
{
switch (ClockState)
{
case ClockStates.Opening:
//JW: Note heightAdjustment
if (Top - 2 <= rScreen.Height - ActualHeight - heightAdjustment)
{
//JW: Note heightAdjustment
Top = rScreen.Height - ActualHeight - heightAdjustment;
Height = ActualHeight;
ClockState = ClockStates.Showing;
viewClock.Interval = WaitTime;
}
else
{
Top -= 2;
Height += 2;
}
break;
 
case ClockStates.Showing:
if (!WaitOnMouseOver || !rGlobDisplay.Contains (Cursor.Position))
{
viewClock.Interval = 1;
ClockState = ClockStates.Closing;
}
break;
 
case ClockStates.Closing:
Top += 2;
Height -= 2;

//JW: Note heightAdjustment
if (Top >= rScreen.Height - heightAdjustment)
{
ClockState = ClockStates.None;
viewClock.Stop();
viewClock.Dispose();
 
//JW: Remove the item from our map
activeWindows.Remove(this);
 
Close();
 
//JW: Just in case...
this.Dispose(true);
}
break;
}
}
 
protected override void OnMouseMove (System.Windows.Forms.MouseEventArgs e)
{
if (Title != null && rGlobTitle.Contains (Cursor.Position) && !textPressed && !closePressed)
{
Cursor = Cursors.Hand;
titleHot = true;
textHot = false; closeHot = false;
Invalidate();
}
else if (rGlobText.Contains (Cursor.Position) && !titlePressed && !closePressed)
{
Cursor = Cursors.Hand;
textHot = true;
titleHot = false; closeHot = false;
Invalidate();
}
else if (rGlobClose.Contains (Cursor.Position) && !titlePressed && !textPressed)
{
Cursor = Cursors.Hand;
closeHot = true;
titleHot = false; textHot = false;
Invalidate();
}
else if ((textHot || titleHot || closeHot) && (!titlePressed && !textPressed && !closePressed))
{
Cursor = Cursors.Default;
titleHot = false; textHot = false; closeHot = false;
Invalidate();
}
base.OnMouseMove (e);
}
 
protected override void OnMouseDown (System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (rGlobClose.Contains (Cursor.Position))
{
closePressed = true;
closeHot = false;
Invalidate();
}
else if (rGlobText.Contains (Cursor.Position))
{
textPressed = true;
Invalidate();
}
else if (Title != null && rGlobTitle.Contains (Cursor.Position))
{
titlePressed = true;
Invalidate();
}
}
base.OnMouseDown (e);
}
 
protected override void OnMouseUp (System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (closePressed)
{
Cursor = Cursors.Default;
closePressed = false;
closeHot = false;
Invalidate();
if (rGlobClose.Contains (Cursor.Position))
Close();
}
else if (textPressed)
{
Cursor = Cursors.Default;
textPressed = false;
textHot = false;
Invalidate();
if (rGlobText.Contains (Cursor.Position))
{
Close();
if (TextClicked != null)
TextClicked (this, new System.EventArgs());
}
}
else if (titlePressed)
{
Cursor = Cursors.Default;
titlePressed = false;
titleHot = false;
Invalidate();
if (rGlobTitle.Contains (Cursor.Position))
{
Close();
if (TitleClicked != null)
TitleClicked (this, new System.EventArgs());
}
}
}
base.OnMouseUp (e);
}
#endregion
 
#region P/Invoke
// DrawThemeBackground()
protected const Int32 WP_CLOSEBUTTON = 18;
protected const Int32 CBS_NORMAL = 1;
protected const Int32 CBS_HOT = 2;
protected const Int32 CBS_PUSHED = 3;
[StructLayout (LayoutKind.Explicit)]
protected struct RECT
{
[FieldOffset (0)] public Int32 Left;
[FieldOffset (4)] public Int32 Top;
[FieldOffset (8)] public Int32 Right;
[FieldOffset (12)] public Int32 Bottom;
 
public RECT (System.Drawing.Rectangle bounds)
{
Left = bounds.Left;
Top = bounds.Top;
Right = bounds.Right;
Bottom = bounds.Bottom;
}
}
 
// SetWindowPos()
protected const Int32 HWND_TOPMOST = -1;
protected const Int32 SWP_NOACTIVATE = 0x0010;
 
// ShowWindow()
protected const Int32 SW_SHOWNOACTIVATE = 4;
 
// UxTheme.dll
[DllImport ("UxTheme.dll")]
protected static extern Int32 IsThemeActive();
[DllImport ("UxTheme.dll")]
protected static extern IntPtr OpenThemeData (IntPtr hWnd, [MarshalAs (UnmanagedType.LPTStr)] string classList);
[DllImport ("UxTheme.dll")]
protected static extern void CloseThemeData (IntPtr hTheme);
[DllImport ("UxTheme.dll")]
protected static extern void DrawThemeBackground (IntPtr hTheme, IntPtr hDC, Int32 partId, Int32 stateId, ref RECT rect, ref RECT clipRect);
 
// user32.dll
[DllImport ("user32.dll")]
protected static extern bool ShowWindow (IntPtr hWnd, Int32 flags);
[DllImport ("user32.dll")]
protected static extern bool SetWindowPos (IntPtr hWnd, Int32 hWndInsertAfter, Int32 X, Int32 Y, Int32 cx, Int32 cy, uint uFlags);
#endregion
}
}

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 3 Aug 2004
Article Copyright 2004 by Robert Misiak
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid