编程实现单文档应用程序,满足以下要求: (1)MFC标准类型,经典菜单与不带工具栏; (2)单击鼠标左键,消息对话框中显示鼠标信息(鼠标左键+坐标); (3)单击鼠标左键+键盘Ctrl键,消息对话框中显示鼠标信息(鼠标左键+Ctrl键+坐标); (4)按下键盘任意字符键(至少识别三个功能键),客户区中显示按键信息(字符或功能键内容)
时间: 2023-05-31 22:04:19 浏览: 94
以下是实现代码:
MyApp.h
```
#pragma once
class CMyApp : public CWinApp
{
public:
CMyApp();
public:
virtual BOOL InitInstance();
private:
static BOOL CALLBACK MouseProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam);
private:
static CWnd* m_pWnd;
};
```
MyApp.cpp
```
#include "stdafx.h"
#include "MyApp.h"
#include "MyFrameWnd.h"
#include <afxwin.h>
CWnd* CMyApp::m_pWnd = NULL;
CMyApp::CMyApp()
{
}
BOOL CMyApp::InitInstance()
{
CWinApp::InitInstance();
CMyFrameWnd* pFrameWnd = new CMyFrameWnd;
pFrameWnd->Create(NULL, _T("Single Document Application"), WS_OVERLAPPEDWINDOW, CRect(0, 0, 800, 600));
pFrameWnd->ShowWindow(SW_SHOW);
m_pMainWnd = pFrameWnd;
m_pWnd = pFrameWnd;
SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, AfxGetInstanceHandle(), 0);
HINSTANCE hInstance = AfxGetInstanceHandle();
SetWindowsHookEx(WH_MOUSE_LL, MouseProc, hInstance, 0);
return TRUE;
}
BOOL CALLBACK CMyApp::MouseProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_LBUTTONDOWN)
{
CString strMsg;
strMsg.Format(_T("Mouse left button clicked at (%d, %d)"), LOWORD(lParam), HIWORD(lParam));
if (GetKeyState(VK_CONTROL) & 0x8000)
{
strMsg += _T(" Ctrl key pressed");
}
AfxMessageBox(strMsg);
}
return CallNextHookEx(NULL, message, wParam, lParam);
}
LRESULT CALLBACK CMyApp::KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
if (code == HC_ACTION)
{
CString strMsg;
if ((GetKeyState(VK_CONTROL) & 0x8000) && (wParam == 'A'))
{
strMsg = _T("Ctrl+A pressed");
}
else if ((GetKeyState(VK_CONTROL) & 0x8000) && (wParam == 'B'))
{
strMsg = _T("Ctrl+B pressed");
}
else if ((GetKeyState(VK_CONTROL) & 0x8000) && (wParam == 'C'))
{
strMsg = _T("Ctrl+C pressed");
}
else
{
strMsg.Format(_T("Key pressed: %c"), wParam);
}
m_pWnd->SetWindowText(strMsg);
}
return CallNextHookEx(NULL, code, wParam, lParam);
}
```
MyFrameWnd.h
```
#pragma once
class CMyFrameWnd : public CFrameWnd
{
public:
CMyFrameWnd();
virtual ~CMyFrameWnd();
protected:
DECLARE_MESSAGE_MAP()
};
```
MyFrameWnd.cpp
```
#include "stdafx.h"
#include "MyFrameWnd.h"
CMyFrameWnd::CMyFrameWnd()
{
}
CMyFrameWnd::~CMyFrameWnd()
{
}
BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd)
END_MESSAGE_MAP()
```
阅读全文