有预览的颜色选择下拉框

从网上找到的,原来的代码画框(Rectangle),改进为画块(FillRect),并保持画块前的画布属性。

function GetSelectedColor(const AIndex: Integer): TColor;
begin
  case AIndex of
    0: Result := clYellow;
    1: Result := clRed;
    2: Result := clGreen;
    3: Result := clBlue;
    4: Result := clNavy;
    5: Result := clMaroon;
    6: Result := clOlive;
    7: Result := clPurple;
    8: Result := clTeal;
    9: Result := clLime;
    10:Result := clFuchsia;
    11:Result := clAqua;
  else
    Result := clYellow;
  end;
end; 

cbSelColor.Style := csOwnerDrawFixed;
procedure cbSelColorDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  OBC, OFC, SelColor: TColor;
  CurrRect: TRect;
begin
  SelColor := GetSelectedColor(Index);
  with cbSelColor.Canvas do
  begin
    FillRect(Rect);
    OBC := Brush.Color; // my enhancement
    OFC := Font.Color; // my enhancement

    Brush.Color := SelColor;
    Font.Color := clBlack;

    CurrRect.Left := Rect.Left + 1;
    CurrRect.Top := Rect.Top + 1;
    CurrRect.Right := Rect.Left + 14;
    CurrRect.Bottom := Rect.Top + 14;

    FillRect(CurrRect); // my enhancement
    Brush.Color := OBC; // my enhancement
    Font.Color := OFC; // my enhancement

    TextOut(CurrRect.Right + 10, CurrRect.Top, cbSelColor.Items[Index]);
  end;
end;

另外几个小技巧:
procedure BitmapToIcon(ABitmap: TBitmap; ATransColor: TColor): TIcon;
begin
  with TImageList.CreateSize(ABitmap.Width, ABitmap.Height) do
  begin
    try
      AllocBy := 1;
      AddMasked(ABitmap, ATransColor);
      Result := TIcon.Create;
      try
        GetIcon(0, Result);
      except
        Ico.Free;
        raise;
      end;
    finally
      Free;
    end;
  end;
end;

function RGBToColor(R, G, B:Byte): TColor;
begin
  Result := B Shl 16 or
            G Shl 8  or
            R;
end;

procedure GetColorRGB(const AColor: TColor; var R, G, B: Integer);
begin
  R := AColor and $FF;
  G := (AColor and $FF00) shr 8;
  B := (AColor and $FF0000) shr 16;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值