求教:关于C#中ListView控件Item图标大小的问题

njustfengkj 2013-09-24 07:47:46
是最近的一个项目,需要做一些修改,其中就是ListView中Item的图表需要改大一点。其实图表本身的大小是32×32的,图中的图标远小于其实际尺寸,不知为何,而且图标也没有显示完全。这点感觉没有MFC中的ListCtrl好用。

下面附上代码,希望有人能够给点指导,怎样才能让图标显示其正常尺寸。



//设置ListView中Item的间距,引入相关的动态链接库
[DllImport("User32.dll")]
//声明需要用到的消息发送函数
private static extern int SendMessage(int Handle, int wMsg, int wParam, int lParam);
//需要用到的相关参数
const int LVM_FIRST = 0x1000;
const int LVM_SETICONSPACING = LVM_FIRST + 53;
//动态库中的系统函数,用来设置ListView控件中Item的间距
public static void SetListViewItemSpacing(ListView lst, int x, int y)
{
SendMessage(lst.Handle.ToInt32(), LVM_SETICONSPACING, 0, y * 65536 + x);
}

//窗体构造函数
public FormOperate()
{
InitializeComponent();
}

//串口初始化函数
private int InitComm()
{
serialPort1.PortName = "COM9";
serialPort1.BaudRate = 38400;
serialPort1.DataBits = 8;
serialPort1.StopBits = StopBits.One;
serialPort1.Parity = Parity.None;
serialPort1.Handshake = Handshake.None;
return 0;
}

//网络接收初始化函数
private bool InitSocket()
{
IPEndPoint e = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6000);
UdpClient u = new UdpClient(e);
m_udpState.u = u;
u.BeginReceive(new AsyncCallback(ReceiveCallback), m_udpState);
return true;
}

//异步接收的回调函数,网络数据的处理
public void ReceiveCallback(IAsyncResult ar)
{
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;

Byte[] receiveBytes = u.EndReceive(ar, ref m_udpState.e);
u.BeginReceive(new AsyncCallback(ReceiveCallback), ar.AsyncState);//重新开启异步接收
//接收到的位置序列分列到AB各自的序列
byte[] posListA = new byte[128];
byte[] posListB = new byte[128];
int posListCountA = 0, posListCountB = 0;
m_recipelCountTotal = receiveBytes.Length;
for (int i = 0; i < receiveBytes.Length; i++)
{
if (receiveBytes[i] >= 0)
{
posListA[posListCountA++] = receiveBytes[i];
}
else
{
posListB[posListCountB++] = (byte)(receiveBytes[i] - 128);
}
}

//把AB位置序列发送出去
serialPort1.ReceivedBytesThreshold = 1;
serialPort1.Write(posListA, 0, posListCountA);
System.Threading.Thread.Sleep(300);
serialPort1.ReceivedBytesThreshold = 1;
serialPort1.Write(posListB, 0, posListCountB);
}

//窗口载入时完成的工作
private void FormOperate_Load(object sender, EventArgs e)
{
listView1.LargeImageList = imageList1;
for (int i = 0; i < 128; i++)
{
SetListViewItemSpacing(listView1, 42, 64);
listView1.Items.Add(i.ToString(), 1);
}
for (int i = 0; i < 24; i++)
{
listView1.Items.Add(i.ToString(), 1);
}

listView2.LargeImageList = imageList1;
for (int i = 0; i < 128; i++)
{
SetListViewItemSpacing(listView2, 42, 64);
listView2.Items.Add(i.ToString(), 1);
}
for (int i = 0; i < 24; i++)
{
listView2.Items.Add(i.ToString(), 1);
}

InitSocket();

InitComm();

serialPort1.Open();
byte[] sendBuf = new byte[5];
sendBuf[0] = 0xda;
sendBuf[1] = 0xdd;
sendBuf[2] = 0x02;
sendBuf[3] = 0x01;
sendBuf[4] = 0x00;
serialPort1.Write(sendBuf, 0, 5);
System.Threading.Thread.Sleep(500);
sendBuf[3] = 0x02;
serialPort1.Write(sendBuf, 0, 5);
}
...全文
1376 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liutian1234567890 2015-06-15
  • 打赏
  • 举报
回复
引用 4 楼 u011693803 的回复:
[quote=引用 1 楼 caozhy 的回复:] imageList1里面的图标决定了显示在ListView上的图标的大小。
多谢!确实如此,不过图片的分辨率好像还是16×16的,不是其原始的32×32[/quote]我试了试不管用啊,因为我的listview的模式是Details所以设置了imagelist的大小但是图标还是原来的大小并没有变大,
henryMessi 2014-05-04
  • 打赏
  • 举报
回复
Largeimaglist.ImageSize = new Size(32,32);
wirner 2013-09-25
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
imageList1里面的图标决定了显示在ListView上的图标的大小。
说的没错, ImageList的属性,修改图片的宽和高.就可以了.
men52676521 2013-09-25
  • 打赏
  • 举报
回复
仔细找找应该有设置大小的属性。
njustfengkj 2013-09-25
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
imageList1里面的图标决定了显示在ListView上的图标的大小。
多谢!确实如此,不过图片的分辨率好像还是16×16的,不是其原始的32×32
threenewbee 2013-09-24
  • 打赏
  • 举报
回复
imageList1里面的图标决定了显示在ListView上的图标的大小。

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧