2023年2月写的修复lazarus combobox的下拉列表在linux时没有高亮显示选中的item的问题,需然解决了显示问题,但下拉列表的颜色在银河麒麟是灰黑色,和应用的颜色明显不搭,想要win一样样式,如果要改变下拉背景颜色,可以按以下修改就可以,当然,如果不想用白色,可以改为想要的颜色。
打开lazarus/lcl/include/customcombobox.inc
按以下修改后重新编译就可以.
procedure TCustomComboBox.DrawItem(Index: Integer; ARect: TRect; State: TOwnerDrawState); begin //TControlCanvas(FCanvas).UpdateTextFlags; if Assigned(FOnDrawItem) then FOnDrawItem(Self, Index, ARect, State) else begin if not (odBackgroundPainted in State) then FCanvas.FillRect(ARect); InternalDrawItem(Self, FCanvas, ARect, Items[Index]) end; end;
改该:
procedure TCustomComboBox.DrawItem(Index: Integer; ARect: TRect; State: TOwnerDrawState); begin //TControlCanvas(FCanvas).UpdateTextFlags; //2022.05.28 LBZ {$ifdef linux} if (FDroppedDown) then begin if (GetItemIndex=index) then //当为ItemIndex时高亮显示item begin FCanvas.Brush.Color := clHighlight; FCanvas.Font.Color := clHighlightText or clHighlight; State:=[odSelected]; end Else begin if not (odBackgroundPainted in State) then FCanvas.Brush.Color := clWhite;//背景颜色 State:=[odFocused]; end; //FCanvas.Font.Color:= clMenuText; end; {$endif} //2022.05.28 LBZ if Assigned(FOnDrawItem) then FOnDrawItem(Self, Index, ARect, State) else begin if not (odBackgroundPainted in State) then FCanvas.FillRect(ARect); InternalDrawItem(Self, FCanvas, ARect, Items[Index]) end; end;
修改将下拉是用系统主题颜色的:
修改后,用自定的颜色(和win一样的颜色):
procedure TCustomComboBox.LMDrawListItem(var TheMessage : TLMDrawListItem); begin with TheMessage.DrawListItemStruct^ do begin FCanvas.Handle := DC; if Font<>nil then begin FCanvas.Font := Font; FCanvas.Font.PixelsPerInch := Font.PixelsPerInch; end; if Brush<>nil then FCanvas.Brush := Brush; if (ItemID <> UINT(-1)) and (odSelected in ItemState) then begin FCanvas.Brush.Color :=clHighlight; FCanvas.Font.Color := clHighlightText; end; DrawItem(ItemID, Area, ItemState); if odFocused in ItemState then {DrawFocusRect(hDC, rcItem)}; FCanvas.Handle := 0; end; end;
改为
procedure TCustomComboBox.LMDrawListItem(var TheMessage : TLMDrawListItem); begin with TheMessage.DrawListItemStruct^ do begin FCanvas.Handle := DC; if Font<>nil then begin FCanvas.Font := Font; FCanvas.Font.PixelsPerInch := Font.PixelsPerInch; end; if Brush<>nil then FCanvas.Brush := Brush; if (ItemID <> UINT(-1)) and (odSelected in ItemState) then begin FCanvas.Brush.Color :=clHighlight; FCanvas.Font.Color := clHighlightText; {$ifdef linux} FCanvas.FillRect(Area); ItemState:=[odBackgroundPainted]; {$endif} end; DrawItem(ItemID, Area, ItemState); if odFocused in ItemState then {DrawFocusRect(hDC, rcItem)}; FCanvas.Handle := 0; end; end;
标签:begin,end,修复,combobox,下拉,State,Brush,Font,FCanvas From: https://www.cnblogs.com/qiufeng2014/p/18160390