简单的问题,如何获取图标? (55分)

  • 主题发起人 主题发起人 徐凤华
  • 开始时间 开始时间

徐凤华

Unregistered / Unconfirmed
GUEST, unregistred user!
我自己编了两个控件,TCurrEdit和TDateEdit,均是从TCustomEdit中继承的。
分别用于输入Currency和TDate数据,只是没有图标,安装控件后 ,
图标显示为三角形(奇丑),请问诸位大侠,如何获取TEdit和TComboBox的图标?
 
哪里有教写控件的文章?
 
你要加一個跟控件同名的DCR文件(資源文件)﹐文件中的圖標名稱要跟控件一模一樣﹐一
起加到DPK包文件中即可。記得以前有這類問題。
 
同意dadabox。用DELPHI的IMAGEEDITOR编辑。
在DCR中是24X24 16色的图形,名称要大写,跟控件名一样(如TDATEEDIT)。
另外也可以放在DPK的RES文件中(据说这样不好,但我觉得挺好)。
 
关键在于到什么地方去找这些RES文件呢?
我几乎把DELPHI目录下的所有RES文件都打开看过了,好像没有找到我需要的图标。
(也可能是我眼神不好,加上比较急躁,可能漏掉了)。
 
RES就是DPK自己的(如mypkg.pkg则自动有一个mypkg.res),DCR则要你创建
(如TDateEdit则创建文件为DateEdit.dcr),图标是你自己画的,当然你可
以拷贝别人画的图标。
创建、编辑图标则由Tools/Image Editor来完成。
 
对不起,看错问题。
>>如何获取TEdit和TComboBox的图标
你打开DELPHI,翻到有TEDIT的控件页,按一下Pring Screen键,然后到Image Editor中
粘贴,移位即可。由于DELPHI中的标准控件是用BPL形式安装的,因此可能没有RES文件。
 
反正闲着也是闲着,顺便把这两个控件的源码贴出来。如果有朋友要急用的话,
也省的花时间写了。(第一次写控件,如有不妥之处,还请各位多多指教)。

//用于输入Currency数据的Edit控件, 添加了以下属性:
// CurrValue: 用于读取/设定Edit控件的Currency数据.
// ErrorMessage: 当输入错误时MessageBox的提示信息.
// ErrorTitle: 当输入错误时MessageBox的标题.
// NumberOfDigits: 指定小数位数.
// Prefix: 用于指定Currency的前缀(比如:$或¥).

unit CurrEdit;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics;

type
TCurrEdit = class(TCustomEdit)
private
{ Private declarations }
FCurrency: Currency;
FErrorMessage: String;
FErrorTitle: String;
FNumberOfDigits: Integer;
FPrefix: String;
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;
procedure DoExit; override;
function GetCurrency: Currency; virtual;
procedure SetCurrency(NewCurrency: Currency); virtual;
procedure SetNumberOfDigits(NewNumberOfDigits: Integer); virtual;
procedure SetPrefix(NewPrefix: String); virtual;
property CharCase;
property MaxLength;
property PasswordChar;
property Text;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Anchors;
property AutoSelect;
property AutoSize;
property BevelEdges;
property BevelInner;
property BevelKind default bkNone;
property BevelOuter;
property BiDiMode;
property BorderStyle;
property Color;
property Constraints;
property Ctl3D;
property CurrValue: Currency read GetCurrency write SetCurrency;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property ErrorMessage: String read FErrorMessage write FErrorMessage;
property ErrorTitle: String read FErrorTitle write FErrorTitle;
property Font;
property HideSelection;
property ImeMode;
property ImeName;
property NumberOfDigits: Integer read FNumberOfDigits write SetNumberOfDigits;
property OEMConvert;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property Prefix: String read FPrefix write SetPrefix;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnChange;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Standard', [TCurrEdit]);
end;

procedure TCurrEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or ES_RIGHT;
end;

procedure TCurrEdit.DoExit;
var
TempExtended: Extended;
TempColor: TColor;
begin
inherited DoExit;
if Pos(FPrefix, Text) = 1 then
Text := Copy(Text, Length(FPrefix) + 1, Length(Text) - Length(FPrefix));
if not TextToFloat(PChar(Text), TempExtended, fvExtended) then
begin
TempColor := Font.Color;
Font.Color := clRed;
MessageBox(0, PChar(FErrorMessage), PChar(FErrorTitle), MB_ICONINFORMATION + MB_OK);
Font.Color := TempColor;
SetFocus;
end
else CurrValue := TempExtended;
end;

function TCurrEdit.GetCurrency: Currency;
begin
Result := FCurrency;
end;

procedure TCurrEdit.SetCurrency(NewCurrency: Currency);
begin
FCurrency := NewCurrency;
Text := FPrefix + CurrToStrF(FCurrency, ffNumber, FNumberOfDigits);
end;

procedure TCurrEdit.SetNumberOfDigits(NewNumberOfDigits: Integer);
begin
if NewNumberOfDigits < 0 then NewNumberOfDigits := 0;
if NewNumberOfDigits <> FNumberOfDigits then
begin
FNumberOfDigits := NewNumberOfDigits;
CurrValue := FCurrency;
end;
end;

procedure TCurrEdit.SetPrefix(NewPrefix: String);
begin
if NewPrefix <> FPrefix then
begin
FPrefix := NewPrefix;
CurrValue := FCurrency;
end;
end;

constructor TCurrEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
CurrValue := 0;
NumberOfDigits := 0;
FErrorMessage := 'Invalid Data Type.';
FErrorTitle := 'Input Error:';
FPrefix := '';
end;

destructor TCurrEdit.Destroy;
begin
inherited Destroy;
end;

end.

//用于输入Date数据的Edit控件, 添加了以下属性:
// Alignment: taLeftJustify, taRightJustify, taCenter. 指定对齐方式.
// DateValue: 用于读取/设定Edit控件的Date数据.
// ErrorMessage: 当输入错误时MessageBox的提示信息.
// ErrorTitle: 当输入错误时MessageBox的标题.
// Format: 用于控制Date显示的格式,Format格式见FormatDateTime函数的帮助.

unit DateEdit;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics;

type
TDateEdit = class(TCustomEdit)
private
{ Private declarations }
FAlignment: TAlignment;
FDate: TDate;
FErrorMessage: String;
FErrorTitle: String;
FFormat: String;
//用于控制Date显示的格式,Format格式见FormatDateTime函数的帮助.
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;
procedure DoExit; override;
function GetAlignment: TAlignment; virtual;
function GetDate: TDate; virtual;
procedure SetAlignment(NewAlignment: TAlignment); virtual;
procedure SetDate(NewDate: TDate); virtual;
procedure SetFormat(NewFormat: String); virtual;
property CharCase;
property MaxLength;
property PasswordChar;
property Text;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Alignment: TAlignment read GetAlignment write SetAlignment;
property Anchors;
property AutoSelect;
property AutoSize;
property BevelEdges;
property BevelInner;
property BevelKind default bkNone;
property BevelOuter;
property BiDiMode;
property BorderStyle;
property Color;
property Constraints;
property Ctl3D;
property DateValue: TDate read GetDate write SetDate;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property ErrorMessage: String read FErrorMessage write FErrorMessage;
property ErrorTitle: String read FErrorTitle write FErrorTitle;
property Font;
property Format: String read FFormat write SetFormat;
property HideSelection;
property ImeMode;
property ImeName;
property OEMConvert;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnChange;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Standard', [TDateEdit]);
end;

procedure TDateEdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[TAlignment] of WORD = (ES_LEFT, ES_RIGHT, ES_CENTER);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or Alignments[FAlignment];
end;

procedure TDateEdit.DoExit;
var
TempDateTime: TDateTime;
TempColor: TColor;
TempString: String;
Index: Integer;
begin
inherited DoExit;
TempString := Text;
Index := 1;
while (Index <= Length(TempString)) and (TempString[Index] >= '0') and
(TempString[Index] <= '9') do Inc(Index);
if Index > Length(TempString) then
begin
case Length(TempString) of
6 : TempString := Copy(TempString, 1, 2) + '-' + Copy(TempString, 3, 2) + '-' + Copy(TempString, 5, 2);
8 : TempString := Copy(TempString, 1, 4) + '-' + Copy(TempString, 5, 2) + '-' + Copy(TempString, 7, 2);
end;
end
else begin
for Index := 1 to Length(TempString) do
if (TempString[Index] < '0') or (TempString[Index] > '9') then
TempString[Index] := '-';
end;
if not TryStrToDateTime(TempString, TempDateTime) then
begin
TempColor := Font.Color;
Font.Color := clRed;
MessageBox(0, PChar(FErrorMessage), PChar(FErrorTitle), MB_ICONINFORMATION + MB_OK);
Font.Color := TempColor;
SetFocus;
end
else DateValue := TempDateTime;
end;

function TDateEdit.GetAlignment: TAlignment;
begin
GetAlignment := FAlignment;
end;

function TDateEdit.GetDate: TDate;
begin
Result := FDate;
end;

procedure TDateEdit.SetAlignment(NewAlignment: TAlignment);
begin
if FAlignment <> NewAlignment then
begin
FAlignment := NewAlignment;
if not (csLoading in componentstate) then ReCreateWnd;
end;
end;

procedure TDateEdit.SetDate(NewDate: TDate);
begin
FDate := NewDate;
Text := FormatDateTime(FFormat, FDate);
end;

procedure TDateEdit.SetFormat(NewFormat: String);
begin
if FFormat <> NewFormat then
begin
FFormat := NewFormat;
DateValue := FDate;
end;
end;

constructor TDateEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAlignment := taLeftJustify;
DateValue := Now;
FErrorMessage := 'Invalid Date Type.';
FErrorTitle := 'Input Error:';
Format := 'yyyy''/''mm''/''dd';
end;

destructor TDateEdit.Destroy;
begin
inherited Destroy;
end;

end.
 
多人接受答案了。
 
后退
顶部