7

我需要为表单上的所有组合组件禁用鼠标滚轮滚动项目。最重要的是有或多或少的通用解决方案,因为表单的设计可能会改变,如果新的组合组件将被忽略而无需对源代码进行任何额外的工作,那就太好了。我有两种类型的组合:TComboBox 和 TcxComboBox(来自 DevExpress ExpressBars Suit)。我试着这样做:

procedure TSomeForm.FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
  var Handled: Boolean);
begin
  if (Screen.ActiveControl is TComboBox) or (Screen.ActiveControl is TcxComboBox) then
    Handled := True;
end;

它适用于 TComboBox,但当 TcxComboBox 具有焦点时,此事件处理程序从未触发。我试图在表单级别上捕获相应的消息,如下所示:

procedure TSomeForm.WndProc(var m: TMessage);
begin
  if (m.Msg = WM_VSCROLL) or (m.Msg = WM_HSCROLL) or (m.msg = WM_Mousewheel) then
    m.Msg := 0;
  inherited;
end;

但是这样的消息永远不会到达这个处理程序。我试图直接禁用 TcxComboBox 的鼠标滚轮处理,因为它具有这样的属性:

procedure TSomeForm.FormCreate(Sender: TObject);
begin
  cxComboBox1.Properties.UseMouseWheel := False;
end;

但它不起作用,仍然可以使用鼠标滚轮滚动项目。我为此问题发布了支持票,但即使他们在下一个版本中修复它,我现在也需要一些解决方案。

任何想法,也许有人以某种方式解决了它?

4

1 回答 1

9

您可以继承自己的组件或使用中介类覆盖而不是挂钩表单DoMouseWheel。您可以将处理绑定到其他属性。

type
  TcxComboBox = Class(cxDropDownEdit.TcxComboBox)
    function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean; override;
  private
    FUseMouseWheel: Boolean;
  public
    Property UseMouseWheel: Boolean Read FUseMouseWheel Write FUseMouseWheel;
  End;

  TComboBox = Class(Vcl.StdCtrls.TComboBox)
    function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean; override;
  private
    FUseMouseWheel: Boolean;
  public
    Property UseMouseWheel: Boolean Read FUseMouseWheel Write FUseMouseWheel;
  End;

  TForm3 = class(TForm)
    ComboBox1: TComboBox;
    cxComboBox1: TcxComboBox;
    cxComboBox2: TcxComboBox;
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}
{ TComboBox }

function TComboBox.DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean;
begin
 if FUseMouseWheel then inherited
 else Result := true;
end;

{ TcxComboBox }

function TcxComboBox.DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean;
begin
 if FUseMouseWheel then inherited
 else Result := true;

end;

procedure TForm3.FormCreate(Sender: TObject);
begin
    cxComboBox2.UseMouseWheel := true;
end;
于 2013-10-16T09:26:51.667 回答