Browse Source

Hover for ListBox in BCComboBox. Style button for combobox.

lainz 6 years ago
parent
commit
203cd8f0f9
2 changed files with 35 additions and 2 deletions
  1. 21 0
      bccombobox.pas
  2. 14 2
      test/test_bccombobox/umain.pas

+ 21 - 0
bccombobox.pas

@@ -16,16 +16,21 @@ type
   private
     FButton: TBCButton;
     FForm: TForm;
+    FHoverItem: integer;
     FListBox: TListBox;
     procedure ButtonClick(Sender: TObject);
     function GetItems: TStrings;
     procedure ListBoxClick(Sender: TObject);
+    procedure ListBoxMouseLeave(Sender: TObject);
+    procedure ListBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
+      Y: Integer);
     procedure ListBoxSelectionChange(Sender: TObject; User: boolean);
     procedure SetItems(AValue: TStrings);
   protected
 
   public
     constructor Create(AOwner: TComponent); override;
+    property HoverItem: integer read FHoverItem;
   published
     property Button: TBCButton read FButton write FButton;
     property ListBox: TListBox read FListBox write FListBox;
@@ -81,6 +86,19 @@ begin
   FButton.Caption := FListBox.Items[FListBox.ItemIndex];
 end;
 
+procedure TBCComboBox.ListBoxMouseLeave(Sender: TObject);
+begin
+  FHoverItem := -1;
+  FListBox.Repaint;
+end;
+
+procedure TBCComboBox.ListBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
+  Y: Integer);
+begin
+  FHoverItem := FListBox.ItemAtPos(Point(x, y), True);
+  FListBox.Repaint;
+end;
+
 procedure TBCComboBox.ListBoxSelectionChange(Sender: TObject; User: boolean);
 begin
   FButton.Caption := FListBox.Items[FListBox.ItemIndex];
@@ -106,6 +124,9 @@ begin
   FListBox.BorderStyle := bsNone;
   FListBox.OnClick := ListBoxClick;
   FListBox.OnSelectionChange := ListBoxSelectionChange;
+  FListBox.OnMouseLeave:=ListBoxMouseLeave;
+  FListBox.OnMouseMove:=ListBoxMouseMove;
+  FHoverItem := -1;
 end;
 
 end.

+ 14 - 2
test/test_bccombobox/umain.pas

@@ -6,7 +6,7 @@ interface
 
 uses
   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, BCComboBox,
-  BCListBox, LCLType;
+  BCListBox, LCLType, BCSamples;
 
 type
 
@@ -41,12 +41,15 @@ begin
   // Selecting items
   BCComboBox1.ListBox.ItemIndex := 0;
 
-  // Style
+  // Style ListBox
   BCComboBox1.ListBox.Style := lbOwnerDrawFixed;
   BCComboBox1.ListBox.OnDrawItem := @OnListBoxDrawItem;
   BCComboBox1.ListBox.Color := clGray;
   BCComboBox1.ListBox.ItemHeight := 2 * Canvas.GetTextHeight('aq');
   BCComboBox1.ListBox.Options := []; // do not draw focus rect
+
+  // Style Button
+  StyleButtonsSample(BCComboBox1.Button, TBCSampleStyle.ssFlashPlayer);
 end;
 
 procedure TForm1.OnListBoxDrawItem(Control: TWinControl; Index: integer;
@@ -56,6 +59,7 @@ var
 begin
   aCanvas := TListBox(Control).Canvas;
 
+  // selected item
   if odSelected in State then
     aCanvas.Brush.Color := clBlack
   else
@@ -64,6 +68,14 @@ begin
   aCanvas.Font.Color := clWhite;
   aCanvas.FillRect(ARect);
 
+  // mouse over
+  if Index = BCComboBox1.HoverItem then
+  begin
+    aCanvas.Pen.Color := clRed;
+    aCanvas.Rectangle(ARect);
+  end;
+
+  // vertically centered text
   aCanvas.TextRect(ARect, 15, ARect.Top +
     (aCanvas.GetTextHeight(TListBox(Control).Items[Index]) div 2),
     TListBox(Control).Items[Index]);