给你做了一个:
unit SrwListBox;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TSrwListBox = class(TListBox)
private
FDisableItemS : string;
{ Private declarations }
procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
protected
{ Protected declarations }
public
procedure EnableItem(Index: Integer);
procedure DisableItem(Index: Integer);
function isDisable(Index: Integer):boolean;
procedure ClearDisableRec;
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
procedure TSrwListBox.disableItem(Index: Integer);
var disindex : string;
begin
disindex := ','+IntToStr(Index)+',';
if Pos(disindex,FDisableItems) = 0 then
FDisableItemS := FDisableItems + disindex;
// SendMessage(Handle, LB_SETITEMDATA, Index, 0);
SendMessage(Handle, LB_SETSEL, Longint(False),Index);
// Refresh;
end;
procedure TSrwListBox.EnableItem(Index: Integer);
var disindex : string;
begin
disindex := ','+IntToStr(Index)+',';
if Pos(disindex,FDisableItems) > 0 then
delete(FDisableItemS, Pos(disindex,FDisableItems), Length(disindex));
SendMessage(Handle, LB_SETSEL, Longint(False),Index);
// Refresh;
end;
function TSrwListBox.isDisable(Index: Integer):boolean;
var disindex : string;
begin
disindex := ','+IntToStr(Index)+',';
Result := Pos(disindex,FDisableItems) > 0;
end;
procedure TSrwListBox.ClearDisableRec;
begin
FDisableItems := '';
end;
procedure TSrwListBox.CNDrawItem(var Message: TWMDrawItem);
var
State: TOwnerDrawState;
idx : integer;
begin
with Message.DrawItemStruct^ do
begin
idx := Integer(itemID);
if Pos(','+IntToStr(idx)+',',FDisableItems) > 0 then
State := [odDisabled]
else
State := TOwnerDrawState(WordRec(LongRec(itemState).Lo).Lo);
Canvas.Handle := hDC;
Canvas.Font := Font;
Canvas.Brush := Brush;
if (Integer(itemID) >= 0) and (odSelected in State) then
begin
Canvas.Brush.Color := clHighlight;
Canvas.Font.Color := clHighlightText
end
else if (odDisabled in State) then
Canvas.Font.Color := clGray;
if Integer(itemID) >= 0 then
DrawItem(itemID, rcItem, State) else
Canvas.FillRect(rcItem);
if odFocused in State then DrawFocusRect(hDC, rcItem);
Canvas.Handle := 0;
end;
end;
procedure Register;
begin
RegisterComponents('Srw', [TSrwListBox]);
end;
end.