winform 高级特效的问题。 (framework1.1) 本人想彻底改善listview, tabcontrol两个控件的界面,可是这两个控间并没有提供owner draw,请大家指条路,推荐一本高级点的书,讲讲datagrid这类复杂界面的实现。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 贴个完整的例子给你,自己去研究一下。这个例子里继承 ListBox,实现 ListBoxItem 奇偶行颜色不同,鼠标拖动一个Item实现重新排序等功能。using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Data;using System.Windows.Forms;namespace rsWinLib.WinControls{ public delegate void evListBoxMoverDrawItem(Graphics g, Rectangle rect, int index, DrawItemEventArgs poArgs); /// <summary> /// 可以排序的ListBox /// songyf 200606 v1.0.0 /// </summary> public class ListBoxMover : System.Windows.Forms.ListBox { private System.Windows.Forms.ImageList imageList; private System.ComponentModel.IContainer components; private int _LastIndex = -1; private int _ItemHeight = 0; private int _MouseDownX = -1; private bool _AlternateBackColor = true; protected int ItemSpace = 3; public event evListBoxMoverDrawItem OnDrawItemContent; public ListBoxMover() { InitializeComponent(); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ListBoxMover)); this.imageList = new System.Windows.Forms.ImageList(this.components); this.SuspendLayout(); // // imageList // this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream"))); this.imageList.TransparentColor = System.Drawing.Color.Transparent; this.imageList.Images.SetKeyName(0, "Move.bmp"); // // ListBoxMover // this.AllowDrop = true; this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; this.ItemHeight = 20; this.DragDrop += new System.Windows.Forms.DragEventHandler(this.ListBoxMover_DragDrop); this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ListBoxMover_DrawItem); this.DragLeave += new System.EventHandler(this.ListBoxMover_DragLeave); this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.ListBoxMover_MouseMove); this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ListBoxMover_MouseDown); this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ListBoxMover_KeyDown); this.DragOver += new System.Windows.Forms.DragEventHandler(this.ListBoxMover_DragOver); this.ResumeLayout(false); } #endregion public bool AlternateBackColor { get { return _AlternateBackColor; } set { _AlternateBackColor = value; } } private void ListBoxMover_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { string lcString = null; if (e.Index >= 0 && e.Index < this.Items.Count) lcString = this.Items[e.Index].ToString(); if (lcString == null && this.DesignMode) lcString = this.Name; if (lcString != null) { int liMoverHeight = this.imageList.Images[0].Height; int liMoverWidth = this.imageList.Images[0].Height; _ItemHeight = Math.Min(this.ItemHeight - 4, Math.Min(liMoverHeight, liMoverWidth)); Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y + (e.Bounds.Height - _ItemHeight) / 2, _ItemHeight, _ItemHeight); e.Graphics.FillRectangle(new SolidBrush(SystemColors.Control), e.Bounds.X, e.Bounds.Y, _ItemHeight, e.Bounds.Height); e.Graphics.DrawImage(this.imageList.Images[0], rect); rect = new Rectangle(_ItemHeight, e.Bounds.Y, e.Bounds.Width - _ItemHeight, e.Bounds.Height); if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) e.Graphics.FillRectangle(new SolidBrush(SystemColors.HotTrack), rect); else { if (e.Index % 2 == 0 || !this.AlternateBackColor) e.Graphics.FillRectangle(new SolidBrush(e.BackColor), rect); else e.Graphics.FillRectangle(new SolidBrush(Color.AliceBlue), rect); } rect = new Rectangle(e.Bounds.X + _ItemHeight + 2, e.Bounds.Y + ItemSpace, e.Bounds.Width - _ItemHeight - 2, e.Bounds.Height - 2 * ItemSpace); if (OnDrawItemContent == null) { SizeF loSize = e.Graphics.MeasureString("A", this.Font, this.Width, StringFormat.GenericDefault); int liW = (int)Math.Ceiling(loSize.Height); // e.Bounds.Height; int liStartY = e.Bounds.Y + (e.Bounds.Height - liW) / 2; e.Graphics.DrawString(lcString, e.Font, new SolidBrush(e.ForeColor), liW, liStartY, StringFormat.GenericDefault); } else { OnDrawItemContent(e.Graphics, rect, e.Index, e); } e.DrawFocusRectangle(); } } 接上面 private void ListBoxMover_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Left) { int liIndex = this.IndexFromPoint(e.X, e.Y); if (liIndex >= 0 && this._MouseDownX <= this._ItemHeight) { _LastIndex = -1; this.DoDragDrop(this.Items[liIndex], DragDropEffects.Move); } } } private void RedrawLastItem() { if (_LastIndex < this.Items.Count && _LastIndex >= 0) this.Invalidate(this.GetItemRectangle(_LastIndex)); } private void ListBoxMover_DragOver(object sender, System.Windows.Forms.DragEventArgs e) { e.Effect = DragDropEffects.None; if (this.Items.Count > 0) { Type loType = this.Items[0].GetType(); if (e.Data.GetDataPresent(loType)) { object loItem = e.Data.GetData(loType); Point loPoint = this.PointToClient(new Point(e.X, e.Y)); int liIndex = this.IndexFromPoint(loPoint.X, loPoint.Y); int liDragged = this.Items.IndexOf(loItem); if (loPoint.X <= this._ItemHeight && liIndex >= 0 && liDragged >= 0 && liDragged != liIndex - 1 && liDragged != liIndex) { e.Effect = DragDropEffects.Move; if (_LastIndex != liIndex) { this.RedrawLastItem(); Rectangle loRect = this.GetItemRectangle(liIndex); loRect.Height = loRect.Height - 1; this.CreateGraphics().DrawRectangle(new Pen(new SolidBrush(Color.Red), 1), loRect); } _LastIndex = liIndex; } } } if (e.Effect == DragDropEffects.None && this._LastIndex >= 0) { this.RedrawLastItem(); this._LastIndex = -1; } } private void ListBoxMover_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { if (this.Items.Count > 0) { Type loType = this.Items[0].GetType(); if (e.Data.GetDataPresent(loType)) { object loItem = e.Data.GetData(loType); Point loPoint = this.PointToClient(new Point(e.X, e.Y)); int liIndex = this.IndexFromPoint(loPoint.X, loPoint.Y); int liDragged = this.Items.IndexOf(loItem); this.Items.Remove(loItem); if (liDragged > liIndex) this.Items.Insert(liIndex, loItem); if (liDragged < liIndex) this.Items.Insert(liIndex - 1, loItem); this.SelectedItem = loItem; } } } private void ListBoxMover_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { int liIndex = this.SelectedIndex; if (e.Shift && liIndex >= 0) { object loItem = this.Items[liIndex]; switch (e.KeyCode) { case Keys.Down: if (liIndex < this.Items.Count - 1) { this.Items.Remove(loItem); this.Items.Insert(liIndex + 1, loItem); this.SelectedIndex = liIndex + 1; e.Handled = true; } break; case Keys.Up: if (liIndex > 0) { this.Items.Remove(loItem); this.Items.Insert(liIndex - 1, loItem); this.SelectedIndex = liIndex - 1; e.Handled = true; } break; default: break; } } } private void ListBoxMover_DragLeave(object sender, System.EventArgs e) { RedrawLastItem(); } private void ListBoxMover_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { this._MouseDownX = e.X; } public void RemoveSelectedItems() { if (this.SelectedItems != null) { Array loList = Array.CreateInstance(typeof(System.Object), this.SelectedItems.Count); this.SelectedItems.CopyTo(loList, 0); foreach (object loObj in loList) this.Items.Remove(loObj); } } }} listview 不是listbox, listbox太简单了,我也会 自己写一个tabpage也不是特别难 C# 如何生成控件组 gridview删除问题 求c# web 实现报表打印 C#中 如何编写 向系统查找与输入PID想匹配的进程是否存在 怎么样才能做个文章采集功能啊 C#菜鸟求救!大家帮帮忙啊! break和continue只能在循环,switch里用吗? 怪事!四舍五入的新问题(有代码) Xml文档的读写有几种方法,请简单介绍? 如果我想让一个程序一天执行两次怎样做?? [Diamond制作]组团行动,挖掘自己! picturebox加载图片时如何加上进度条
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;namespace rsWinLib.WinControls
{
public delegate void evListBoxMoverDrawItem(Graphics g, Rectangle rect, int index, DrawItemEventArgs poArgs);
/// <summary>
/// 可以排序的ListBox
/// songyf 200606 v1.0.0
/// </summary>
public class ListBoxMover : System.Windows.Forms.ListBox
{
private System.Windows.Forms.ImageList imageList;
private System.ComponentModel.IContainer components; private int _LastIndex = -1;
private int _ItemHeight = 0;
private int _MouseDownX = -1; private bool _AlternateBackColor = true;
protected int ItemSpace = 3; public event evListBoxMoverDrawItem OnDrawItemContent;
public ListBoxMover()
{
InitializeComponent();
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
} #region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ListBoxMover));
this.imageList = new System.Windows.Forms.ImageList(this.components);
this.SuspendLayout();
//
// imageList
//
this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream")));
this.imageList.TransparentColor = System.Drawing.Color.Transparent;
this.imageList.Images.SetKeyName(0, "Move.bmp");
//
// ListBoxMover
//
this.AllowDrop = true;
this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.ItemHeight = 20;
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.ListBoxMover_DragDrop);
this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ListBoxMover_DrawItem);
this.DragLeave += new System.EventHandler(this.ListBoxMover_DragLeave);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.ListBoxMover_MouseMove);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ListBoxMover_MouseDown);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ListBoxMover_KeyDown);
this.DragOver += new System.Windows.Forms.DragEventHandler(this.ListBoxMover_DragOver);
this.ResumeLayout(false); }
#endregion public bool AlternateBackColor
{
get { return _AlternateBackColor; }
set { _AlternateBackColor = value; }
}
private void ListBoxMover_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
string lcString = null;
if (e.Index >= 0 && e.Index < this.Items.Count)
lcString = this.Items[e.Index].ToString(); if (lcString == null && this.DesignMode)
lcString = this.Name; if (lcString != null)
{
int liMoverHeight = this.imageList.Images[0].Height;
int liMoverWidth = this.imageList.Images[0].Height;
_ItemHeight = Math.Min(this.ItemHeight - 4, Math.Min(liMoverHeight, liMoverWidth));
Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y + (e.Bounds.Height - _ItemHeight) / 2,
_ItemHeight, _ItemHeight);
e.Graphics.FillRectangle(new SolidBrush(SystemColors.Control),
e.Bounds.X, e.Bounds.Y, _ItemHeight, e.Bounds.Height);
e.Graphics.DrawImage(this.imageList.Images[0], rect); rect = new Rectangle(_ItemHeight, e.Bounds.Y,
e.Bounds.Width - _ItemHeight, e.Bounds.Height);
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
e.Graphics.FillRectangle(new SolidBrush(SystemColors.HotTrack), rect);
else
{
if (e.Index % 2 == 0 || !this.AlternateBackColor)
e.Graphics.FillRectangle(new SolidBrush(e.BackColor), rect);
else
e.Graphics.FillRectangle(new SolidBrush(Color.AliceBlue), rect);
} rect = new Rectangle(e.Bounds.X + _ItemHeight + 2, e.Bounds.Y + ItemSpace,
e.Bounds.Width - _ItemHeight - 2, e.Bounds.Height - 2 * ItemSpace); if (OnDrawItemContent == null)
{
SizeF loSize = e.Graphics.MeasureString("A", this.Font,
this.Width, StringFormat.GenericDefault); int liW = (int)Math.Ceiling(loSize.Height); // e.Bounds.Height;
int liStartY = e.Bounds.Y + (e.Bounds.Height - liW) / 2; e.Graphics.DrawString(lcString, e.Font,
new SolidBrush(e.ForeColor), liW, liStartY, StringFormat.GenericDefault);
}
else
{
OnDrawItemContent(e.Graphics, rect, e.Index, e);
} e.DrawFocusRectangle();
}
}
private void ListBoxMover_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int liIndex = this.IndexFromPoint(e.X, e.Y);
if (liIndex >= 0 && this._MouseDownX <= this._ItemHeight)
{
_LastIndex = -1;
this.DoDragDrop(this.Items[liIndex], DragDropEffects.Move);
}
}
} private void RedrawLastItem()
{
if (_LastIndex < this.Items.Count && _LastIndex >= 0)
this.Invalidate(this.GetItemRectangle(_LastIndex));
}
private void ListBoxMover_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.None; if (this.Items.Count > 0)
{
Type loType = this.Items[0].GetType(); if (e.Data.GetDataPresent(loType))
{
object loItem = e.Data.GetData(loType);
Point loPoint = this.PointToClient(new Point(e.X, e.Y)); int liIndex = this.IndexFromPoint(loPoint.X, loPoint.Y);
int liDragged = this.Items.IndexOf(loItem);
if (loPoint.X <= this._ItemHeight && liIndex >= 0 &&
liDragged >= 0 && liDragged != liIndex - 1 && liDragged != liIndex)
{
e.Effect = DragDropEffects.Move; if (_LastIndex != liIndex)
{
this.RedrawLastItem();
Rectangle loRect = this.GetItemRectangle(liIndex);
loRect.Height = loRect.Height - 1;
this.CreateGraphics().DrawRectangle(new Pen(new SolidBrush(Color.Red), 1),
loRect);
}
_LastIndex = liIndex;
}
}
} if (e.Effect == DragDropEffects.None && this._LastIndex >= 0)
{
this.RedrawLastItem();
this._LastIndex = -1;
}
} private void ListBoxMover_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
if (this.Items.Count > 0)
{
Type loType = this.Items[0].GetType();
if (e.Data.GetDataPresent(loType))
{
object loItem = e.Data.GetData(loType);
Point loPoint = this.PointToClient(new Point(e.X, e.Y)); int liIndex = this.IndexFromPoint(loPoint.X, loPoint.Y);
int liDragged = this.Items.IndexOf(loItem);
this.Items.Remove(loItem);
if (liDragged > liIndex)
this.Items.Insert(liIndex, loItem); if (liDragged < liIndex)
this.Items.Insert(liIndex - 1, loItem); this.SelectedItem = loItem;
}
}
} private void ListBoxMover_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
int liIndex = this.SelectedIndex;
if (e.Shift && liIndex >= 0)
{
object loItem = this.Items[liIndex];
switch (e.KeyCode)
{
case Keys.Down:
if (liIndex < this.Items.Count - 1)
{
this.Items.Remove(loItem);
this.Items.Insert(liIndex + 1, loItem);
this.SelectedIndex = liIndex + 1;
e.Handled = true;
}
break;
case Keys.Up:
if (liIndex > 0)
{
this.Items.Remove(loItem);
this.Items.Insert(liIndex - 1, loItem);
this.SelectedIndex = liIndex - 1;
e.Handled = true;
}
break;
default:
break;
}
}
} private void ListBoxMover_DragLeave(object sender, System.EventArgs e)
{
RedrawLastItem();
} private void ListBoxMover_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
this._MouseDownX = e.X;
} public void RemoveSelectedItems()
{
if (this.SelectedItems != null)
{
Array loList = Array.CreateInstance(typeof(System.Object), this.SelectedItems.Count);
this.SelectedItems.CopyTo(loList, 0);
foreach (object loObj in loList)
this.Items.Remove(loObj);
}
}
}
}