首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

通过API来操作ListView,插入数据的时候报冲突.解决方法

2012-03-25 
通过API来操作ListView,插入数据的时候报冲突. - C++ Builder / Windows SDK/API通过API来操作ListView,插

通过API来操作ListView,插入数据的时候报冲突. - C++ Builder / Windows SDK/API
通过API来操作ListView,插入数据的时候报冲突.但是数据还是插进去了。
下面是全部的代码:::
//---------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
  : TForm(Owner) {
}
//---------------------------------------
void ListViewInsertItemText(HWND hListView, const int iItem, const int iSubItem, char *pszText) {
  LVITEM item;

  item.mask = LVIF_TEXT;
  item.iItem = iItem;
  item.iSubItem = iSubItem;
  item.state = 0;
  item.stateMask = 0;
  item.iImage = 0;
  item.lParam = 0;
  item.pszText = pszText;
  item.cchTextMax = (int)strlen(pszText);

  if (!iSubItem)
  SendMessage(hListView, LVM_INSERTITEM, (WPARAM)0, (LPARAM)&item); //屏蔽掉这句就没有问题了
  else
  SendMessage(hListView, LVM_SETITEMTEXT, (WPARAM)iItem, (LPARAM)&item);
};

void ListViewSetExtStyle(HWND hListView, const DWORD dwStyle) {
  DWORD style;

  style = (dwStyle) ? dwStyle : LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_FLATSB;
  SendMessage(hListView, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)style);
};

void ListViewInsertColumnText(HWND hListView, const short wIdx,
  const int wFmt, char *pszText, const BOOL bFinal) {
  LVCOLUMN column;
  TEXTMETRIC text;
  int nWidth;
  HDC hDC = ::GetDC(Form2->Handle);
  int fmt;

  GetTextMetrics(hDC, &text);
  nWidth = text.tmAveCharWidth*20;
  ReleaseDC(Form2->Handle, hDC);

  if (!wFmt)
  fmt = LVCFMT_LEFT;
  column.mask = LVCF_TEXT|LVCF_FMT|LVCF_WIDTH;
  column.pszText = pszText;
  column.fmt = fmt;

  if (!bFinal)
  column.cx = nWidth;
  else
  column.cx = nWidth + 100 ;

  if (SendMessage(hListView, LVM_INSERTCOLUMN, wIdx, (LPARAM)(LPLVCOLUMN)&column) == -1) {
  ShowMessage("1111111111111");
  }
};

void __fastcall TForm2::FormCreate(TObject *Sender) {
  HWND hListView1 = ListView1->Handle ;

  ListViewSetExtStyle(hListView1, 0);

  ListViewInsertColumnText(hListView1, 0, 0, "Field", 0);
  ListViewInsertColumnText(hListView1, 1, 0, "Descritption", 1);
  ListViewInsertItemText(hListView1, 0, 0, "你好啊");
  ListViewInsertItemText(hListView1, 1, 0, "测试DIY!");

}
//---------------------------------------


[解决办法]
为什么要用API呢?
[解决办法]
同上。

热点排行