1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok pbartok@novell.com
30 using System.ComponentModel;
31 using System.ComponentModel.Design;
34 namespace System.Windows.Forms {
35 [DefaultProperty("Text")]
36 [DefaultEvent("MouseDown")]
37 [Designer ("System.Windows.Forms.Design.NotifyIconDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
38 [ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow)]
39 public sealed class NotifyIcon : Component {
40 #region Local Variables
41 private ContextMenu context_menu;
43 private Bitmap icon_bitmap;
46 private NotifyIconWindow window;
47 private bool systray_active;
48 private ToolTip tooltip;
49 #endregion // Local Variables
51 #region NotifyIconWindow Class
52 internal class NotifyIconWindow : Form {
56 public NotifyIconWindow(NotifyIcon owner) {
59 rect = new Rectangle(0, 0, 1, 1);
61 FormBorderStyle = FormBorderStyle.None;
65 SizeChanged += new EventHandler(HandleSizeChanged);
67 // Events that need to be sent to our parent
68 Click += new EventHandler(HandleClick);
69 DoubleClick += new EventHandler(HandleDoubleClick);
70 MouseDown +=new MouseEventHandler(HandleMouseDown);
71 MouseUp +=new MouseEventHandler(HandleMouseUp);
72 MouseMove +=new MouseEventHandler(HandleMouseMove);
73 ContextMenu = owner.context_menu;
76 protected override CreateParams CreateParams {
80 cp = base.CreateParams;
82 cp.Parent = IntPtr.Zero;
83 cp.Style = (int)WindowStyles.WS_POPUP;
84 cp.Style |= (int)WindowStyles.WS_CLIPSIBLINGS;
86 cp.ExStyle = (int)(WindowExStyles.WS_EX_TOOLWINDOW);
92 protected override void WndProc(ref Message m) {
95 // NotifyIcon does CONTEXTMENU on mouse up, not down
96 // so we swallow the message here, and handle it on our own
98 case Msg.WM_CONTEXTMENU:
102 switch ((Msg)m.LParam.ToInt32()) {
103 case Msg.WM_LBUTTONDOWN: {
104 owner.OnMouseDown (new MouseEventArgs(MouseButtons.Left, 1, Control.MousePosition.X, Control.MousePosition.Y, 0));
108 case Msg.WM_LBUTTONUP: {
109 owner.OnMouseUp (new MouseEventArgs(MouseButtons.Left, 1, Control.MousePosition.X, Control.MousePosition.Y, 0));
110 owner.OnClick (EventArgs.Empty);
114 case Msg.WM_LBUTTONDBLCLK: {
115 owner.OnDoubleClick (EventArgs.Empty);
119 case Msg.WM_MOUSEMOVE: {
120 owner.OnMouseMove (new MouseEventArgs(MouseButtons.None, 1, Control.MousePosition.X, Control.MousePosition.Y, 0));
124 case Msg.WM_RBUTTONDOWN: {
125 owner.OnMouseDown (new MouseEventArgs(MouseButtons.Right, 1, Control.MousePosition.X, Control.MousePosition.Y, 0));
129 case Msg.WM_RBUTTONUP: {
130 owner.OnMouseUp (new MouseEventArgs(MouseButtons.Right, 1, Control.MousePosition.X, Control.MousePosition.Y, 0));
131 owner.OnClick (EventArgs.Empty);
135 case Msg.WM_RBUTTONDBLCLK: {
136 owner.OnDoubleClick (EventArgs.Empty);
143 base.WndProc (ref m);
146 internal void CalculateIconRect() {
151 // Icons are always square. Try to center them in the window
152 if (ClientRectangle.Width < ClientRectangle.Height) {
153 size = ClientRectangle.Width;
155 size = ClientRectangle.Height;
157 x = this.ClientRectangle.Width / 2 - size / 2;
158 y = this.ClientRectangle.Height / 2 - size / 2;
159 rect = new Rectangle(x, y, size, size);
161 Bounds = new Rectangle (0, 0, size, size);
164 internal override void OnPaintInternal (PaintEventArgs e) {
165 if (owner.icon != null) {
166 e.Graphics.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(SystemColors.Window), rect);
167 e.Graphics.DrawImage(owner.icon_bitmap,
169 new Rectangle (0, 0, owner.icon_bitmap.Width, owner.icon_bitmap.Height),
175 internal void InternalRecreateHandle () {
176 base.RecreateHandle ();
180 private void HandleSizeChanged(object sender, EventArgs e) {
181 owner.Recalculate ();
184 private void HandleClick (object sender, EventArgs e)
189 private void HandleDoubleClick (object sender, EventArgs e)
191 owner.OnDoubleClick (e);
194 private void HandleMouseDown (object sender, MouseEventArgs e)
196 owner.OnMouseDown (e);
199 private void HandleMouseUp (object sender, MouseEventArgs e)
204 private void HandleMouseMove (object sender, MouseEventArgs e)
206 owner.OnMouseMove (e);
209 #endregion // NotifyIconWindow Class
211 #region Public Constructors
212 public NotifyIcon() {
213 window = new NotifyIconWindow(this);
214 systray_active = false;
217 public NotifyIcon(System.ComponentModel.IContainer container) : this() {
219 #endregion // Public Constructors
221 #region Private Methods
222 private void OnClick (EventArgs e)
224 EventHandler eh = (EventHandler)(Events [ClickEvent]);
229 private void OnDoubleClick (EventArgs e)
231 EventHandler eh = (EventHandler)(Events [DoubleClickEvent]);
236 private void OnMouseDown (MouseEventArgs e)
238 MouseEventHandler eh = (MouseEventHandler)(Events [MouseDownEvent]);
243 private void OnMouseUp (MouseEventArgs e)
245 if ((e.Button & MouseButtons.Right) == MouseButtons.Right && context_menu != null)
246 context_menu.Show (window, new Point(e.X, e.Y));
248 MouseEventHandler eh = (MouseEventHandler)(Events [MouseUpEvent]);
253 private void OnMouseMove (MouseEventArgs e)
255 MouseEventHandler eh = (MouseEventHandler)(Events [MouseMoveEvent]);
260 private void Recalculate ()
262 window.CalculateIconRect ();
268 private void ShowSystray()
270 systray_active = true;
275 icon_bitmap = icon.ToBitmap();
277 XplatUI.SystrayAdd(window.Handle, text, icon, out tooltip);
280 private void HideSystray()
282 if (!systray_active) {
286 systray_active = false;
287 XplatUI.SystrayRemove(window.Handle, ref tooltip);
290 private void UpdateSystray()
292 if (icon_bitmap != null) {
293 icon_bitmap.Dispose();
297 icon_bitmap = icon.ToBitmap();
300 XplatUI.SystrayChange(window.Handle, text, icon, ref tooltip);
303 #endregion // Private Methods
305 #region Public Instance Properties
307 public ContextMenu ContextMenu {
313 if (context_menu != value) {
314 context_menu = value;
315 window.ContextMenu = value;
330 if (text == string.Empty && icon == null) {
348 if (value.Length >= 64) {
349 throw new ArgumentException("ToolTip length must be less than 64 characters long", "Text");
352 if (text == string.Empty && icon == null) {
362 [DefaultValue(false)]
363 public bool Visible {
369 if (visible != value) {
372 // Let our control know, too
373 window.is_visible = value;
383 #endregion // Public Instance Properties
385 #region Protected Instance Methods
386 protected override void Dispose(bool disposing) {
387 if (icon_bitmap != null) {
388 icon_bitmap.Dispose();
394 base.Dispose (disposing);
397 #endregion // Protected Instance Methods
400 static object ClickEvent = new object ();
401 static object DoubleClickEvent = new object ();
402 static object MouseDownEvent = new object ();
403 static object MouseMoveEvent = new object ();
404 static object MouseUpEvent = new object ();
407 public event EventHandler Click {
408 add { Events.AddHandler (ClickEvent, value); }
409 remove { Events.RemoveHandler (ClickEvent, value); }
413 public event EventHandler DoubleClick {
414 add { Events.AddHandler (DoubleClickEvent, value); }
415 remove { Events.RemoveHandler (DoubleClickEvent, value); }
418 public event MouseEventHandler MouseDown {
419 add { Events.AddHandler (MouseDownEvent, value); }
420 remove { Events.RemoveHandler (MouseDownEvent, value); }
423 public event MouseEventHandler MouseMove {
424 add { Events.AddHandler (MouseMoveEvent, value); }
425 remove { Events.RemoveHandler (MouseMoveEvent, value); }
428 public event MouseEventHandler MouseUp {
429 add { Events.AddHandler (MouseUpEvent, value); }
430 remove { Events.RemoveHandler (MouseUpEvent, value); }