C#仿QQ皮肤-实现原理系列文章导航
http://www.cnblogs.com/sufei/archive/2010/03/10/1682847.html
大家还是先来看看效果吧
下面我们一起来看看是怎么样实现的
这个其实是控件里是没有的,那天在博客上看到一个同志在写这个控件,所性抄一点思想,在结合我和控件就加上了,还挺好用的,呵呵,有了这个控件大家就不用再为绑定一个表那个字段面苦了,可以一下自全部绑定,实现很简单大家一起来看看吧
第一步我们新建一个组件名称 为ComboBoxDataGridView
其实就是dataGridViewHost 和ToolStripDropDown之间的相互配合
这个控件是继承自我皮肤里的控件Combox这个大家一会儿看代码
我们要声明一个委托来调用 构造DataGridViw的方法
public delegate void ComboBoxDataGridViewScroll( object sender, bool vscroll);
public event ComboBoxDataGridViewScroll OnScroll;
private const int WM_HSCROLL = 0x114 ;
private const int WM_VSCROLL = 0x115 ;
public ComboBoxDataGridView()
{
this .SetStyle(ControlStyles.OptimizedDoubleBuffer, true );
DrawDataGridView();
this .OnScroll += new ComboBoxDataGridViewScroll(ComboBoxDataGridView_OnScroll);
}
我们先来看看 DrawDataGridView();
方法吧
{
CRD.WinUI.Editors.DataGridView dataGridView = new CRD.WinUI.Editors.DataGridView();
dataGridView.RowHeadersVisible = false ;
dataGridView.RowTemplate.Resizable = DataGridViewTriState.False; // 固定行高不允许调节
dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView.BackgroundColor = SystemColors.ActiveCaptionText;
dataGridView.BorderStyle = BorderStyle.None;
dataGridView.ReadOnly = true ;
dataGridView.AllowUserToAddRows = false ;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView.Click += new EventHandler(dataGridView_Click);
// 设置DataGridView的数据源
CRD.WinUI.Forms.EntryForm frmDataSource = new CRD.WinUI.Forms.EntryForm();
frmDataSource.Controls.Add(dataGridView);
frmDataSource.SuspendLayout();
dataGridViewHost = new ToolStripControlHost(dataGridView);
dataGridViewHost.AutoSize = m_blPopupAutoSize;
dropDown = new ToolStripDropDown();
dropDown.Width = this .Width;
dropDown.Items.Add(dataGridViewHost);
}
有关DatagridView和Combox的实现 代码大家请参考我的皮肤系统,呵呵
下面我把代码都放上来各们看看吧也提提建议
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace CRD.WinUI.Misc
{
public partial class ComboBoxDataGridView : CRD.WinUI.Misc.ComboBox
{
// 成员变量
private const int WM_LBUTTONDOWN = 0x201 , WM_LBUTTONDBLCLK = 0x203 ;
ToolStripControlHost dataGridViewHost;
ToolStripDropDown dropDown;
private string m_sDefaultColumn;
private bool m_blPopupAutoSize = false ;
private DataGridViewRow m_dgvRow;
public event EventHandler AfterSelector;
/**/
/// /声明一个委托
public delegate void ComboBoxDataGridViewScroll( object sender, bool vscroll);
public event ComboBoxDataGridViewScroll OnScroll;
private const int WM_HSCROLL = 0x114 ;
private const int WM_VSCROLL = 0x115 ;
public ComboBoxDataGridView()
{
this .SetStyle(ControlStyles.OptimizedDoubleBuffer, true );
DrawDataGridView();
this .OnScroll += new ComboBoxDataGridViewScroll(ComboBoxDataGridView_OnScroll);
}
public void ComboBoxDataGridView_OnScroll( object sender, bool vscroll)
{
this .dataGridViewHost.Focus();
}
[Description( " 设置DataGridView属性 " ), Browsable( true ), Category( " N8 " )]
public DataGridView DataGridView
{
get
{
return dataGridViewHost.Control as DataGridView;
}
}
[Description( " 下拉表格尺寸是否为自动 " ), Browsable( true ), Category( " N8 " )]
public bool PopupGridAutoSize
{
set
{
m_blPopupAutoSize = value;
}
}
[Description( " 设置默认值 " ), Browsable( true ), Category( " N8 " )]
public string DefaultColumn
{
set
{
m_sDefaultColumn = value;
}
get
{
if (m_sDefaultColumn == null )
{
return String.Empty;
}
else
{
return m_sDefaultColumn;
}
}
}
// 绘制DataGridView以及下拉DataGridView
private void DrawDataGridView()
{
CRD.WinUI.Editors.DataGridView dataGridView = new CRD.WinUI.Editors.DataGridView();
dataGridView.RowHeadersVisible = false ;
dataGridView.RowTemplate.Resizable = DataGridViewTriState.False; // 固定行高不允许调节
dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView.BackgroundColor = SystemColors.ActiveCaptionText;
dataGridView.BorderStyle = BorderStyle.None;
dataGridView.ReadOnly = true ;
dataGridView.AllowUserToAddRows = false ;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView.Click += new EventHandler(dataGridView_Click);
// 设置DataGridView的数据源
CRD.WinUI.Forms.EntryForm frmDataSource = new CRD.WinUI.Forms.EntryForm();
frmDataSource.Controls.Add(dataGridView);
frmDataSource.SuspendLayout();
dataGridViewHost = new ToolStripControlHost(dataGridView);
dataGridViewHost.AutoSize = m_blPopupAutoSize;
dropDown = new ToolStripDropDown();
dropDown.Width = this .Width;
dropDown.Items.Add(dataGridViewHost);
}
public void dataGridView_Click( object sender, EventArgs e)
{
PopupGridView(e);
}
public string GetDataProperty( string sColumn) //
{
string sValue = "" ;
if (m_dgvRow != null )
{
if (DataGridView.Columns.Contains(sColumn))
{
sValue = m_dgvRow.Cells[sColumn].Value.ToString();
}
}
return sValue;
}
public void dataGridView_DoubleClick( object sender, EventArgs e)
{
PopupGridView(e);
}
// PopupGridView
/**/
/// <summary>
/// 弹出下拉表格并触发选择后事件
/// </summary>
/// <param name="e"></param>
private void PopupGridView(EventArgs e)
{
if (DataGridView.SelectedRows.Count > 0 )
{
m_dgvRow = DataGridView.SelectedRows[ 0 ];
if (m_sDefaultColumn != String.Empty)
{
string [] sColumnList = m_sDefaultColumn.Split( ' , ' );
foreach ( string sColumn in sColumnList)
{
if (DataGridView.Columns.Contains(sColumn))
{
Items.Clear();
Items.Add(m_dgvRow.Cells[sColumn].Value.ToString());
SelectedIndex = 0 ;
}
}
}
if (AfterSelector != null )
{
AfterSelector( this , e);
}
}
dropDown.Close();
}
private void ShowDropDown()
{
if (dropDown != null )
{
dataGridViewHost.Size = new Size(DropDownWidth - 2 , DropDownHeight);
dropDown.Show( this , 0 , this .Height);
}
}
// 重写方法
protected override void WndProc( ref Message m)
{
if (m.Msg == WM_LBUTTONDBLCLK || m.Msg == WM_LBUTTONDOWN || m.Msg == 0x114 || m.Msg == 0x115 )
{
ShowDropDown();
if (OnScroll != null )
{
OnScroll( this , m.Msg == 0x115 );
}
return ;
}
base .WndProc( ref m);
}
protected override void Dispose( bool disposing)
{
if (disposing)
{
if (dropDown != null )
{
dropDown.Dispose();
dropDown = null ;
}
}
base .Dispose(disposing);
}
}
}
欢迎大家转载,如有转载请注明文章来自: http://sufei.cnblogs.com/
签名:做一番一生引以为豪的事业;在有生之年报答帮过我的人;并有能力帮助需要帮助的人;
QQ:361983679 Email:sufei.1013@163.com MSN:sufei.1013@163.com