mbshareat
دوشنبه 08 آبان 1391, 06:33 صبح
سلام
من میخوام یه کامپوننت لیست باکس داشته باشم که متن داخلش رو خودم به شیوه دلخواهم بنویسم و کاربر هم نتونه در نوشتن متن داخل اون دخالت کنه.
میخوام همیشه Style در این لیست باکس lbOwnerDrawFixed باشه و آیتمها در این لیست باکس کادر نقطه چین نگیرند و فقط آیتم کلیک شده رنگی بشه (تقریبا یه چیزی مثل لینکهای لیست شده در منوی اصلی سمت راست صفحه اینترنتی!)
در حال حاضر نمی دونم چطور میتونم یه لیست باکس داشته باشم که با کد من ترسیم متن رو انجام بده.
از کد زیر انتظار داشتم متن رو با رنگ آبی نشون بده اما وقتی کامپوننت رو نصب می کنم و یه نمونه روی فرم میذارم متوجه میشم اصلا کاری به پروسیجر من نداره و خود دلفی متن درون لیست باکس رو ترسیم می کنه.ممکنه راهنماییم کنید؟
unit LBoxUnit;
interface
uses
Windows, Messages, SysUtils, Classes
,Forms,Controls, StdCtrls,Graphics,Math,dialogs,ExtCtrls,GraphUtil;
type
TLBox = class(TCustomListBox)
private
procedure CMFontChanged(var Message: TMessage);
constructor Create(AOwner: TComponent);
Protected
PromptMemo:TMemo;
property OnDrawItem;
property Style;
procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
procedure DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState); virtual;
published
property Anchors;
property Color;
property Enabled;
property Font;
property Items;
property ItemHeight;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnClick;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;
procedure Register;
implementation
{ TLBox }
constructor TLBox.Create(AOwner: TComponent);
begin
inherited;
Style:=lbOwnerDrawFixed;
Canvas.Font:=Font;
ItemHeight:=Canvas.TextHeight('hgsd');
end;
procedure TLBox.CMFontChanged(var Message: TMessage);
begin
Canvas.Font:=Font;
Invalidate;
end;
procedure TLBox.CNDrawItem(var Message: TWMDrawItem);
var
State: TOwnerDrawState;
begin
with Message.DrawItemStruct^ do
begin
State := TOwnerDrawState(LongRec(itemState).Lo);
Canvas.Handle := hDC;
Canvas.Font := Font;
Canvas.Brush := Brush;
if (Integer(itemID) >= 0) and (odSelected in State) then
Canvas.Brush.Color :=Color ;
if Integer(itemID) >= 0 then
Begin
Canvas.Font.Color:=clblue;
Canvas.TextRect(rcItem,rcItem.Left,rcItem.Right,It ems[itemID]);
End;
end;
end;
procedure TLBox.DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState);
begin
Canvas.FillRect(Rect);
Canvas.Font.Color:=clblue;
Canvas.TextRect(Rect,Rect.Left,Rect.Right,Items[Index]);
end;
//------
procedure Register;
begin
RegisterComponents('Test', [TLBox]);
End;
end.
من میخوام یه کامپوننت لیست باکس داشته باشم که متن داخلش رو خودم به شیوه دلخواهم بنویسم و کاربر هم نتونه در نوشتن متن داخل اون دخالت کنه.
میخوام همیشه Style در این لیست باکس lbOwnerDrawFixed باشه و آیتمها در این لیست باکس کادر نقطه چین نگیرند و فقط آیتم کلیک شده رنگی بشه (تقریبا یه چیزی مثل لینکهای لیست شده در منوی اصلی سمت راست صفحه اینترنتی!)
در حال حاضر نمی دونم چطور میتونم یه لیست باکس داشته باشم که با کد من ترسیم متن رو انجام بده.
از کد زیر انتظار داشتم متن رو با رنگ آبی نشون بده اما وقتی کامپوننت رو نصب می کنم و یه نمونه روی فرم میذارم متوجه میشم اصلا کاری به پروسیجر من نداره و خود دلفی متن درون لیست باکس رو ترسیم می کنه.ممکنه راهنماییم کنید؟
unit LBoxUnit;
interface
uses
Windows, Messages, SysUtils, Classes
,Forms,Controls, StdCtrls,Graphics,Math,dialogs,ExtCtrls,GraphUtil;
type
TLBox = class(TCustomListBox)
private
procedure CMFontChanged(var Message: TMessage);
constructor Create(AOwner: TComponent);
Protected
PromptMemo:TMemo;
property OnDrawItem;
property Style;
procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
procedure DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState); virtual;
published
property Anchors;
property Color;
property Enabled;
property Font;
property Items;
property ItemHeight;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnClick;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;
procedure Register;
implementation
{ TLBox }
constructor TLBox.Create(AOwner: TComponent);
begin
inherited;
Style:=lbOwnerDrawFixed;
Canvas.Font:=Font;
ItemHeight:=Canvas.TextHeight('hgsd');
end;
procedure TLBox.CMFontChanged(var Message: TMessage);
begin
Canvas.Font:=Font;
Invalidate;
end;
procedure TLBox.CNDrawItem(var Message: TWMDrawItem);
var
State: TOwnerDrawState;
begin
with Message.DrawItemStruct^ do
begin
State := TOwnerDrawState(LongRec(itemState).Lo);
Canvas.Handle := hDC;
Canvas.Font := Font;
Canvas.Brush := Brush;
if (Integer(itemID) >= 0) and (odSelected in State) then
Canvas.Brush.Color :=Color ;
if Integer(itemID) >= 0 then
Begin
Canvas.Font.Color:=clblue;
Canvas.TextRect(rcItem,rcItem.Left,rcItem.Right,It ems[itemID]);
End;
end;
end;
procedure TLBox.DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState);
begin
Canvas.FillRect(Rect);
Canvas.Font.Color:=clblue;
Canvas.TextRect(Rect,Rect.Left,Rect.Right,Items[Index]);
end;
//------
procedure Register;
begin
RegisterComponents('Test', [TLBox]);
End;
end.