///////////////////////////////////////////////////////////////////// // Copyright (c) 2007 Aaron Lerch // http://www.aaronlerch.com/ // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. ///////////////////////////////////////////////////////////////////// using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace Lerch.Samples { public class CueToolStripTextBox : ToolStripTextBox { public CueToolStripTextBox() : base() { if (this.Control != null) { this.Control.HandleCreated += new EventHandler(OnControlHandleCreated); } } public CueToolStripTextBox(string name) : base(name) { if (this.Control != null) { this.Control.HandleCreated += new EventHandler(OnControlHandleCreated); } } protected override void Dispose(bool disposing) { if (disposing) { if (this.Control != null) { this.Control.HandleCreated -= new EventHandler(OnControlHandleCreated); } } base.Dispose(disposing); } void OnControlHandleCreated(object sender, EventArgs e) { UpdateCue(); } #region PInvoke Helpers private static uint ECM_FIRST = 0x1500; private static uint EM_SETCUEBANNER = ECM_FIRST + 1; [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] private static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, String lParam); #endregion PInvoke Helpers #region CueText private string _cueText = String.Empty; /// /// Gets or sets the text the will display as a cue to the user. /// [Description("The text value to be displayed as a cue to the user.")] [Category("Appearance")] [DefaultValue("")] [Localizable(true)] public string CueText { get { return _cueText; } set { if (value == null) { value = String.Empty; } if (!_cueText.Equals(value, StringComparison.CurrentCulture)) { _cueText = value; UpdateCue(); OnCueTextChanged(EventArgs.Empty); } } } /// /// Occurs when the property value changes. /// public event EventHandler CueTextChanged; [EditorBrowsable(EditorBrowsableState.Advanced)] protected virtual void OnCueTextChanged(EventArgs e) { EventHandler handler = CueTextChanged; if (handler != null) { handler(this, e); } } #endregion CueText #region ShowCueTextOnFocus private bool _showCueTextWithFocus = false; /// /// Gets or sets a value indicating whether the will display the /// even when the control has focus. /// [Description("Indicates whether the CueText will be displayed even when the control has focus.")] [Category("Appearance")] [DefaultValue(false)] [Localizable(true)] public bool ShowCueTextWithFocus { get { return _showCueTextWithFocus; } set { if (_showCueTextWithFocus != value) { _showCueTextWithFocus = value; UpdateCue(); OnShowCueTextWithFocusChanged(EventArgs.Empty); } } } /// /// Occurs when the property value changes. /// public event EventHandler ShowCueTextWithFocusChanged; [EditorBrowsable(EditorBrowsableState.Advanced)] protected virtual void OnShowCueTextWithFocusChanged(EventArgs e) { EventHandler handler = ShowCueTextWithFocusChanged; if (handler != null) { handler(this, e); } } #endregion ShowCueTextOnFocus private void UpdateCue() { // If the handle isn't yet created, // this will be called when it is created if ((this.Control != null) && (this.Control.IsHandleCreated)) { SendMessage(new HandleRef(this.Control, this.Control.Handle), EM_SETCUEBANNER, (_showCueTextWithFocus) ? new IntPtr(1) : IntPtr.Zero, _cueText); } } } }