2 zjutchenjm zjutchenjm 于 2013.06.03 15:30 提问

Win32消息,子窗口如何接受WM_KEYDOWN消息

我有一个主窗口,里面创建了一个子窗口。所有的操作其实都是为了对子窗口里的图片进行处理。但是一些控件如Button等放在主窗口。现在我想要这个子窗口处理我的PAGEUP和PAGEDOWN键盘消息,但是在主窗口的消息处理函数中可以接收到WM_KEYDOWN消息,而在子窗口中却不能接收到。我知道可以通过主窗口主动POST消息给子窗口实现,但是我想知道能不能直接在子窗口里接收消息。比如,主窗口放弃处理这个消息,并传递给子窗口。我对Windows的消息传递机制一直不是很清楚,希望有懂的大神简单讲解一下这个问题,非常感谢。
case WM_KEYDOWN:
switch (wParam)
{
case VK_LEFT: // LEFT ARROW
MessageBox(hWnd,"Left","Left", MB_YESNO);
break;
case VK_RIGHT: // RIGHT ARROW
MessageBox(hWnd,"Right","Right", MB_YESNO);
break;
case VK_UP: // UP ARROW
MessageBox(hWnd,"UP","UP", MB_YESNO);
break;
case VK_DOWN: // DOWN ARROW
MessageBox(hWnd,"DOWN","DOWN", MB_YESNO);
break;
case VK_HOME: // HOME
MessageBox(hWnd,"HOME","HOME", MB_YESNO);
break;
case VK_END: // END
MessageBox(hWnd,"END","END", MB_YESNO);
break;
case VK_PRIOR: // PAGE UP
MessageBox(hWnd,"PAGEUP","PAGEUP",MB_YESNO);
break;
case VK_NEXT: // PAGE DOWN
MessageBox(hWnd,"PAGEDOWN","PAGEDOWN",MB_YESNO);
break;
default:
break;
}
break;

2个回答

wangyaninglm
wangyaninglm   Ds   Rxr 2015.01.15 20:40

case COMMAND_GET_DIRECTORY:
{
CmdAndHost cmdhost;
memset(&cmdhost,0,sizeof(CmdAndHost));
memcpy(&cmdhost.cmd,&cmd,sizeof(Command));
cmdhost.host = host;
::SendMessage(((CFileManagerDlg*)cmd.hWind)->m_hWnd,WM_FILE_ONRECEIVE,0,(LPARAM)(&cmdhost));
break;
}
上面是主窗口的接受消息,下面是消息处理:

    LRESULT CFileManagerDlg::OnReceiveData(WPARAM wParam, LPARAM lParam)//加载上线信息

{
CmdAndHost cmdandhost = (CmdAndHost *)lParam;
Command * cmd = &(cmdandhost->cmd);
m_host = (mdk::NetHost
)cmdandhost->host;
switch(cmd->order)
{
case COMMAND_GET_DIRECTORY:
{
unsigned char * buf = (unsigned char * )malloc(cmd->datasize);
while ( !m_host->Recv(buf, cmd->datasize) )
Sleep(500);
AddToList(cmd->datasize, (LPARAM )buf);
free(buf);

            break;
        }
    case COMMAND_WRONG_DIRECTORY:
        {
            MessageBox("目录为空或不可读","警告",MB_OK);

            break;
        }

}

return 0;

}

wangyaninglm
wangyaninglm   Ds   Rxr 2015.01.15 20:41

用这个映射:
ON_MESSAGE(WM_FILE_ONRECEIVE, &CFileManagerDlg::OnReceiveData) //

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!