为什么我从TListBox(或TCustomListBox)派生出的组件,放到窗口上后,总是出现no parent window的错误?

Hopping 2002-09-04 08:45:20
为什么我从TListBox(或TCustomListBox)派生出的组件,放到窗口上后,总是出现no parent window的错误?高手请指教
...全文
46 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hopping 2002-09-04
  • 打赏
  • 举报
回复
to jishiping(JSP 季世平):
按照那方法,好象不行,还是出现那错误
jishiping 2002-09-04
  • 打赏
  • 举报
回复
看看下面这个贴子就可以了:
http://www.csdn.net/Expert/TopicView1.asp?id=853765
Hopping 2002-09-04
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------
SMediaFileInfo __fastcall TImageListBox::GetItemInfo(int nIndex)
{
SMediaFileInfo mfi;
FMedia = (TMediaFileInfo*)Items->Objects[nIndex];
if(FMedia)
mfi = FMedia->mfi;

return mfi;
}
//---------------------------------------------------------------------------
void __fastcall TImageListBox::SetItemCaption(int nIndex, String sCaption)
{
FMedia = (TMediaFileInfo*)Items->Objects[nIndex];
if(FMedia)
{
strcpy(FMedia->mfi.sFileCaption, sCaption.c_str());
Invalidate();
}
}
//---------------------------------------------------------------------------

/*int __fastcall TImageListBox::ReplaceItemData(int nIndex, SMediaFileInfo mfi)
{
int nRetValue = LB_ERR;

// Get pointer to associated datas (if any)
SMediaFileInfo* pMfi = (SMediaFileInfo*)Items->Objects[nIndex];
// If no datas exist create a new one
if(pMfi != NULL)
{
*pMfi = mfi;
}

return nRetValue;
} */
//---------------------------------------------------------------------------
void __fastcall TImageListBox::FreeResources()
{
while(Count)
{
/*FMedia = (TMediaFileInfo*)Items->Objects[Count - 1];
if(FMedia)
delete FMedia;*/
Items->Delete(Count - 1);
}
}
//---------------------------------------------------------------------------
void __fastcall TImageListBox::ResetContent(void)
{
FreeResources();
}
//---------------------------------------------------------------------------
int __fastcall TImageListBox::Move(int nOldIndex, int nNewIndex, BOOL bSetCurSel)
{
int nInsertedIndex = LB_ERR;
String sText;
SMediaFileInfo mfi;

// If index is out of range
if((UINT)nOldIndex >= (UINT)Count) return nInsertedIndex;
// Get item text
sText = Items->Strings[nOldIndex];
FMedia = (TMediaFileInfo*)Items->Objects[nOldIndex];
// Delete string
DeleteString(nOldIndex);
// Insert string at new position
nInsertedIndex = InsertString(nNewIndex, mfi);
// Restore associated data
// ReplaceItemData(nInsertedIndex, mfi);

// Select item
if(bSetCurSel && nInsertedIndex != LB_ERR)
Selected[nInsertedIndex] = true;

return nInsertedIndex;
}
//---------------------------------------------------------------------------
int __fastcall TImageListBox::GetImageFileType(String FileName)
{
String sExt = ExtractFileExt(FileName);

sExt.LowerCase();
if(sExt.IsEmpty())
return -1;

int nType = 0;
if (sExt == ".bmp") nType = CXIMAGE_FORMAT_BMP;
#if CXIMAGE_SUPPORT_JPG
else if (sExt==".jpg" || sExt==".jpeg") nType = CXIMAGE_FORMAT_JPG;
#endif
#if CXIMAGE_SUPPORT_GIF
else if (sExt == ".gif") nType = CXIMAGE_FORMAT_GIF;
#endif
#if CXIMAGE_SUPPORT_PNG
else if (sExt == "png") nType = CXIMAGE_FORMAT_PNG;
#endif
#if CXIMAGE_SUPPORT_MNG
else if (sExt=="mng"||sExt=="jng") nType = CXIMAGE_FORMAT_MNG;
#endif
#if CXIMAGE_SUPPORT_ICO
else if (sExt == "ico") nType = CXIMAGE_FORMAT_ICO;
#endif
#if CXIMAGE_SUPPORT_TIF
else if (sExt=="tiff"||sExt=="tif") nType = CXIMAGE_FORMAT_TIF;
#endif
#if CXIMAGE_SUPPORT_TGA
else if (sExt=="tga") nType = CXIMAGE_FORMAT_TGA;
#endif
#if CXIMAGE_SUPPORT_PCX
else if (sExt=="pcx") nType = CXIMAGE_FORMAT_PCX;
#endif
#if CXIMAGE_SUPPORT_WBMP
else if (sExt=="wbmp") nType = CXIMAGE_FORMAT_WBMP;
#endif
#if CXIMAGE_SUPPORT_WMF
else if (sExt=="wmf"||sExt=="emf") nType = CXIMAGE_FORMAT_WMF;
#endif
#if CXIMAGE_SUPPORT_J2K
else if (sExt=="j2k"||sExt=="jp2") nType = CXIMAGE_FORMAT_J2K;
#endif
#if CXIMAGE_SUPPORT_JBG
else if (sExt=="jbg") nType = CXIMAGE_FORMAT_JBG;
#endif
else nType = -1;

return nType;
}
//---------------------------------------------------------------------------
String __fastcall TImageListBox::GetItemCaption(int nIndex)
{
FMedia = (TMediaFileInfo*)Items->Objects[nIndex];
return FMedia->mfi.sFileCaption;
}
//---------------------------------------------------------------------------
Hopping 2002-09-04
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop

#include "ImageListBox.h"
#pragma package(smart_init)

//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//

static inline void ValidCtrCheck(TImageListBox *)
{
new TImageListBox(NULL);
}
//---------------------------------------------------------------------------
__fastcall TImageListBox::TImageListBox(TComponent* Owner)
: TListBox(Owner)
{
FMedia = NULL;

Style = lbOwnerDrawVariable;
ItemHeight = 52;
OnMeasureItem = OnImageMeasureItem;
OnDrawItem = OnImageDrawItem;
}

//---------------------------------------------------------------------------
__fastcall TImageListBox::~TImageListBox()
{
FreeResources();
}

//---------------------------------------------------------------------------
namespace Imagelistbox
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TImageListBox)};
RegisterComponents("HappySoft", classes, 0);
}
}
//---------------------------------------------------------------------------
// 增加一个图片文件
int __fastcall TImageListBox::AddString(SMediaFileInfo mfi)
{
int nIndex = LB_ERR;

FMedia = new TMediaFileInfo();
FMedia->mfi = mfi;
if(FMedia)
{
FMedia->image.Load(mfi.sFileName, GetImageFileType(mfi.sFileName));
// resample
FMedia->image.Resample(48, 48);
}
nIndex = Items->AddObject(mfi.sFileName, (TObject*)FMedia);
/* if(nIndex != LB_ERR)
ReplaceItemData(nIndex, mfi);
*/
return nIndex;
}
//---------------------------------------------------------------------------
int __fastcall TImageListBox::InsertString(int nIndex, SMediaFileInfo mfi)
{
// get file icon
FMedia = new TMediaFileInfo();
if(FMedia)
{
FMedia->image.Load(mfi.sFileName, GetImageFileType(mfi.sFileName));
// resample
FMedia->image.Resample(48, 48);
}
Items->InsertObject(nIndex, mfi.sFileName, (TObject*)FMedia);

return nIndex;
}
//---------------------------------------------------------------------------
void __fastcall TImageListBox::DeleteString(int Index)
{
/* FMedia = (TMediaFileInfo*)Items->Objects[Index];
if(FMedia)
delete FMedia;*/
Items->Delete(Index);
}
//---------------------------------------------------------------------------
void __fastcall TImageListBox::OnImageDrawItem(TWinControl* Control, int Index, const TRect& Rect,
TOwnerDrawState State)
{
// Canvas->Brush->Style = bsClear;
int nFontHeight = -(Canvas->Font->Height);
Canvas->FillRect(Rect);
FMedia = (TMediaFileInfo*)Items->Objects[Index];
SMediaFileInfo mfi = FMedia->mfi;

String sText;
int nLeft = Rect.left + 52;
// draw image
FMedia->image.Draw(Canvas->Handle, Rect.left + 2, Rect.Top + 2, 48, 48);
// name
sText = "File: " + ExtractFileName(mfi.sFileName);
Canvas->TextOut(nLeft + 2, Rect.Top + 2, sText);
// path
sText = "Path: " + ExtractFilePath(mfi.sFileName);
Canvas->TextOut(nLeft + 2, Rect.Top + nFontHeight + 3, sText);
// caption
sText = "Caption: " + String(mfi.sFileCaption);
Canvas->TextOut(nLeft + 2, Rect.Top + nFontHeight * 2 + 4, sText);
// size
sText = "Size: " + IntToStr(mfi.dwSize);
Canvas->TextOut(nLeft + 2, Rect.Top + nFontHeight * 3 + 5, sText);
}
//---------------------------------------------------------------------------
void __fastcall TImageListBox::OnImageMeasureItem(TWinControl* Control, int Index, int &Height)
{
Height = ItemHeight;
}
//---------------------------------------------------------------------------
// move
int __fastcall TImageListBox::MoveUp(int nIndex, BOOL bSetCurSel)
{
int nRetValue = nIndex;

if(nIndex > 0)
{
nRetValue = Move(nIndex, nIndex - 1, bSetCurSel);
}

return nRetValue;
}
//---------------------------------------------------------------------------
int __fastcall TImageListBox::MoveDown(int nIndex, BOOL bSetCurSel)
{
int nRetValue = nIndex;

if(nIndex < Count - 1)
{
nRetValue = Move(nIndex, nIndex + 1, bSetCurSel);
}

return nRetValue;
}
//---------------------------------------------------------------------------
int __fastcall TImageListBox::MoveTop(int nIndex, BOOL bSetCurSel)
{
int nRetValue = nIndex;

if(nIndex > 0)
{
nRetValue = Move(nIndex, 0, bSetCurSel);
}

return nRetValue;
}
//---------------------------------------------------------------------------
int __fastcall TImageListBox::MoveBottom(int nIndex, BOOL bSetCurSel)
{
int nRetValue = nIndex;

if(nIndex < Count - 1)
{
nRetValue = Move(nIndex, Count - 1, bSetCurSel);
}

return nRetValue;
}
Hopping 2002-09-04
  • 打赏
  • 举报
回复
这是头文件
//---------------------------------------------------------------------------
/*********显示缩略图的listbox*****************/
#ifndef ImageListBoxH
#define ImageListBoxH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <xImage.h>

#define LB_ERR (-1)

// 图片文件结构
struct SMediaFileInfo
{
char sFileName[MAX_PATH]; // 文件名
int eFileType; // 文件类型
DWORD dwSize; // 文件尺寸
char sFileCaption[50]; // 文件标题
DWORD dwStartAddress; // 文件的起始地址

SMediaFileInfo()
{
memset(sFileName, 0, sizeof(sFileName));
eFileType = 0;
dwSize = 0;
memset(sFileCaption, 0, sizeof(sFileCaption));
dwStartAddress = 0;
}
};

//---------------------------------------------------------------------------
class PACKAGE TImageListBox : public TListBox
{
class TMediaFileInfo : public TObject
{
public:
__fastcall TMediaFileInfo() {}
__fastcall ~TMediaFileInfo() {}

public:
SMediaFileInfo mfi;
CxImage image;
};

private:
TMediaFileInfo* FMedia;

private:
void __fastcall FreeResources();
int __fastcall GetImageFileType(String FileName);

protected:
void __fastcall OnImageDrawItem(TWinControl* Control, int Index, const TRect& Rect,
TOwnerDrawState State);
void __fastcall OnImageMeasureItem(TWinControl* Control, int Index, int &Height);

public:
__fastcall TImageListBox(TComponent* Owner);
__fastcall ~TImageListBox();

// 增加一个图片文件
int __fastcall AddString(SMediaFileInfo mfi);
int __fastcall InsertString(int nIndex, SMediaFileInfo mfi);
DYNAMIC void __fastcall DeleteString(int Index);
int __fastcall ReplaceString(int nIndex, LPCTSTR lpszString);
DYNAMIC void __fastcall ResetContent(void);

// move
int __fastcall MoveUp(int nIndex, BOOL bSetCurSel = true);
int __fastcall MoveDown(int nIndex, BOOL bSetCurSel = true);
int __fastcall MoveTop(int nIndex, BOOL bSetCurSel = true);
int __fastcall MoveBottom(int nIndex, BOOL bSetCurSel = true);
int __fastcall Move(int nOldIndex, int nNewIndex, BOOL bSetCurSel = true);

SMediaFileInfo __fastcall GetItemInfo(int nIndex);
void __fastcall SetItemCaption(int nIndex, String sCaption);
String __fastcall GetItemCaption(int nIndex);

__published:
__property ItemHeight;
};
//---------------------------------------------------------------------------
#endif
bluebird97073 2002-09-04
  • 打赏
  • 举报
回复
代码贴出来

13,826

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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