在控制台试图子类化 [英] Trying Subclassing on Console

查看:87
本文介绍了在控制台试图子类化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个控制台应用程序陷阱键盘消息,所以我尝试这样的:

I want to trap keyboard messages in a console application, so I tried this:

HWND GetConsoleHwnd(void)
{
    #define SIZEBUF 1024
    char szBuffer[SIZEBUF];

    GetConsoleTitle(szBuffer, SIZEBUF);

    #undef SIZEBUF
    return FindWindow(NULL, szBuffer);
}

LRESULT CALLBACK ConsoleProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_KEYDOWN:
            switch (wParam)
            {
               //VK Cases
            }
            break;
    }

    return CallWindowProc(OldConsoleProc, hwnd, msg, wParam, lParam);
}

这主:

HWND hwndConsole = GetConsoleHwnd();
OldConsoleProc = (WNDPROC) SetWindowLong(hwndConsole, GWL_WNDPROC,
                               ConsoleProc);

和这个全局变量: WNDPROC OldConsoleProc;

但它不工作,我在做什么错了?

but it doesnt work, what I am doing wrong?

推荐答案

您不能子类另一个进程的窗口这种方式。 您可以,但我不会推荐钩子做在控制台窗口中尝试此。 ReadConsoleInput 是低级别不够,这是尽可能您可以在不丑不可移植的黑客得到(我甚至不知道有的一些事件达到的WndProc 时,控制台窗口是全屏幕)。

You can't subclass a window of another process this way. You can do it with hooks but I wouldn't recommend trying this on console window. ReadConsoleInput is low-level enough, and it's as far as you can get without ugly nonportable hacks (I'm not even sure there are some events reaching WndProc when the console window is full screen).

这篇关于在控制台试图子类化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆