delphi - 如何在 TcxComboBox 中抑制鼠标滚轮

标签 delphi combobox mousewheel

我需要为表单上的所有组合组件禁用鼠标滚轮的项目滚动。 最好的是有或多或少通用的解决方案,因为表单的设计可能会改变,如果新的组合组件将被忽略而不需要对源代码进行任何额外的工作,那就太好了。 我有两种类型的组合: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;

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

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

最佳答案

您可以继承自己的组件或使用覆盖 DoMouseWheel 的插入器类,而不是 Hook 表单。您可以将处理绑定(bind)到其他属性。

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;

关于delphi - 如何在 TcxComboBox 中抑制鼠标滚轮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19399121/

相关文章:

c# - 有没有一种简单的方法可以在 WinForms 中实现 Checked Combobox

java - 正确处理 Ext-GWT 中的 OnMouseWheel 事件

web-services - Delphi 7 Indy 9 多线程 HTTP 服务器

delphi - 使用 Synapse 的 IMAP + TLS/SSL?

delphi - 编辑路径中的第一个INI文件

c# - 如何使 wpf 组合框拉出特殊属性

multithreading - 多个连续线程上的 TThread.WaitFor

python - 使用 tkinter 编辑文本时显示下拉组合框

javascript - onWheel 事件 - 如何立即触发方法,而不是在用户停止滚动后触发

Linux:使用鼠标加*键盘*修饰符模拟滚动