本文共 11868 字,大约阅读时间需要 39 分钟。
[ToolboxItem(false)] public class OrpDropDownButton : Button { protected override void OnEnter(EventArgs e) { base.OnEnter(e); if (Parent != null) Parent.Focus(); } public OrpDropDownButton () : base() { Image = LCBResource.DROPDOWNBTN1; ImageAlign = ContentAlignment.MiddleCenter; } } |
using System; using System.Drawing; using System.ComponentModel; using System.Runtime.InteropServices; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace LookupComboBox { internal class NativeAPI { [Serializable, StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; public RECT(int left_, int top_, int right_, int bottom_) { Left = left_; Top = top_; Right = right_; Bottom = bottom_; } public int Height { get { return Bottom - Top; } } public int Width { get { return Right - Left; } } public Size Size { get { return new Size(Width, Height); } } public Point Location { get { return new Point(Left, Top); } } // Handy method for converting to a System.Drawing.Rectangle public Rectangle ToRectangle() { return Rectangle.FromLTRB(Left, Top, Right, Bottom); } public static RECT FromRectangle(Rectangle rectangle) { return new RECT(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom); } public override int GetHashCode() { return Left ^ ((Top << 13) | (Top >> 0x13)) ^ ((Width << 0x1a) | (Width >> 6)) ^ ((Height << 7) | (Height >> 0x19)); } #region Operator overloads public static implicit operator Rectangle(RECT rect) { return Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom); } public static implicit operator RECT(Rectangle rect) { return new RECT(rect.Left, rect.Top, rect.Right, rect.Bottom); } #endregion } public const uint EM_SETRECT = 0xb3; public const int WS_CLIPCHILDREN = 0x02000000; public const int WS_CLIPSIBLINGS = 0x04000000; public const int ES_MULTILINE = 0x0004; [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, ref RECT lParam); } } |
[ToolboxItem(false)] public class OrpCustomButtonEdit:TextBox { .................. protected override void CreateHandle() { CreateParams.Style = CreateParams.Style | NativeAPI.ES_MULTILINE | NativeAPI.WS_CLIPCHILDREN | NativeAPI.WS_CLIPSIBLINGS; base.CreateHandle(); } ............................ } |
[ToolboxItem(false)] public class OrpCustomButtonEdit:TextBox { private OrpDropDownButton _dropBtn = null; private void AdjustTextSize() { _dropBtn.Top = 0; _dropBtn.Left = Width - 20; _dropBtn.Height = Height - 5; _dropBtn.Width = 16; Rectangle rect = new Rectangle(0, 0, _dropBtn.Left-2, ClientRectangle.Bottom - ClientRectangle.Top); NativeAPI.RECT r = NativeAPI.RECT.FromRectangle(rect); NativeAPI.SendMessage(Handle, NativeAPI.EM_SETRECT, (IntPtr)0, ref r); } ............................ protected override void OnFontChanged(EventArgs e) { base.OnFontChanged(e); AdjustTextSize(); } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); AdjustTextSize(); } protected override void OnResize(EventArgs e) { base.OnResize(e); AdjustTextSize(); } protected override void OnEnter(EventArgs e) { base.OnEnter(e); AdjustTextSize(); } protected override void InitLayout() { base.InitLayout(); AdjustTextSize(); } public OrpCustomButtonEdit() : base() { ................. } } |
[ToolboxItem(false)] public class OrpCustomButtonEdit:TextBox { private OrpDropDownButton _dropBtn = null; ....................... protected override void OnKeyDown(KeyEventArgs e) { if (e.KeyCode == Keys.Return || e.KeyCode == Keys.F4) ButtonClick(this, EventArgs.Empty); else base.OnKeyDown(e); } protected virtual void EmbedButtonClick(EventArgs args) { } private void ButtonClick(object sender,EventArgs args) { EmbedButtonClick(args); } public OrpCustomButtonEdit() : base() { _dropBtn = new OrpDropDownButton(); _dropBtn.Cursor = Cursors.Hand; _dropBtn.CausesValidation = false; _dropBtn.Click += new EventHandler(ButtonClick); _dropBtn.TabStop = false; Controls.Add(_dropBtn); } } |
[ToolboxItem(true)] public class OrpButtonEdit : OrpCustomButtonEdit { private static object _onButtonClick = new object(); [Category("Behavior")] public virtual event EventHandler ButtonClick { add { Events.AddHandler(_onButtonClick, value); } remove { Events.RemoveHandler(_onButtonClick, value); } } protected virtual void OnButtonClick(EventArgs args) { EventHandler handler = (EventHandler)Events[_onButtonClick]; if (handler != null) handler(this, args); } protected override void EmbedButtonClick(EventArgs args) { OnButtonClick(args); } } |
using System; using System.Drawing; using System.Collections.Specialized; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Globalization; using System.Runtime.InteropServices; using System.Reflection; namespace LookupComboBox { [ToolboxItem(false)] public class OrpDropDownButton : Button { protected override void OnEnter(EventArgs e) { base.OnEnter(e); if (Parent != null) Parent.Focus(); } public OrpDropDownButton() : base() { Image = LCBResource.DROPDOWNBTN1; ImageAlign = ContentAlignment.MiddleCenter; } } [ToolboxItem(false)] public class OrpCustomButtonEdit:TextBox { private OrpDropDownButton _dropBtn = null; private void AdjustTextSize() { _dropBtn.Top = 0; _dropBtn.Left = Width - 20; _dropBtn.Height = Height - 5; _dropBtn.Width = 16; Rectangle rect = new Rectangle(0, 0, _dropBtn.Left-2, ClientRectangle.Bottom - ClientRectangle.Top); NativeAPI.RECT r = NativeAPI.RECT.FromRectangle(rect); NativeAPI.SendMessage(Handle, NativeAPI.EM_SETRECT, (IntPtr)0, ref r); } protected override void CreateHandle() { CreateParams.Style = CreateParams.Style | NativeAPI.ES_MULTILINE | NativeAPI.WS_CLIPCHILDREN | NativeAPI.WS_CLIPSIBLINGS; base.CreateHandle(); } protected override void OnFontChanged(EventArgs e) { base.OnFontChanged(e); AdjustTextSize(); } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); AdjustTextSize(); } protected override void OnResize(EventArgs e) { base.OnResize(e); AdjustTextSize(); } protected override void OnEnter(EventArgs e) { base.OnEnter(e); AdjustTextSize(); } protected override void InitLayout() { base.InitLayout(); AdjustTextSize(); } protected override void OnKeyDown(KeyEventArgs e) { if (e.KeyCode == Keys.Return || e.KeyCode == Keys.F4) ButtonClick(this, EventArgs.Empty); else base.OnKeyDown(e); } protected virtual void EmbedButtonClick(EventArgs args) { } private void ButtonClick(object sender,EventArgs args) { EmbedButtonClick(args); } public OrpCustomButtonEdit() : base() { _dropBtn = new OrpDropDownButton(); _dropBtn.Cursor = Cursors.Hand; _dropBtn.CausesValidation = false; _dropBtn.Click += new EventHandler(ButtonClick); _dropBtn.TabStop = false; Controls.Add(_dropBtn); } } [ToolboxItem(true)] public class OrpButtonEdit : OrpCustomButtonEdit { private static object _onButtonClick = new object(); [Category("Behavior")] public virtual event EventHandler ButtonClick { add { Events.AddHandler(_onButtonClick, value); } remove { Events.RemoveHandler(_onButtonClick, value); } } protected virtual void OnButtonClick(EventArgs args) { EventHandler handler = (EventHandler)Events[_onButtonClick]; if (handler != null) handler(this, args); } protected override void EmbedButtonClick(EventArgs args) { OnButtonClick(args); } } } |
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1138674