listView1.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(listView1_DrawColumnHeader);
//listView1.DrawItem += new DrawListViewItemEventHandler(MyListView_DrawItem);
listView1.DrawSubItem += new DrawListViewSubItemEventHandler(MyListView_DrawSubItem);
//listView1.DrawSubItem += new DrawListViewSubItemEventHandler(listView1_DrawSubItem);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Windows.Forms;
namespace testcsharp1
{
public partial class Form2 : Form
{
private void listView1_DrawColumnHeader(object sender,
DrawListViewColumnHeaderEventArgs e)
{
using (StringFormat sf = new StringFormat())
{
// Store the column text alignment, letting it default
// to Left if it has not been set to Center or Right.
switch (e.Header.TextAlign)
{
case HorizontalAlignment.Center:
sf.Alignment = StringAlignment.Center;
break;
case HorizontalAlignment.Right:
sf.Alignment = StringAlignment.Far;
break;
}
// Draw the standard header background.
e.DrawBackground();
// Draw the header text.
using (Font headerFont =
new Font("Helvetica", 10, FontStyle.Bold))
{
e.Graphics.DrawString(e.Header.Text, headerFont,
Brushes.Black, e.Bounds, sf);
}
}
return;
}
private void MyListView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
using (StringFormat sf = new StringFormat())
{
// Store the column text alignment, letting it default
// to Left if it has not been set to Center or Right.
switch (e.Header.TextAlign)
{
case HorizontalAlignment.Center:
sf.Alignment = StringAlignment.Center;
break;
case HorizontalAlignment.Right:
sf.Alignment = StringAlignment.Far;
break;
}
// Draw the standard header background.
e.DrawBackground();
// Draw the header text.
using (Font headerFont =
new Font("宋体", 10, FontStyle.Regular))
{
//e.Graphics.FillRectangle(Brushes.Pink, e.Bounds);
Rectangle r2 = e.Bounds;
r2.X = r2.X + 1;
//Brushes.BurlyWood,
e.Graphics.FillRectangle(Brushes.SkyBlue, r2);
Rectangle r1 = e.Bounds;
r1.Y = r1.Y + 4;
//e.Graphics.DrawString(e.Header.Text, headerFont, Brushes.Black, e.Bounds, sf);
e.Graphics.DrawString(e.Header.Text, headerFont, Brushes.Black, r1, sf);
/*
Point tPoint = new Point();
SolidBrush tFrontBrush = new SolidBrush(Color.Black);
Font tFont = new Font("宋体", 9, FontStyle.Regular);
tPoint.X = e.Bounds.X ;
tPoint.Y = e.Bounds.Y + 8;
e.Graphics.DrawString(e.Header.Text, tFont, tFrontBrush, tPoint);
*/
}
}
return;
}
private void MyListView_DrawItem(object sender, DrawListViewItemEventArgs e)
{
/*
e.DrawBackground();
e.DrawFocusRectangle();
if ((e.State & ListViewItemStates.Selected) != 0)
{
// Draw the background and focus rectangle for a selected item.
e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
e.DrawFocusRectangle();
}
else
{
// Draw the background for an unselected item.
using (LinearGradientBrush brush =
new LinearGradientBrush(e.Bounds, Color.White,
Color.Maroon, LinearGradientMode.Horizontal))
{
e.Graphics.FillRectangle(brush, e.Bounds);
}
}
return;*/
if ((e.State & ListViewItemStates.Selected) != 0)
{
// Draw the background and focus rectangle for a selected item.
e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
e.DrawFocusRectangle();
}
else
{
// Draw the background for an unselected item.
using (LinearGradientBrush brush =
new LinearGradientBrush(e.Bounds, Color.Orange,
Color.Maroon, LinearGradientMode.Horizontal))
{
e.Graphics.FillRectangle(brush, e.Bounds);
}
}
e.DrawText();
// Draw the item text for views other than the Details view.
if (listView1.View != View.Details)
{
e.DrawText();
}
//e.DrawDefault = true;
return;
e.DrawBackground();
e.DrawFocusRectangle();
return;
}
private void MyListView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
// e.DrawDefault = true;
// return;
if (e.ItemIndex == 1)
{
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawString(e.SubItem.Text, listView1.Font, Brushes.Red, e.Bounds, sf);
}
}
else e.DrawText();
return;
}
private void listView1_MouseMove(object sender, MouseEventArgs e)
{
ListViewItem item = listView1.GetItemAt(e.X, e.Y);
if (item != null && item.Tag == null)
{
listView1.Invalidate(item.Bounds);
item.Tag = "tagged";
}
}
private void listView1_DrawItem(object sender,
DrawListViewItemEventArgs e)
{
if ((e.State & ListViewItemStates.Selected) != 0)
{
// Draw the background and focus rectangle for a selected item.
e.Graphics.FillRectangle(Brushes.SkyBlue, e.Bounds);
e.DrawFocusRectangle();
}
else
{
// Draw the background for an unselected item.
using (LinearGradientBrush brush =
new LinearGradientBrush(e.Bounds, Color.White,
Color.White, LinearGradientMode.Horizontal))
{
e.Graphics.FillRectangle(brush, e.Bounds);
}
}
// Draw the item text for views other than the Details view.
if (listView1.View != View.Details)
{
e.DrawText();
}
}
// Draws subitem text and applies content-based formatting.
private void listView1_DrawSubItem(object sender,
DrawListViewSubItemEventArgs e)
{
TextFormatFlags flags = TextFormatFlags.Left;
using (StringFormat sf = new StringFormat())
{
// Store the column text alignment, letting it default
// to Left if it has not been set to Center or Right.
switch (e.Header.TextAlign)
{
case HorizontalAlignment.Center:
sf.Alignment = StringAlignment.Center;
flags = TextFormatFlags.HorizontalCenter;
break;
case HorizontalAlignment.Right:
sf.Alignment = StringAlignment.Far;
flags = TextFormatFlags.Right;
break;
}
// Draw the text and background for a subitem with a
// negative value.
double subItemValue;
if (e.ColumnIndex > 0 && Double.TryParse(
e.SubItem.Text, NumberStyles.Currency,
NumberFormatInfo.CurrentInfo, out subItemValue) &&
subItemValue < 0)
{
// Unless the item is selected, draw the standard
// background to make it stand out from the gradient.
if ((e.ItemState & ListViewItemStates.Selected) == 0)
{
e.DrawBackground();
}
// Draw the subitem text in red to highlight it.
e.Graphics.DrawString(e.SubItem.Text,
listView1.Font, Brushes.White, e.Bounds, sf);
return;
}
// Draw normal text for a subitem with a nonnegative
// or nonnumerical value.
if (e.ItemIndex == 1)
{
using (StringFormat sf1 = new StringFormat())
{
sf1.Alignment = StringAlignment.Center;
e.Graphics.DrawString(e.SubItem.Text, listView1.Font, Brushes.Red, e.Bounds, sf1);
}
}else
e.DrawText(flags);
}
}
// Resets the item tags.
void listView1_Invalidated(object sender, InvalidateEventArgs e)
{
foreach (ListViewItem item in listView1.Items)
{
if (item == null) return;
item.Tag = null;
}
}
// Forces the entire control to repaint if a column width is changed.
void listView1_ColumnWidthChanged(object sender,
ColumnWidthChangedEventArgs e)
{
listView1.Invalidate();
}
private void listView1_MouseUp(object sender, MouseEventArgs e)
{
ListViewItem clickedItem = listView1.GetItemAt(5, e.Y);
if (clickedItem != null)
{
clickedItem.Selected = true;
clickedItem.Focused = true;
}
}
private ListView listView1 = new ListView();
public void Form21()
{
this.AutoSize = true;
// Initialize the ListView control.
listView1.BackColor = Color.Black;
listView1.ForeColor = Color.White;
listView1.Dock = DockStyle.Fill;
listView1.View = View.Details;
listView1.FullRowSelect = true;
// Add columns to the ListView control.
listView1.Columns.Add("Name", 100, HorizontalAlignment.Center);
listView1.Columns.Add("First", 100, HorizontalAlignment.Center);
listView1.Columns.Add("Second", 100, HorizontalAlignment.Center);
listView1.Columns.Add("Third", 100, HorizontalAlignment.Center);
// Create items and add them to the ListView control.
ListViewItem listViewItem1 = new ListViewItem(new string[] { "One", "20", "30", "-40" }, -1);
ListViewItem listViewItem2 = new ListViewItem(new string[] { "Two", "-250", "145", "37" }, -1);
ListViewItem listViewItem3 = new ListViewItem(new string[] { "Three", "200", "800", "-1,001" }, -1);
ListViewItem listViewItem4 = new ListViewItem(new string[] { "Four", "not available", "-2", "100" }, -1);
listView1.Items.AddRange(new ListViewItem[] { listViewItem1, listViewItem2, listViewItem3, listViewItem4 });
// Initialize the shortcut menu and
// assign it to the ListView control.
/*
contextMenu1.MenuItems.Add("List",
new EventHandler(menuItemList_Click));
contextMenu1.MenuItems.Add("Details",
new EventHandler(menuItemDetails_Click));
listView1.ContextMenu = contextMenu1;
*/
// Configure the ListView control for owner-draw and add
// handlers for the owner-draw events.
listView1.OwnerDraw = true;
listView1.DrawItem += new
DrawListViewItemEventHandler(listView1_DrawItem);
listView1.DrawSubItem += new
DrawListViewSubItemEventHandler(listView1_DrawSubItem);
listView1.DrawColumnHeader += new
DrawListViewColumnHeaderEventHandler(listView1_DrawColumnHeader);
// Add a handler for the MouseUp event so an item can be
// selected by clicking anywhere along its width.
listView1.MouseUp += new MouseEventHandler(listView1_MouseUp);
// Add handlers for various events to compensate for an
// extra DrawItem event that occurs the first time the mouse
// moves over each row.
listView1.MouseMove += new MouseEventHandler(listView1_MouseMove);
/* listView1.ColumnWidthChanged += new ColumnWidthChangedEventHandler(listView1_ColumnWidthChanged);
listView1.Invalidated += new InvalidateEventHandler(listView1_Invalidated);
*/
// Initialize the form and add the ListView control to it.
this.ClientSize = new Size(450, 150);
//this.FormBorderStyle = FormBorderStyle.FixedSingle;
//this.MaximizeBox = false;
this.Text = "ListView OwnerDraw Example";
this.Controls.Add(listView1);
}
public Form2()
{
//InitializeComponent();
//--------------------------------------------
//listViewTestItems.OwnerDraw = true;
//listView1.GridLines = true;
/*
this.AutoSize = true;
// Initialize the ListView control.
listView1.BackColor = Color.Black;
listView1.ForeColor = Color.White;
listView1.Dock = DockStyle.Fill;
listView1.View = View.Details;
listView1.FullRowSelect = true;
*/
this.AutoSize = true;
//listView1.BackColor = Color.Black;
//listView1.ForeColor = Color.White;
listView1.Dock = DockStyle.Fill;
this.listView1.View = View.Details;
//listView1.HideSelection = false; //原来关键在这
listView1.FullRowSelect = true;
this.listView1.MultiSelect = false;
//listViewTestItems.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(MyListView_DrawColumnHeader);
listView1.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(listView1_DrawColumnHeader);
//listView1.DrawItem += new DrawListViewItemEventHandler(MyListView_DrawItem);
listView1.DrawItem += new DrawListViewItemEventHandler(listView1_DrawItem);
//listView1.DrawSubItem += new DrawListViewSubItemEventHandler(MyListView_DrawSubItem);
listView1.DrawSubItem += new DrawListViewSubItemEventHandler(listView1_DrawSubItem);
listView1.OwnerDraw = true;
listView1.MouseMove += new MouseEventHandler(listView1_MouseMove);
listView1.ColumnWidthChanged += new ColumnWidthChangedEventHandler(listView1_ColumnWidthChanged);
listView1.Invalidated += new InvalidateEventHandler(listView1_Invalidated);
listView1.Dock = DockStyle.Fill;
listView1.View = View.Details;
listView1.FullRowSelect = true;
//--------------------------------------------
this.listView1.Columns.Add("测试项", 150, HorizontalAlignment.Center);
this.listView1.Columns.Add("内容", 250, HorizontalAlignment.Center);
this.listView1.Columns.Add("进展", 100, HorizontalAlignment.Center);
this.listView1.Columns.Add("结果", 150, HorizontalAlignment.Center);
ImageList imgList = new ImageList();
imgList.ImageSize = new Size(1, 20);
// 这里设置listView的SmallImageList ,用imgList将其撑大
listView1.SmallImageList = imgList;
this.listView1.Items.Clear();
ListViewItem lvi = new ListViewItem();
lvi.Text = "连接设备";
lvi.SubItems.Add("检查本地IP");
lvi.SubItems.Add("完成");
lvi.SubItems.Add("成功");
lvi.UseItemStyleForSubItems = false;
this.listView1.Items.Add(lvi);
ListViewItem lvi1 = new ListViewItem();
lvi1.Text = "LED测试";
lvi1.SubItems.Add("全灭失败"); //失败这一栏显示失败项???
lvi1.SubItems.Add("完成");
lvi1.SubItems.Add("失败");
lvi1.UseItemStyleForSubItems = false;
this.listView1.Items.Add(lvi1);
this.Controls.Add(listView1);
//this.listView1.Items[1].SubItems[3].BackColor = Color.Red;
this.listView1.Items[1].SubItems[3].ForeColor = Color.Red;
//this.Controls.Add(listView1);
}
private void button1_Click(object sender, EventArgs e)
{
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
//contextMenu1.Dispose();
}
base.Dispose(disposing);
}
}
}
/*
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
string localStrIp = "";
string ips = "";
List<String> allDevIps = new List<String> ();
allDevIps.Add("192.168.2.1");
allDevIps.Add("192.168.3.1");
foreach (var sip in allDevIps)
{
Console.WriteLine("IP1 Address: " + sip);
String []arr1 =sip.Split('.');
String ipseg = sip.Substring(0, sip.Length - arr1[arr1.Length - 1].Length);
Console.WriteLine("ipseg: "+ipseg);
}
foreach (var ip in ipEntry.AddressList)
{
string strip = ip.ToString();
Console.WriteLine("IP Address: " + strip);
bool b1 = strip.Contains("192.168.2.");
foreach (var sip in allDevIps)
{
Console.WriteLine("IP1 Address: " + sip);
String[] arr1 = sip.Split('.');
String ipseg = sip.Substring(0, sip.Length - arr1[arr1.Length - 1].Length);
Console.WriteLine("ipseg: " + ipseg);
}
}
*/