我时这样写得
procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items.Add('0001=张三');
ComboBox1.Items.Add('0002=李四');
ComboBox1.Items.Add('0003=王五');
end;在重绘事件我这样写
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
if ComboBox1.ItemIndex = Index then begin
Canvas.Brush.Color := clHotLight;
Canvas.FillRect(Rect);
Canvas.Font.Color := clWindow;
end else begin
Canvas.Brush.Color := clWindow;
Canvas.FillRect(Rect);
Canvas.Font.Color := clWindowText;
end;
Canvas.TextRect(Rect, Rect.Left, Rect.Top + 1,ComboBox1.Items.Values[Combobox1.Items.Names[index]]);
end;但是当运行时点击combobox的时候,其下拉菜单的文字偏移大到老远的位置,不知为何
procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items.Add('0001=张三');
ComboBox1.Items.Add('0002=李四');
ComboBox1.Items.Add('0003=王五');
end;在重绘事件我这样写
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
if ComboBox1.ItemIndex = Index then begin
Canvas.Brush.Color := clHotLight;
Canvas.FillRect(Rect);
Canvas.Font.Color := clWindow;
end else begin
Canvas.Brush.Color := clWindow;
Canvas.FillRect(Rect);
Canvas.Font.Color := clWindowText;
end;
Canvas.TextRect(Rect, Rect.Left, Rect.Top + 1,ComboBox1.Items.Values[Combobox1.Items.Names[index]]);
end;但是当运行时点击combobox的时候,其下拉菜单的文字偏移大到老远的位置,不知为何
Rect: TRect; State: TOwnerDrawState);
begin
with ComboBox1 do
begin
if ItemIndex = Index then begin
Canvas.Brush.Color := clHotLight;
Canvas.FillRect(Rect);
Canvas.Font.Color := clWindow;
end else begin
Canvas.Brush.Color := clWindow;
Canvas.FillRect(Rect);
Canvas.Font.Color := clWindowText;
end;
Canvas.TextRect(Rect, Rect.Left, Rect.Top, Items[Index]);
end;
end;
换成Canvas.TextOut(Rect.Left, Rect.Top, Items[Index]);
Rect: TRect; State: TOwnerDrawState);
begin
with ComboBox1 do
begin
if odSelected in State then
begin
Canvas.Brush.Color := clRed;
Canvas.Font.Color := clWindow;
end
else
begin
Canvas.Brush.Color := clWindow;
Canvas.Font.Color := clWindowText;
end;
Canvas.TextRect(Rect, Rect.Left, Rect.Top + 1, ComboBox1.Items.Values[Combobox1.Items.Names[index]]);
end;
end;