您的位置:首页 > 编程语言 > Delphi

Delphi CxCombobox 拦截滚轮事件,使之聚焦时文本框内的内容不随着滚轮的滚动而发生变化

2013-10-19 12:34 579 查看
  在写程序时,我发现在滚动鼠标滚轮时,原意是想让整个页面向下Scroll,但是此时恰巧

页面焦点聚焦在Combobox的文本框内,滚动滚轮却是把Combobox内的内容给改变了。正常情况下这个也不影响什么,有时也方便操作,但是在此时的情况下无疑是不合适的,因为用户可能在不知情的情况下会把Combobox内的内容给改变,这无疑是不能发生的,

所以,我就自己写段代码,来阻止这种事情发生。

  RzCombobox有滚轮事件,但是Delphi自带的Combobox还有Dev Express的cxCombobox没有此事件,那么我们就可以自己写代码来拦截滚轮事件,以下是代码:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Panel1: TPanel;
ComboBox1: TComboBox;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
Procedure OnMouseWheel(Var Msg: TMsg; var Handled: Boolean);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMessage := OnMouseWheel;
end;

procedure TForm1.OnMouseWheel(var Msg: TMsg; var Handled: Boolean);
begin
if Msg.message = WM_MOUSEWHEEL then
begin
//ShowMessage(ActiveControl.ClassName);
if ActiveControl IS TComboBox then  //Delphi自带Combobox
//if ActiveControl.ClassName = 'TcxCustomComboBoxInnerEdit' then  //CxCombobox
begin
Handled := True;
end;
end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
Application.OnMessage := nil;
end;

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: