处理系统键关闭消息时禁用烦人的声音
Disable annoying sound when handling syskeydown message
嗨,我已经编写了自己的自定义控件,从 MFC CEdit 重写。为此,我需要覆盖SYS_KEY_UP和SYS_KEY_DOWN。但是,每次控件处理这些消息时,都会听到烦人的声音,就好像输入无效或其他什么一样。有谁知道是什么产生了声音以及如何禁用它。
更新代码
// CShotcutEdit
class CShortcutEdit : public CEdit
{
DECLARE_DYNAMIC(CShortcutEdit)
public:
CShortcutEdit();
virtual ~CShortcutEdit();
protected:
DECLARE_MESSAGE_MAP()
afx_msg UINT OnGetDlgCode();
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnSysKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
virtual BOOL PreTranslateMessage(MSG* pMsg);
virtual void DoDataExchange(CDataExchange* pDX);
virtual void PreSubclassWindow();
virtual void OnChar(UINT uiChar, UINT uiCount, UINT uiFlags);
// ... Other Members
};
// ShotcutEdit.cpp : implementation file
//
#include "stdafx.h"
#include "GridPlus.h"
#include "ShortcutEdit.h"
// CShotcutEdit
IMPLEMENT_DYNAMIC(CShortcutEdit, CEdit)
CShortcutEdit::CShortcutEdit()
{
}
CShortcutEdit::~CShortcutEdit()
{
}
BEGIN_MESSAGE_MAP(CShortcutEdit, CEdit)
ON_WM_GETDLGCODE()
ON_WM_KEYDOWN()
ON_WM_SYSKEYDOWN()
ON_WM_SYSKEYUP()
ON_WM_KEYUP()
ON_WM_CHAR()
END_MESSAGE_MAP()
UINT CShortcutEdit::OnGetDlgCode()
{
return DLGC_WANTARROWS | DLGC_WANTALLKEYS | DLGC_WANTCHARS;
}
void CShortcutEdit::DoDataExchange(CDataExchange* pDX)
{
CEdit::DoDataExchange(pDX);
}
BOOL CShortcutEdit::PreTranslateMessage(MSG* pMsg)
{
return CEdit::PreTranslateMessage(pMsg);
}
void CShortcutEdit::OnSysKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
}
void CShortcutEdit::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// ... Check for Alt or F10 and translate to string;
}
void CShortcutEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// .. Check for Ctrl Shift and translate to string
}
void CShortcutEdit::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
SetWindowText(L"The desired shortcut is..."); // Should print "alt+f10" for example
}
void CShortcutEdit::OnChar(UINT uiChar, UINT uiCount, UINT uiFlags)
{
}
void CShortcutEdit::PreSubclassWindow()
{
HideCaret();
CEdit::PreSubclassWindow();
}
// CShotcutEdit message handlers
这是编辑控件的默认行为。记事本在响应 Alt+A 键时执行相同的操作。
为避免声音,过载PreTranslateMessage
BOOL CShortcutEdit::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_SYSKEYDOWN)
{
UINT nFlags = HIWORD(pMsg->lParam);
int nChar = (int)pMsg->wParam;
if (nChar == 'A' && nFlags & KF_ALTDOWN)
{
TRACE("ALT+A Keyn");
return 1;
}
}
return CEdit::PreTranslateMessage(pMsg);
}
此外,如果您不需要编辑控件,则不要使用编辑控件,而不是使用编辑控件并破坏每个输入。
您可能需要 CHotKeyCtrl
相关文章:
- boost::进程间消息队列引发错误
- 在线编译器中的分段C++没有打印消息
- C++错误消息*成员参考.**初学者*
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- Xaudio2在更改缓冲区或循环时弹出声音
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何通过参数抛出错误消息
- 从服务器传输到客户端的消息不会出现
- 如何更改'a'声音?
- ROS2 动态消息模板
- C++秘密消息学校作业
- glad 导致 glfwSwapBuffers 返回错误消息
- 如何收听Windows当前正在播放的声音?
- C++入门 5 版:类消息和文件夹
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 由于无效的 ValidateRgn() 子窗口不会收到WM_PAINT消息
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 处理系统键关闭消息时禁用烦人的声音