【问题标题】:How to stop the beep after I handle the WM_SYSKEYDOWN message? [duplicate]处理 WM_SYSKEYDOWN 消息后如何停止哔声? [复制]
【发布时间】:2015-07-05 20:00:47
【问题描述】:

当我按下 Alt+Enter 组合键时,我希望我的控件执行一些操作。我通过捕获WM_SYSKEYDOWN 来实现这一点。但是每次按下“syskey”组合时,系统都会发出哔哔声,我不知道如何停止它。我尝试返回 0(Message.Result:=0),清除字符代码(Message.CharCode:=0)或不调用继承的方法但没有成功。我发现这个哔声不是在我的WMSysKeyDown 程序中发出的,而是在它之后发出的。

在我真正的控件中,我没有 TEdit,但这没关系,每个控件都会发出哔声。

unit Unit1;

interface

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

type
  TMyControl = class(TEdit)
  private
    procedure WMSysKeyDown(var Message: TWMSysKeyDown); message WM_SYSKEYDOWN;
  end;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    MyControl:TMyControl;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TMyControl.WMSysKeyDown(var Message: TWMKey);
begin
 case Message.CharCode of
  VK_RETURN: if (Message.KeyData and $40000000)=0 then begin
    TForm(Parent).Caption:=TForm(Parent).Caption+' x';
    Message.Result:=0;
  end;
 end;
 inherited;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 MyControl:=TMyControl.Create(Form1);
 MyControl.Parent:=Form1;
end;

end.

【问题讨论】:

  • @TLama,WM_SYSCHAR 是! :) 我删除了CharCode(分配#0)并且不再发出哔哔声。再次感谢。

标签: delphi delphi-2009 messages


【解决方案1】:

使用 PeekMessage 函数。 (Winapi 函数)。删除队列中的所有消息。我想你会在winapi peekmessage帮助中找到详细的说明。

procedure TMyControl.WMSysKeyDown(var Message: TWMKey);
var
  Mesaj : TMsg ;
begin
 case Message.CharCode of
  VK_RETURN:
    if (Message.KeyData and $40000000)=0 then
    begin
      TForm(Parent).Caption:=TForm(Parent).Caption+' x';
      PeekMessage(Mesaj, Form1.Handle, 0, 0, PM_REMOVE); // Here is the answer. Delete all queued messages in the message queue
    end;
  end;
 inherited;
end;

【讨论】:

  • 确实,它正在工作......但我怎样才能删除队列中的所有消息?也许有重要的消息......
  • 不是删除所有消息。删除消息队列中的仅一条消息。我想我解释错了(对不起我的英语:)当用户按下 RETURN 键时它只删除一条消息。我希望我现在解释正确。
  • 对不起我的英语。它删除这里的一条消息是删除所有消息的解释。我写了删除一条消息:)
  • msdn.microsoft.com/en-us/library/windows/desktop/…(哎呀,我是堆栈溢出用户界面的新手。我在写的时候出错了:)
  • PeekMessage(Mesaj, Form1.Handle, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE) 请试试这个。仅删除 KEYboard 消息。它不会触及其他消息。
猜你喜欢
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2021-12-05
  • 1970-01-01
  • 2017-07-31
  • 2014-12-22
  • 1970-01-01
相关资源
最近更新 更多