代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
让ListView显示图形
作者:
Dezai.CN
/ 发布于
2011/9/21
/
605
<div>using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing; namespace 库存显示控件 { class mylistview : ListView { public struct ItemIndex { private int rowIndex; private int columnIndex; public ItemIndex(int rowIndex, int columnIndex) { this.rowIndex = rowIndex; this.columnIndex = columnIndex; } public int RowIndex { get { return rowIndex; } set { rowIndex = value; } } public int ColumnIndex { get { return columnIndex; } set { columnIndex = value; } } } private string subItemText = string.Empty; private ItemIndex itemindex; public mylistview() { this.GridLines = true; this.OwnerDraw = true; this.View = System.Windows.Forms.View.Details; this.MouseMove += new MouseEventHandler(mylistview_MouseMove); } void mylistview_MouseMove(object sender, MouseEventArgs e) { int columncount = this.Columns.Count; int rowcount = this.Items.Count; int[] clm = new int[columncount]; for (int i = 0; i < columncount; i++) { clm[i] = this.Items[0].SubItems[i].Bounds.X; } int colmn = 0; int x = e.X; for (int i = 0; i < columncount; i++) if (x < clm[i]) { colmn = i - 1; break; } if (clm[columncount - 1] < x) colmn = columncount - 1; try { int row = this.HitTest(e.X, e.Y).Item.Index; subItemText = this.Items[row].SubItems[colmn].Text; int pane = GetPaneLocation(this.Items[row].SubItems[colmn].Bounds, subItemText, e.X); itemindex = new ItemIndex(row, colmn); NewMouseMove(itemindex,pane); } catch { } } private int GetPaneLocation(Rectangle Rr,string part,int mouseX) { int pane = -1; if (part.Equals("0")) { return 1; } else { int width = Rr.Width / part.Length; while ((part.Length + 1 + width * part.Length) > Rr.Width) { width--; } int x = Rr.X + width + 2; for (int i = 0; i < part.Length; i++) { if (mouseX < x) { pane = i + 1; break; } x = x + width + 1; } return pane; } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); } protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e) { using (StringFormat sf = new StringFormat()) { switch (e.Header.TextAlign) { case HorizontalAlignment.Center: sf.Alignment = StringAlignment.Center; break; case HorizontalAlignment.Right: sf.Alignment = StringAlignment.Far; break; } e.DrawBackground(); using (Font headerFont = new Font("Helvetica", 10, FontStyle.Bold)) { e.Graphics.DrawString(e.Header.Text, headerFont, Brushes.Black, e.Bounds, sf); } } return; } protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e) { if (e.ColumnIndex > 0) { Graphics g = e.Graphics; Rectangle Rr = e.Bounds; SolidBrush brush1 = new SolidBrush(Color.Black); Rectangle Rr5 = new Rectangle(); Rr5.X = Rr.X + 1; Rr5.Y = Rr.Y + 1; Rr5.Width = Rr.Width - 2; Rr5.Height = Rr.Height - 3; g.FillRectangle(brush1, Rr5); string part = e.Item.SubItems[e.ColumnIndex].Text; if (part.Equals("0")) { Rectangle Rr3 = new Rectangle(); SolidBrush brush = new SolidBrush(Color.Gray); Rr3.X = Rr5.X + 1; Rr3.Y = Rr5.Y + 1; Rr3.Height = Rr5.Height - 2; Rr3.Width = Rr5.Width - 2; g.FillRectangle(brush, Rr3); } else { int x = Rr5.X + 1; int y = Rr5.Y + 1; int width = Rr5.Width / part.Length; while ((part.Length + 1 + width * part.Length) > Rr5.Width) { width--; } int height = Rr5.Height - 2; for (int i = 0; i < part.Length; i++) { if (part[i] == '0') { Rectangle Rr3 = new Rectangle(); SolidBrush brush = new SolidBrush(Color.DarkKhaki); Rr3.Height = height; Rr3.Width = width; Rr3.X = x; Rr3.Y = y; g.FillRectangle(brush, Rr3); } //else if (part[i] == '1') //作为第三状态不是全满 //{ // Rectangle Rr3 = new Rectangle(); // SolidBrush brush = new SolidBrush(Color.Brown); // Rr3.Height = height; // Rr3.Width = width; // Rr3.X = x; // Rr3.Y = y; // g.FillRectangle(brush, Rr3); //} else { Rectangle Rr3 = new Rectangle(); SolidBrush brush = new SolidBrush(Color.Brown); Rr3.Height = height; Rr3.Width = width; Rr3.X = x; Rr3.Y = y; g.FillRectangle(brush, Rr3); } x = x + width + 1; if (i == part.Length - 2) { width = Rr5.Width - (width + 1) * (part.Length - 1) - 2; } } } g.Dispose(); } else { using (StringFormat sf = new StringFormat()) { switch (e.Header.TextAlign) { case HorizontalAlignment.Center: sf.Alignment = StringAlignment.Center; break; case HorizontalAlignment.Right: sf.Alignment = StringAlignment.Far; break; } e.Graphics.DrawString(e.SubItem.Text, this.Font, Brushes.Black, e.Bounds, sf); } } } public delegate void MouseMoveHendle(ItemIndex ItemBothIndex,int pane); public event MouseMoveHendle NewMouseMove; } } <img style="position: relative; display: none" id="paperPicArea1" title="C#让ListView显示图形" name="paperPicArea1" alt="C#让ListView显示图形" src="http://cnc.imgcache.qq.com/ac/b.gif" /></div>
试试其它关键字
ListView
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3