CSV文件写入到TCheckListBox中出问题
csv文档如下:
"RP7","RESPACK-4","118.745mm","11.246mm","T","90.00","4.7K""RP6","RESPACK-4","140.208mm","44.704mm","T","180.00","4.7K""P10","SIP-3-2MM","146.939mm","28.829mm","T","90.00","Header 3""U5","DW024_L","87.907mm","19.317mm","T","225.00","SN74LVC4245PW""R9","0805-MINI","116.332mm","57.785mm","T","270.00","1K""P9","SIP-6-2MM","67.31mm","13.97mm","T","270.00","Header 6""P8","SIP-6-2MM","67.31mm","30.099mm","T","270.00","Header 6""P7","SIP-6-2MM","67.31mm","46.228mm","T","270.00","Header 6""P6","SIP-8-2MM","48.133mm","46.228mm","T","270.00","Header 8"
void __fastcall TForm1::CheckListBox1DrawItem(TWinControl *Control, int Index, TRect &Rect, //重绘checklistbox TOwnerDrawState State){CheckListBox1 -> Canvas -> MoveTo(50, 0); //绘制元器件编号列,宽度50CheckListBox1 -> Canvas -> LineTo(50, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);CheckListBox1 -> Canvas -> MoveTo(150, 0); //绘制元器件封装类型列,宽度100CheckListBox1 -> Canvas -> LineTo(150, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);CheckListBox1 -> Canvas -> MoveTo(200, 0); //绘制焊盘中心X坐标列,宽度50CheckListBox1 -> Canvas -> LineTo(200, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);CheckListBox1 -> Canvas -> MoveTo(250, 0); //绘制焊盘中心Y坐标列,宽度50CheckListBox1 -> Canvas -> LineTo(250, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);CheckListBox1 -> Canvas -> MoveTo(270, 0); //绘制元件器所在层列,宽度20CheckListBox1 -> Canvas -> LineTo(270, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);CheckListBox1 -> Canvas -> MoveTo(320, 0); //绘制元件器放置方位角度列,宽度50CheckListBox1 -> Canvas -> LineTo(320, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);CheckListBox1 -> Canvas -> MoveTo(470, 0); //绘制元件器备注列,宽度150CheckListBox1 -> Canvas -> LineTo(470, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);}
//---------------------------------------#include <vcl.h>#pragma hdrstop#include "Unit1.h"//---------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TForm1 *Form1;//---------------------------------------#define CSVBOX_COL_COUNT 3#define CSVBOX_COL_SPACE 2typedef struct tagTCsvRecord{ String sCol[CSVBOX_COL_COUNT]; // ......}TCsvRecord;int __fastcall GetColWidth(int iIndex)// 可用其他任何方式决定列宽{ int nRet = 0; switch(iIndex) { case 0: nRet = 150; break; case 1: nRet = 150; break; case 2: nRet = 200; break; default: nRet = 150; break; } return nRet;}__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){ // 初始化测试数据 TCsvRecord *pCsvRecord = NULL; for(int i = 0; i < 20; ++ i) { pCsvRecord = new TCsvRecord(); for(int j = 0; j < CSVBOX_COL_COUNT; ++ j) { pCsvRecord->sCol[j] = "A" + IntToStr(i) + "Col" + IntToStr(j); } CheckListBox1->AddItem("", (TObject*)pCsvRecord); }}__fastcall TForm1::~TForm1(){ // 释放内存 // .......}//---------------------------------------bool FDrawAutoSize = false;void __fastcall TForm1::CheckListBox1DrawItem(TWinControl *Control, int Index, TRect &Rect, TOwnerDrawState State){ String s; TCanvas *Canvas = CheckListBox1->Canvas; int nItemHeight = CheckListBox1 -> ItemHeight; int nLineHeight = CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight; TCsvRecord* pRecord = (TCsvRecord*)this->CheckListBox1->Items->Objects[Index]; if(!FDrawAutoSize) { int nWidth = this->CheckListBox1->Width; int nHeight = this->CheckListBox1->Height; int nCurX = 0; int nCurColWidth = 0; for(int i = 0; i < CSVBOX_COL_COUNT; ++ i) { nCurColWidth = GetColWidth(i); // 具体位置自己调吧 TRect rcCol( nCurX + CSVBOX_COL_SPACE + Rect.Left, Rect.Top + CSVBOX_COL_SPACE, nCurX + nCurColWidth - CSVBOX_COL_SPACE + Rect.Left, Rect.Bottom - CSVBOX_COL_SPACE); nCurX += nCurColWidth; if(i != CSVBOX_COL_COUNT - 1) // 也许只需画 列数-1 条线就够了 { Canvas->MoveTo(nCurX, Rect.Top); Canvas->LineTo(nCurX, Rect.Bottom); } s = pRecord->sCol[i]; // 对齐方式自己调整 DWORD dwTextFmt = DT_CENTER | DT_VCENTER | DT_SINGLELINE; //if(FWordWrap) //{ //dwTextFmt ^= DT_SINGLELINE; //dwTextFmt |= DT_WORDBREAK; //} DrawText(Canvas->Handle, s.c_str(), s.Length(), &rcCol, dwTextFmt);// | DrawTextBiDiModeFlagsReadingOnly()); } } else { // 自己算吧。。。。 // 用 TextExtent 取得文字大小调整列宽和输出位置 }}//---------------------------------------
[解决办法]