鼠标滚动时组合框项目列表浮动在窗体上

Combo-box items list floating on the form when mouse scrolling

此问题与常规 TComboBox 组件有关。 从组合框访问 下拉列表 时存在问题,然后当 滚动表单 时,字段列表仍保留在同一位置。从用户的角度来看,这真的很烦人。

有什么方法可以在滚动表单时自动失去控制焦点吗?还有其他想法吗?

请查看下面的示例代码以重现该问题。

unit Unit10;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, vcl.wwcheckbox, Vcl.Mask, vcl.wwdbedit, Vcl.ExtCtrls,
  vcl.wwdotdot, vcl.wwdbcomb, vcl.wwdbdatetimepicker;

type
  TForm10 = class(TForm)
    grpPanels: TCategoryPanelGroup;
    pnl1: TCategoryPanel;
    pnl2: TCategoryPanel;
    ComboBox1: TComboBox;
    procedure FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
      var Handled: Boolean);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  var
  Form10 : TForm10;

implementation

{$R *.dfm}

procedure TForm10.FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
  var Handled: Boolean);
begin
   if WheelDelta > 0 then
      grpPanels.Perform(WM_VSCROLL, SB_LINEUP, 0)
   else
      grpPanels.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
end;

procedure TForm10.FormShow(Sender: TObject);
var
   i: Integer;
begin
   for i := 0 to 5 do
      ComboBox1.Items.Add(IntToStr(i));
end;

end. 

Is there any way to automatically lose the control focus when scrolling through the form? Any other ideas?

添加到 FormMouseWheel() 的开头:

确保组合失去焦点:

  if ActiveControl is TComboBox then
    FocusControl(nil);

或者,确保下拉菜单关闭,但保持焦点

  if ActiveControl is TComboBox then
    TComboBox(ActiveControl).DroppedDown := False;

我找到了另一种方法来处理这个问题并想与您分享。 问题是其他 WIN32 vcl 组件也存在问题,例如 TDateTimePickerTComboBox

为了在不失去控制焦点的情况下处理所有这些情况,我们可以在 FormMouseWheel() 的开头调用 CM_CANCELMODE 消息:

for i := 0 to ComponentCount - 1 do
   if Components[i] is TWinControl then
      TWinControl(Components[i]).Perform(CM_CANCELMODE, 0, 0);