WatermarkComboBox 和 WatermarkTextBox

直接给出代码

一、WatermarkComboBox(转)

/* 作者:Starts_2000
* 日期:2009-07-25
* 网站:http://www.csharpwin.com CS 程序员之窗。
* 你可以免费使用或修改以下代码,但请保留版权信息。
* 具体请查看 CS程序员之窗开源协议(http://www.csharpwin.com/csol.html)。
*/

Code Snippet
  1. namespace CSharpWin
  2. {
  3.     /* ×÷??£oStarts_2000
  4.      * è?期ú£o2009-07-25
  5.      * í?站?£ohttp://www.csharpwin.com CS 3ìDò员±之?′°。£
  6.      * 你?可éò?免a·?ê1ó?或òDT改?ò?下?′ú码?£?μ???±£á?°?è¨D?息¢。£
  7.      * 具?ì???2é看′ CS3ìDò员±之?′°开a源′D-ò飨http://www.csharpwin.com/csol.html£?。£
  8.      */
  9.  
  10.     [ToolboxBitmap(typeof(ComboBox))]
  11.     public class WatermarkComboBox : ComboBox
  12.     {
  13.         private string _emptyTextTip;
  14.         private Color _emptyTextTipColor = Color.DarkGray;
  15.         private IntPtr _editHandle;
  16.         private EditNativeWindow _editNativeWindow;
  17.  
  18.         public WatermarkComboBox()
  19.             : base()
  20.         {
  21.         }
  22.  
  23.         [DefaultValue("")]
  24.         public string EmptyTextTip
  25.         {
  26.             get { return _emptyTextTip; }
  27.             set
  28.             {
  29.                 _emptyTextTip = value;
  30.                 base.Invalidate();
  31.             }
  32.         }
  33.  
  34.         [DefaultValue(typeof(Color), "DarkGray")]
  35.         public Color EmptyTextTipColor
  36.         {
  37.             get { return _emptyTextTipColor; }
  38.             set
  39.             {
  40.                 _emptyTextTipColor = value;
  41.                 base.Invalidate();
  42.             }
  43.         }
  44.  
  45.         internal IntPtr EditHandle
  46.         {
  47.             get { return _editHandle; }
  48.         }
  49.  
  50.         internal Rectangle EditRect
  51.         {
  52.             get
  53.             {
  54.                 if (DropDownStyle != ComboBoxStyle.DropDownList)
  55.                 {
  56.                     if (IsHandleCreated && EditHandle != IntPtr.Zero)
  57.                     {
  58.                         RECT rcClient = new RECT();
  59.                         GetWindowRect(EditHandle, ref rcClient);
  60.                         return RectangleToClient(rcClient.Rect);
  61.                     }
  62.                 }
  63.                 return Rectangle.Empty;
  64.             }
  65.         }
  66.  
  67.         [DllImport("user32.dll")]
  68.         private static extern bool GetComboBoxInfo(
  69.             IntPtr hwndCombo, ref ComboBoxInfo info);
  70.  
  71.         [DllImport("user32.dll")]
  72.         private static extern int GetWindowRect(IntPtr hwnd, ref RECT lpRect);
  73.  
  74.         [StructLayout(LayoutKind.Sequential)]
  75.         public struct RECT
  76.         {
  77.             public int Left;
  78.             public int Top;
  79.             public int Right;
  80.             public int Bottom;
  81.  
  82.             public RECT(int left, int top, int right, int bottom)
  83.             {
  84.                 Left = left;
  85.                 Top = top;
  86.                 Right = right;
  87.                 Bottom = bottom;
  88.             }
  89.  
  90.             public RECT(Rectangle rect)
  91.             {
  92.                 Left = rect.Left;
  93.                 Top = rect.Top;
  94.                 Right = rect.Right;
  95.                 Bottom = rect.Bottom;
  96.             }
  97.  
  98.             public Rectangle Rect
  99.             {
  100.                 get
  101.                 {
  102.                     return new Rectangle(
  103.                         Left,
  104.                         Top,
  105.                         Right - Left,
  106.                         Bottom - Top);
  107.                 }
  108.             }
  109.  
  110.             public Size Size
  111.             {
  112.                 get
  113.                 {
  114.                     return new Size(Right - Left, Bottom - Top);
  115.                 }
  116.             }
  117.  
  118.             public static RECT FromXYWH(int x, int y, int width, int height)
  119.             {
  120.                 return new RECT(x,
  121.                                 y,
  122.                                 x + width,
  123.                                 y + height);
  124.             }
  125.  
  126.             public static RECT FromRectangle(Rectangle rect)
  127.             {
  128.                 return new RECT(rect.Left,
  129.                                  rect.Top,
  130.                                  rect.Right,
  131.                                  rect.Bottom);
  132.             }
  133.         }
  134.  
  135.         private struct ComboBoxInfo
  136.         {
  137.             public int cbSize;
  138.             public RECT rcItem;
  139.             public RECT rcButton;
  140.             public ComboBoxButtonState stateButton;
  141.             public IntPtr hwndCombo;
  142.             public IntPtr hwndEdit;
  143.             public IntPtr hwndList;
  144.         }
  145.  
  146.         private enum ComboBoxButtonState
  147.         {
  148.             STATE_SYSTEM_NONE = 0,
  149.             STATE_SYSTEM_INVISIBLE = 0x00008000,
  150.             STATE_SYSTEM_PRESSED = 0x00000008
  151.         }
  152.  
  153.         protected override void OnHandleCreated(EventArgs e)
  154.         {
  155.             base.OnHandleCreated(e);
  156.             ComboBoxInfo cbi = new ComboBoxInfo();
  157.             cbi.cbSize = Marshal.SizeOf(cbi);
  158.             GetComboBoxInfo(base.Handle, ref cbi);
  159.             _editHandle = cbi.hwndEdit;
  160.             if (DropDownStyle != ComboBoxStyle.DropDownList)
  161.             {
  162.                 _editNativeWindow = new EditNativeWindow(this);
  163.             }
  164.         }
  165.  
  166.         protected override void OnHandleDestroyed(EventArgs e)
  167.         {
  168.             base.OnHandleDestroyed(e);
  169.             if (_editNativeWindow != null)
  170.             {
  171.                 _editNativeWindow.Dispose();
  172.                 _editNativeWindow = null;
  173.             }
  174.         }
  175.  
  176.         private class EditNativeWindow : NativeWindow, IDisposable
  177.         {
  178.             private WatermarkComboBox _owner;
  179.             private const int WM_PAINT = 0xF;
  180.  
  181.             public EditNativeWindow(WatermarkComboBox owner)
  182.                 : base()
  183.             {
  184.                 _owner = owner;
  185.                 AssignHandle(_owner.EditHandle);
  186.             }
  187.  
  188.             [DllImport("user32.dll")]
  189.             private static extern IntPtr GetDC(IntPtr ptr);
  190.  
  191.             [DllImport("user32.dll")]
  192.             private static extern int ReleaseDC(IntPtr hwnd, IntPtr hDC);
  193.  
  194.             protected override void WndProc(ref Message m)
  195.             {
  196.                 base.WndProc(ref m);
  197.                 if (m.Msg == WM_PAINT)
  198.                 {
  199.                     IntPtr handle = m.HWnd;
  200.                     IntPtr hdc = GetDC(handle);
  201.                     if (hdc == IntPtr.Zero)
  202.                     {
  203.                         return;
  204.                     }
  205.                     try
  206.                     {
  207.                         using (Graphics graphics = Graphics.FromHdc(hdc))
  208.                         {
  209.                             if (_owner.Text.Length == 0
  210.                                 && !_owner.Focused
  211.                                 && !string.IsNullOrEmpty(_owner.EmptyTextTip))
  212.                             {
  213.                                 TextFormatFlags format =
  214.                                     TextFormatFlags.EndEllipsis |
  215.                                     TextFormatFlags.VerticalCenter;
  216.  
  217.                                 if (_owner.RightToLeft == RightToLeft.Yes)
  218.                                 {
  219.                                     format |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
  220.                                 }
  221.  
  222.                                 TextRenderer.DrawText(
  223.                                     graphics,
  224.                                     _owner.EmptyTextTip,
  225.                                     _owner.Font,
  226.                                     new Rectangle(0, 0, _owner.EditRect.Width, _owner.EditRect.Height),
  227.                                     _owner.EmptyTextTipColor,
  228.                                     format);
  229.                             }
  230.                         }
  231.                     }
  232.                     finally
  233.                     {
  234.                         ReleaseDC(handle, hdc);
  235.                     }
  236.                 }
  237.             }
  238.  
  239.             #region IDisposable 3é员±
  240.  
  241.             public void Dispose()
  242.             {
  243.                 ReleaseHandle();
  244.                 _owner = null;
  245.             }
  246.  
  247.             #endregion
  248.         }
  249.     }
  250. }

二、WatermarkTextBox(转)

Code Snippet
  1. namespace CSharpWin
  2. {
  3.       /* 作??Starts_2000
  4.        * 日期?2009-07-25
  5.        * 网站?http://www.csharpwin.com CS 程序员之窗。
  6.        * 你可以免?使用或修改以下代码?但?保留版权信息。
  7.        * 具体?查看 CS程序员之窗开源协??http://www.csharpwin.com/csol.html?。
  8.        */
  9.  
  10.     [ToolboxBitmap(typeof(TextBox))]
  11.     public class WatermarkTextBox : TextBox
  12.     {
  13.         private string _emptyTextTip;
  14.         private Color _emptyTextTipColor = Color.DarkGray;
  15.  
  16.         private const int WM_PAINT = 0xF;
  17.  
  18.         public WatermarkTextBox()
  19.             : base()
  20.         {
  21.         }
  22.  
  23.         [DefaultValue("")]
  24.         public string EmptyTextTip
  25.         {
  26.             get { return _emptyTextTip; }
  27.             set
  28.             {
  29.                 _emptyTextTip = value;
  30.                 base.Invalidate();
  31.             }
  32.         }
  33.  
  34.         [DefaultValue(typeof(Color), "DarkGray")]
  35.         public Color EmptyTextTipColor
  36.         {
  37.             get { return _emptyTextTipColor; }
  38.             set
  39.             {
  40.                 _emptyTextTipColor = value;
  41.                 base.Invalidate();
  42.             }
  43.         }
  44.  
  45.         protected override void WndProc(ref Message m)
  46.         {
  47.             base.WndProc(ref m);
  48.             if (m.Msg == WM_PAINT)
  49.             {
  50.                 WmPaint(ref m);
  51.             }
  52.         }
  53.  
  54.         private void WmPaint(ref Message m)
  55.         {
  56.             using (Graphics graphics = Graphics.FromHwnd(base.Handle))
  57.             {
  58.                 if (Text.Length == 0
  59.                     && !string.IsNullOrEmpty(_emptyTextTip)
  60.                     && !Focused)
  61.                 {
  62.                     TextFormatFlags format =
  63.                         TextFormatFlags.EndEllipsis |
  64.                         TextFormatFlags.VerticalCenter;
  65.  
  66.                     if (RightToLeft == RightToLeft.Yes)
  67.                     {
  68.                         format |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
  69.                     }
  70.  
  71.                     TextRenderer.DrawText(
  72.                         graphics,
  73.                         _emptyTextTip,
  74.                         Font,
  75.                         base.ClientRectangle,
  76.                         _emptyTextTipColor,
  77.                         format);
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
posted @ 2009-12-08 13:06  流泉飞石  阅读(439)  评论(0编辑  收藏  举报