自定义控件,如何在属性板中编辑IMAGE

老小笨鸟 2012-04-17 04:31:25
从TCUSTOMPANEL派生一个控件,包含1个image控件,3个Label控件,我想问问呢,这个image控件的图像怎么从属性板中导入,分不多了,但还是会最少50分。
...全文
139 点赞 收藏 12
写回复
12 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
老小笨鸟 2012-04-27
不能直接用timage*,我用bitmap转了一下,可以了,散分结贴
回复
yoke_wolf 2012-04-19
我在你另一个帖子中回复了相关问题,请在构造时就new好实例
回复
老小笨鸟 2012-04-19
我现在已能从属性面板导入位图,在设计期是可以的,然后编译也没有问题,但一运行,在构造对象还没构造好的时候就报错了,说是 invalid property path。
回复
yoke_wolf 2012-04-19

class PACKAGE CtrlXXX:public AA
{
private:
TImage *FImage; // Image构造函数中要new好
....
TPicture* __fastcall GetImagePicture();
void __fastcall SetImagePicture(TPicture* Value);
// ....

__published:
__property ImagePicture = { read = GetImagePicture, write = SetImagePicture};
}

TPicture* __fastcall CtrlXXX::GetImagePicture()
{
return this->FImage->Picture;
}

void __fastcall CtrlXXX::SetImagePicture(TPicture* Value)
{
this->FImage->Picture->Assign(Value);
this->Invalidate();
}



也可以从TPropertyEditor继承添加属性编辑窗口

class ThqXXXXEditor: TPropertyEditor
{
public:
virtual Designintf::TPropertyAttributes __fastcall GetAttributes(void);
virtual System::UnicodeString __fastcall GetValue(void); // 显示在IDE属性编辑器中的名字
virtual void __fastcall SetValue(const System::UnicodeString Value)/* overload */; // 设置值时
virtual void __fastcall SetValue(const System::WideString Value)/* overload */;
virtual void __fastcall Edit(void); // 编辑动作
};

并在注册函数中指定类型属性所用的PropertyEditor

RegisterPropertyEditor(__typeinfo(TImage), __classid(TMyControlClass), L"MyPropertyName", __classid(ThqXXXXEditor));
回复
老小笨鸟 2012-04-19
yoke_wolf,您QQ多少?
image 我本来是加的,但__property TImage *PerPhoto = {read=FimgPhoto,write=SetPhoto}的时候,编译不通过,报构造函数多重定义
回复
yoke_wolf 2012-04-19
说的不够明白么。。。?

构造函数里加上 FGlyph = new Graphics::TBitmap(); 就可以了

如果
//__property TPicture *PicPerson = { read=FpicPerson,write=SetpicPerson);
//__property TImage *PerPhoto = {read=FimgPhoto,write=SetPhoto}
不注释

就再加上
FpicPerson = new Graphics::TPicture();
FimgPhoto = new TImage(this);
// ...
FimgPhoto->Parent = this;
FimgPhoto->SetSubComponent(true); // TImage还得加上这个
回复
老小笨鸟 2012-04-19
cpp 文件:
//---------------------------------------------------------------------------


#pragma hdrstop

#include "ImgInfo.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(TImgInfo *)
{
new TImgInfo(NULL);
}
//---------------------------------------------------------------------------
__fastcall TImgInfo::TImgInfo(TComponent* Owner) : TCustomPanel(Owner)
{
Caption = "";
FCreated = false;
FCurRow = 1;
FScolls = true;
FRaiseMode = rsTopUpScroll;
}
//---------------------------------------------------------------------------
void __fastcall TImgInfo::CreateWnd()
{
TCustomPanel::CreateWnd();
InitPanel();
InitImgInfo();
}
//---------------------------------------------------------------------------
void __fastcall TImgInfo::InitPanel()
{
// 确定滚动区域
Height = 300;
Width = 200;
FRect = GetClientRect(); // 取得新的窗口客户区
FRect.Top += 2;
FRect.Bottom -= 2;
FRect.Left += 4;
FRect.Right -= 4;
// 计算每行高度
Canvas->Font = Font;
FHeightOfRow = Canvas->TextHeight('W')+2;
FRows = FRect.Height() / FHeightOfRow;
// 处理光标位置
if(FCurRow > FRows)
{
ScrollWindow(Handle,0,(FRows-FCurRow)*FHeightOfRow,NULL,&FRect);
FCurRow = FRows ;
}
if(FRaiseMode == rsBottUpScroll)
FCurRow = FRows;
}
//---------------------------------------------------------------------------
void __fastcall TImgInfo::InitImgInfo(void)
{
int iX,iY;
TRect stRect;

FimgPhoto = new TImage(NULL);
FlbName = new TLabel(NULL);
FlbJoinDate = new TLabel(NULL);
FlbBirthDate = new TLabel(NULL);
FlbPrompt = new TLabel(NULL);
//FpicPerson = new TPicture();

stRect = GetClientRect();
FimgPhoto->Parent = this;
FimgPhoto->Left = 35;
FimgPhoto->Top = 20;
FimgPhoto->Width = 130;
FimgPhoto->Height = 150;
FimgPhoto->Visible = true;
FimgPhoto->Stretch = true;
FimgPhoto->Picture->LoadFromFile("C:\\Program Files\\Borland\\CBuilder6\\Examples\\Games\\EarthPng\\androm.bmp");
FimgPhoto->Canvas->Brush->Color = clInactiveCaption;//clGray;
stRect = FimgPhoto->ClientRect;
stRect = TRect(0,0,FimgPhoto->Width,FimgPhoto->Height);
FimgPhoto->Canvas->FillRect(stRect);

FlbName->Parent = this;
FlbName->Left = 35;
FlbName->Top = 200;
FlbName->Color = clGreen;
FlbName->AutoSize = false;
FlbName->Width = 130;
FlbName->Font->Size = 10;
FlbName->Caption = "姓名测试:笑嘻嘻";
FlbName->Visible = true;

FlbJoinDate->Parent = this;
FlbJoinDate->Left = 35;
FlbJoinDate->Top = 225;
FlbJoinDate->Color = clGreen;
FlbJoinDate->Caption = "2012-02-01";
FlbJoinDate->Font->Size = 10;
FlbJoinDate->AutoSize = false;
FlbJoinDate->Width = 130;
FlbJoinDate->Visible = true;

FlbBirthDate->Parent = this;
FlbBirthDate->Left = 35;
FlbBirthDate->Top = 250;
FlbBirthDate->Color = clGreen;
FlbBirthDate->Caption = "2011-01-01";
FlbBirthDate->Font->Size = 10;
FlbBirthDate->AutoSize = false;
FlbBirthDate->Width = 130;
FlbBirthDate->Visible = true;

FlbPrompt->Parent = this;
FlbPrompt->Left = 35;
FlbPrompt->Top = 275;
FlbPrompt->Color = clGreen;
FlbPrompt->Caption = "Prompt: ";
FlbPrompt->Font->Size = 10;
FlbPrompt->AutoSize = true;
FlbPrompt->Width = 130;
FlbPrompt->Visible = true;
}
//---------------------------------------------------------------------------
__fastcall TImgInfo::~TImgInfo()
{
delete FimgPhoto;
delete FlbName;
delete FlbJoinDate;
delete FlbBirthDate;
}
//---------------------------------------------------------------------------
void __fastcall TImgInfo::Clear()
{
ScrollWindow(Handle,0,-FRect.Height(),NULL,&FRect);
if(FRaiseMode == rsBottUpScroll)
FCurRow = FRows;
else
FCurRow = 1;
FScolls = false;
}
//---------------------------------------------------------------------------
void __fastcall TImgInfo::Paint(void)
{
TColor TopColor, BottomColor;

if( ! FCreated || ComponentState.Contains(csDesigning))
{

TRect Rect = GetClientRect();
if( BevelOuter != bvNone )
{
TopColor = (BevelOuter == bvLowered) ? clBtnShadow : clBtnHighlight;
BottomColor = (BevelOuter == bvLowered) ? clBtnHighlight : clBtnShadow;
Frame3D(Canvas, Rect, TopColor, BottomColor, BevelWidth);
}
if( BevelInner != bvNone )
{
TopColor = (BevelInner == bvLowered) ? clBtnText : clInactiveBorder;
BottomColor = (BevelInner == bvLowered) ? clInactiveBorder : clBtnText;
Frame3D(Canvas, Rect, TopColor, BottomColor, BevelWidth);
}
Canvas->Brush->Color = Color;
Canvas->Brush->Style = bsClear;
Canvas->FillRect(Rect);
}
}

//---------------------------------------------------------------------------
void __fastcall TImgInfo::Resize(void)
{
FCreated = false;
TCustomPanel::Resize();
InitPanel();
Clear();
}

//---------------------------------------------------------------------------
void __fastcall TImgInfo::SetRaiseMode(RAISEMODE Value)
{
FRaiseMode = Value;
}

//---------------------------------------------------------------------------
void __fastcall TImgInfo::SetPromptStr(String Value)
{
FPromptStr = Value;
FlbPrompt->Caption = Value;
}
//---------------------------------------------------------------------------
void __fastcall TImgInfo::SetMonName(String Value)
{
FName = Value;
FlbName->Caption = Value;
}
//---------------------------------------------------------------------------
void __fastcall TImgInfo::SetJoinDate(String Value)
{
FJoinDate = Value;
FlbJoinDate->Caption = Value;
}
//---------------------------------------------------------------------------
void __fastcall TImgInfo::SetBirthDate(String Value)
{
FBirthDate = Value;
FlbBirthDate->Caption = Value;
}
//---------------------------------------------------------------------------
void __fastcall TImgInfo::SetGlyph(Graphics::TBitmap *Value)
{
if(Value==NULL)
{
if(FGlyph)
{
try
{
delete FGlyph;
FGlyph = NULL;
RefreshBmp();
Invalidate();
}
catch(Exception &e)
{
}
}
return;
}
if(!FGlyph)
{
FGlyph = new Graphics::TBitmap();
}
FGlyph->Assign(Value);
RefreshBmp();
Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TImgInfo::RefreshBmp(void)
{
TRect cRect,tRect,gRect;
TColor TopColor,BottomColor;
Graphics::TBitmap *bit = new Graphics::TBitmap();

FimgPhoto->Canvas->Brush->Color = clBtnFace;
FimgPhoto->Canvas->FillRect(ClientRect);

cRect = FimgPhoto->ClientRect;
bit->Width = FimgPhoto->ClientWidth;
bit->Height = FimgPhoto->ClientHeight;
bit->Canvas->Brush->Color = clBtnFace;
bit->Canvas->FillRect(cRect);

if(FGlyph)
if(!FGlyph->Empty)
{
gRect = cRect;
//bit->Canvas->BrushCopy(gRect,FGlyph,Rect(0,0,FGlyph->Width,FGlyph->Height),FGlyph->TransparentColor);
bit->Canvas->StretchDraw(cRect,FGlyph);
}
BitBlt(FimgPhoto->Canvas->Handle,0,0,FimgPhoto->ClientWidth,FimgPhoto->ClientHeight,bit->Canvas->Handle,0,0,SRCCOPY);
delete bit;
}
//---------------------------------------------------------------------------
回复
老小笨鸟 2012-04-19
头文件:
//---------------------------------------------------------------------------

#ifndef ImgInfoH
#define ImgInfoH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <ComCtrls.hpp>

enum RAISEMODE { rsTopUpScroll,rsBottUpScroll,rsTopDownScroll };
//---------------------------------------------------------------------------
class PACKAGE TImgInfo : public TCustomPanel
{
private:

String FName;
String FJoinDate;
String FBirthDate;
String FPromptStr;

TLabel *FlbPrompt;
TLabel *FlbName;
TLabel *FlbJoinDate;
TLabel *FlbBirthDate;
TImage *FimgPhoto;
//TPicture *FpicPerson;
//int FPicture;
Graphics::TBitmap *FGlyph;

TRect FRect;
bool FBellOn;
int FRows;
int FCurRow;
int FHeightOfRow;
bool FScolls;
bool FCreated;
RAISEMODE FRaiseMode;

void __fastcall InitPanel();
void __fastcall InitImgInfo(void);
void __fastcall SetRaiseMode(RAISEMODE Value);

void __fastcall SetPromptStr(String Value);
void __fastcall SetMonName(String Value);
void __fastcall SetJoinDate(String Value);
void __fastcall SetBirthDate(String Value);
//void __fastcall SetpicPerson(TPicture *Value);
//void __fastcall SetPicture(int Value);
//void __fastcall SetPhoto(TImage *Value);
void __fastcall SetGlyph(Graphics::TBitmap *Value);
void __fastcall RefreshBmp(void);

protected:
void __fastcall SetMessageText(String Value);

virtual void __fastcall Paint(void);
DYNAMIC void __fastcall Resize(void);
virtual void __fastcall CreateWnd();

virtual void __fastcall Notification(TComponent *AComponent,TOperation Operation); // 实验

public:
__fastcall TImgInfo(TComponent* Owner);
virtual __fastcall ~TImgInfo();
void __fastcall Clear();
__property DockManager;

__published:
__property RAISEMODE RaiseMode ={read=FRaiseMode,write=SetRaiseMode,default=rsTopUpScroll};

__property AnsiString MonName = { read=FName, write=SetMonName};
__property AnsiString JoinDate = { read=FJoinDate, write=SetJoinDate};
__property AnsiString BirthDate = { read=FBirthDate,write=SetBirthDate};
__property AnsiString PromptStr = { read=FPromptStr,write=SetPromptStr};
//__property int Picture = { read=FPicture, write=SetPicture};
//__property TPicture *PicPerson = { read=FpicPerson,write=SetpicPerson); // 指针是不是要包裹下?
//__property TImage *PerPhoto = {read=FimgPhoto,write=SetPhoto};
__property Graphics::TBitmap *Glyph = { read = FGlyph, write = SetGlyph };

__property Align ;
__property Alignment ;
__property Anchors ;
__property AutoSize ;
__property BevelInner ;
__property BevelOuter ;
__property BevelWidth ;
__property BiDiMode ;
__property BorderWidth ;
__property BorderStyle ;
__property Color ;
__property Constraints ;
__property Ctl3D ;
__property UseDockManager ;
__property DockSite ;
__property DragCursor ;
__property DragKind ;
__property DragMode ;
__property Enabled ;
__property FullRepaint ;
__property Font ;
__property Locked ;
__property ParentBiDiMode ;
__property ParentColor ;
__property ParentCtl3D ;
__property ParentFont ;
__property ParentShowHint ;
__property PopupMenu ;
__property ShowHint ;
__property TabOrder ;
__property TabStop ;
__property Visible ;
__property OnCanResize ;
__property OnClick ;
__property OnConstrainedResize ;
__property OnContextPopup ;
__property OnDockDrop ;
__property OnDockOver ;
__property OnDblClick ;
__property OnDragDrop ;
__property OnDragOver ;
__property OnEndDock ;
__property OnEndDrag ;
__property OnEnter ;
__property OnExit ;
__property OnGetSiteInfo ;
__property OnMouseDown ;
__property OnMouseMove ;
__property OnMouseUp ;
__property OnResize ;
__property OnStartDock ;
__property OnStartDrag ;
__property OnUnDock ;
};
//---------------------------------------------------------------------------
#endif
回复
老小笨鸟 2012-04-18
我的帖子被屏蔽了?怎么没有人帮我看帖子啊?
回复
老小笨鸟 2012-04-18
我就是不知道怎么添加属性编辑器啊
回复
老小笨鸟 2012-04-17
各位大哥,没人回答吗?来一个吧,求你们了.
回复
我不懂电脑 2012-04-17
用类库编辑器添加一个属性啊。
回复
相关推荐
发帖
VCL组件使用和开发
创建于2007-08-02

586

社区成员

C++ Builder VCL组件使用和开发
申请成为版主
帖子事件
创建了帖子
2012-04-17 04:31
社区公告
暂无公告