自定義彈出控件(下)
来源:互联网 发布:mac添加不了qq邮箱 编辑:程序博客网 时间:2024/05/16 08:30
自定義彈出控件(下)
原理:組合Popup和ComboBox的功能,製作一個可以下拉任意控件的PopupComboBox,也可以繼續擴展PopupComboBox,生成CalendarComboBox,CalculateComboBox,TreeComboBox等等.
以下示例制作一個可編輯的下拉選擇框.本文需要使用到上一篇文章中的所有類.
1.PopupComboBox.cs定義一個ComboBox類
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;
namespace PopupControl
{
/// <summary>
/// Represents a Windows combo box control with a custom popup control attached.
/// </summary>
[ToolboxBitmap(typeof(System.Windows.Forms.ComboBox)), ToolboxItem(true), ToolboxItemFilter("System.Windows.Forms"), Description("Displays an editable text box with a drop-down list of permitted values.")]
public partial class PopupComboBox : ComboBox
{
/// <summary>
/// Initializes a new instance of the <see cref="PopupControl.PopupComboBox" /> class.
/// </summary>
public PopupComboBox()
{
InitializeComponent();
base.DropDownHeight = base.DropDownWidth = 1;
base.IntegralHeight = false;
}
private Popup dropDown;
private Control dropDownControl;
/// <summary>
/// Gets or sets the drop down control.
/// </summary>
/// <value>The drop down control.</value>
public Control DropDownControl
{
get
{
return dropDownControl;
}
set
{
if (dropDownControl == value)
return;
dropDownControl = value;
dropDown = new Popup(value);
}
}
/// <summary>
/// Shows the drop down.
/// </summary>
public void ShowDropDown()
{
if (dropDown != null)
{
dropDown.Show(this);
}
}
/// <summary>
/// Hides the drop down.
/// </summary>
public void HideDropDown()
{
if (dropDown != null)
{
dropDown.Hide();
}
}
/// <summary>
/// Processes Windows messages.
/// </summary>
/// <param name="m">The Windows <see cref="T:System.Windows.Forms.Message" /> to process.</param>
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc(ref Message m)
{
if (m.Msg == (NativeMethods.WM_REFLECT + NativeMethods.WM_COMMAND))
{
if (NativeMethods.HIWORD(m.WParam) == NativeMethods.CBN_DROPDOWN)
{
ShowDropDown();
return;
}
}
base.WndProc(ref m);
}
#region " Unused Properties "
/// <summary>This property is not relevant for this class.</summary>
/// <returns>This property is not relevant for this class.</returns>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
public new int DropDownWidth
{
get { return base.DropDownWidth; }
set { base.DropDownWidth = value; }
}
/// <summary>This property is not relevant for this class.</summary>
/// <returns>This property is not relevant for this class.</returns>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
public new int DropDownHeight
{
get { return base.DropDownHeight; }
set { base.DropDownHeight = value; }
}
/// <summary>This property is not relevant for this class.</summary>
/// <returns>This property is not relevant for this class.</returns>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
public new bool IntegralHeight
{
get { return base.IntegralHeight; }
set { base.IntegralHeight = value; }
}
/// <summary>This property is not relevant for this class.</summary>
/// <returns>This property is not relevant for this class.</returns>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
public new ObjectCollection Items
{
get { return base.Items; }
}
/// <summary>This property is not relevant for this class.</summary>
/// <returns>This property is not relevant for this class.</returns>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
public new int ItemHeight
{
get { return base.ItemHeight; }
set { base.ItemHeight = value; }
}
#endregion
}
}
2.調用
拖一個PopupComboBox到界面,然后在构造函數中初始化PopupComboBox的DropDownControl屬性.可以設成任意的想要顯示的控件.
public MainForm()
{
//InitializeComponent();
this.popupComboBox1.DropDownControl = new ComplexPopup();
}
請參考以下文章:
自定義彈出控件(上)
http://blog.csdn.net/manimanihome/archive/2008/01/18/2051649.aspx
自定義彈出控件(中)
http://blog.csdn.net/manimanihome/archive/2008/02/27/2124951.aspx
- 自定義彈出控件(下)
- 自定義彈出控件(上)
- 自定義彈出控件(中)
- Wince下自绘文本控件
- 学习自绘控件(mfc) 转(下)
- Delphi自带控件
- 自定義控件
- 控件自绘
- 自绘控件笔记
- 自绘 列表控件
- 自绘 tab 控件
- TextBoxt自验证控件
- 自绘CStatic控件
- 自绘GridView控件
- 自绘控件笔记
- VC控件自绘制
- 自会控件
- MFC自画控件
- javascript 显示时间
- 创建可以定时自动关闭的对话框
- 如何在C#中使用内嵌资源文件
- 用MASM32写GUI版的netstat--NetViewG.exe v0.0.0001 beta1
- sql技巧
- 自定義彈出控件(下)
- Delphi中initialization和finalization
- abap 中的内表操作
- Oracle触发器详细介绍
- 架设 DNS 所需要的套件
- GridView自动编号
- FreeBSD 7.0 Release已放出,可以下载了
- 改的检测current mounted的removable device的代码
- ioctl函数介绍