- 积分
- 40165
- 好友
- 记录
- 主题
- 帖子
- 听众
- 收听
|
C#皮肤-ComboBoxDataGridView
导读部分
-------------------------------------------------------------------------------------------------------------
C#皮肤-实现原理系列文章导航
http://www.sufeinet.com/thread-2-1-1.html
大家还是先来看看效果吧
我个人认为这个控件是皮肤控件走向高端的第一步,很多人说让我把控件做的像什么,什么第三方的那种,其实最开始的是为了让大家知道怎么样使用C#单纯的Gdi+来实现,不过现在我感觉是时候改改了,
今天就以这个控件开始吧,接下来的皮肤会更高端一些,希望大家期待并支持,如果你感觉不错的话可以订阅哦
订阅网址:http://list.qq.com/cgi-bin/qf_invite?id=084ffdd8383d8ced5040839a4dfbcb1cf5511d43c9c31beb
下面我们一起来看看是怎么样实现 的
这个其实是控件里是没有的,那天在博客上看到一个同志在写这个控件,所性抄一点思想,在结合我和控件就加上了,还挺好用的,呵呵,有了这个控件大家就不用再为绑定一个表那个字段面苦了,可以一下自全部绑定,实现很简单大家一起来看看吧
第一步我们新建一个组件名称 为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();
方法吧
- 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);
- }
复制代码
有关DatagridView和Combox的实现 代码大家请参考我的皮肤系统,呵呵
下面我把代码都放上来各们看看吧也提提建议
- using System;
- 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);
- }
- }
- }
复制代码
|
|