mfc open console test
海前 王
Posted on September 6, 2024
define _CRT_SECURE_NO_WARNINGS
include // MFC 核心和标准组件
include // 标准输入输出流
include // Windows API 函数
class CMyFrameWnd : public CFrameWnd {
public:
CMyFrameWnd() {
Create(NULL, _T("Key Event Handling Window"), WS_OVERLAPPEDWINDOW, CRect(0, 0, 300, 200));
}
protected:
// 处理按键事件
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) {
// 输出按键虚拟键码到控制台
std::cout << "Key Pressed: " << nChar << std::endl;
// 调用基类的处理方法
CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd)
ON_WM_KEYDOWN()
END_MESSAGE_MAP()
class CMyApp : public CWinApp {
public:
virtual BOOL InitInstance() {
// 创建控制台窗口
AllocConsole();
freopen("CONOUT$", "w", stdout);
freopen("CONIN$", "r", stdin);
freopen("CONIN$", "r", stderr);
CMyFrameWnd* pFrame = new CMyFrameWnd;
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
};
CMyApp theApp;
Posted on September 6, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.