DEV Community

海前 王
海前 王

Posted on

mfc open console test

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()
Enter fullscreen mode Exit fullscreen mode

};

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;
}
Enter fullscreen mode Exit fullscreen mode

};

CMyApp theApp;

Top comments (0)