创建一个WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nCmdShow)工程,
在实现过程中CreateWindow(....)。
在创建工程的同时,我也导入有菜单的资源Erp.rc文件,资源里面包含了一个IDC_MENU
菜单,请问在CraeteWindow后,怎样加载IDC_MANU?
我是使用了CMenu menu.LoadMenu(IDC_MENU); menu.TrackPopupMenu
但是还是有问题,问题是AfxGetResourceHandle()出现了错误,因为在工程里头,没有用到
CWinApp类,所以我AfxGetResourceHandle(hInst),结果还是出现了问题,请问怎么解决?感谢!祝大家新年快乐!

解决方案 »

  1.   

    其实,上面的问题也说不太清楚,我目的就是:
    在WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nCmdShow)工程
    里头怎样使用自定义use.dll里头的资源文件?谢谢,比如想使用use.dll里头的
    Dialog资源,菜单IDC_MENU等。谢谢
      

  2.   

    函数声明如下:
    HHOOK SetWindowsHookEx(
      int idHook,        // 钩子类型
      HOOKPROC lpfn,     // 钩子函数
      HINSTANCE hMod,    // 钩子函数所存在的模块句柄
      DWORD dwThreadId   // 指定线程id值
    );
    对各个参数的解释:
     第一个参数:指定要安装的钩子的类型(将在后面详细应用中用到)
    第二个参数:钩子函数指针。如果是和异地线程或者是所有存在的线程相关联(既是全局钩子),
    那么这个函数必须是在动态链接库中(当然不绝对,日志钩子是全局钩子,但是它的实现可以不用在动态链接库中);
    第三个参数:如果此模块为dll,则为0(NULL);
    第四参数: 全局钩子为0,其他为线程id值;
    首先给出一个很特殊的日志钩子实例,它是用来纪录和回放系统消息的,这时idhook=WH_JOURNALRECORD(纪录),
    idhook=WH_JOURNALPLAYBACK(回放):
    源代码如下:(vc and sdk) 
    //***********************************************************/
    #include <windows.h>
    #include "resource.h"HHOOK ghook=0, ghook1=0;
    int count=0, count1=0;
    EVENTMSG eventmsg[60000];
    bool ok;HINSTANCE hInst=NULL;LRESULT CALLBACK JournalRecordProc(int code,  WPARAM wParam, LPARAM lParam );      
    LRESULT CALLBACK JournalPlaybackProc(int code, WPARAM wParam,LPARAM lParam );        
    LRESULT CALLBACK wndproc(HWND,UINT,WPARAM,LPARAM);
    LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hpreinst,LPSTR cmdline,int ncmdshow)
    {   
    HWND hWnd; 
    ok=true;

     MSG  msg;
     WNDCLASS wndclass;
     char title[]="Hook Window for Test";
     char classname[]="ZK-window";  hInst =hInstance;  //save the handle of instance to global vaiable  wndclass.style=0;
     wndclass.lpfnWndProc=wndproc;
     wndclass.cbClsExtra=0;
     wndclass.cbWndExtra=0;
     wndclass.hInstance=hInstance;
     wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
     wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
     wndclass.hbrBackground=HBRUSH(GetStockObject(WHITE_BRUSH));
     wndclass.lpszMenuName=(LPCSTR)IDR_MENU_MAIN;
     wndclass.lpszClassName=classname;
     if(!RegisterClass(&wndclass))
     {
      MessageBeep(0);
      return FALSE;
     }
     hWnd=CreateWindow(classname,
                    title,
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
          CW_USEDEFAULT,
    CW_USEDEFAULT,
    0,
    0,
    hInstance,
    0);
     ShowWindow(hWnd,ncmdshow);
     UpdateWindow(hWnd);
     while(GetMessage(&msg,0,0,0))
     {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
     }
     return msg.wParam;
    }LRESULT CALLBACK wndproc(HWND hWnd,UINT message,WPARAM wparam,LPARAM lparam)
    {
     switch(message)
     {  
     
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
     
    case WM_COMMAND:
        switch(LOWORD(wparam))
    {
        case IDM_HELP:
    ShellExecute(hWnd, NULL, "readme.txt", NULL, NULL, SW_SHOWNORMAL);
    break; case IDM_ABOUT:
    DialogBox(hInst, (LPCTSTR)IDD_ABOUT, hWnd, (DLGPROC)About);
    MessageBox(0,"感谢使用 , 多提意见","copy and modified by zdleek from csdn.net",0);
    break;

    case IDM_RECORD_HOOK:    
    ok=true;
    count=0;
    ghook=SetWindowsHookEx(WH_JOURNALRECORD,HOOKPROC(JournalRecordProc),hInst,0);
    break; case IDM_UNHOOK:
    UnhookWindowsHookEx(ghook);   
    break;

    case IDM_PLAYBACK_HOOK:
    count1=0;
    ghook1=SetWindowsHookEx(WH_JOURNALPLAYBACK,HOOKPROC(JournalPlaybackProc),hInst,0);
    break;
    case IDM_EXIT:
    PostQuitMessage(0);
    break;
    }
    break; 
    } return DefWindowProc(hWnd,message,wparam,lparam); 
    }//proc for JournalRecord-hook
    LRESULT CALLBACK JournalRecordProc(
      int code,       
      WPARAM wParam,  
      LPARAM lParam   
       )
    {  
     if(code<0) return CallNextHookEx(ghook,code,wParam,lParam);
     else if(code==HC_ACTION && count<=60000)
     {  
      EVENTMSG *pevt=(EVENTMSG*)lParam;
      eventmsg[count]=*pevt;
      count=count+1;
     }
     return 0;}//proc for JournalPlayback-hook
    LRESULT CALLBACK JournalPlaybackProc(int code,WPARAM wParam,LPARAM lParam )   
    {  
         if(code<0) return CallNextHookEx(ghook1,code,wParam,lParam );
            
         else if(code==HC_GETNEXT  )
         { 
          
            
             if(count1==0) 
         
         {
      EVENTMSG *pevt=(EVENTMSG*)lParam;
      (*pevt)=eventmsg[count1];
      count1=1;
      return 50;
     }
          if(count1>=1 )
          {  
           
          
          if(ok==false)
          { ok=true; 
          EVENTMSG *pevt=(EVENTMSG*)lParam;
                      (*pevt)=eventmsg[count1];
          return 0;   }            else if(ok==true )
          
          {  count1++;
              if(count1<=count)
       {   
    if(count1==count) {  UnhookWindowsHookEx(ghook1);}
    ok=false;  
    return (eventmsg[count1].time-eventmsg[count1-1].time);
            
       }
            
          }
         }
        
         }
        
        return 0;
    }// Mesage handler for about box.
    LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    HWND hItem;
    switch (message)
    {
    case WM_INITDIALOG:
    return TRUE; case WM_COMMAND:
    switch(LOWORD(wParam))
    {
    case IDOK:
    //SetWindowText(hDlg,"Register OCX Now"); //Change the dialog windows caption

    hItem=GetDlgItem(hDlg,IDOK);  //get the Item(Button) handle
    if (hItem != NULL)
    {
    /************************************
    HPEN hpen, hpenOld;
    HBRUSH hbrush, hbrushOld;
    HDC hdc; hdc = GetDC(hItem); 
    BeginPath(hdc);  // Create a green pen.
    hpen = CreatePen(PS_SOLID, 2, RGB(100, 255, 50));
    // Create a red brush.
    hbrush = CreateSolidBrush(RGB(200, 80, 100)); // Select the new pen and brush, and then draw.
    hpenOld = (HPEN)SelectObject(hdc, hpen);
    hbrushOld = (HBRUSH)SelectObject(hdc, hbrush);
    //*********************************************/

    //change the OK-Button's caption
    SetWindowText(hItem,"Clicked");    

    /*********************************************
    EndPath(hdc);
    // Do not forget to clean up.
    SelectObject(hdc, hpenOld);
    DeleteObject(hpen);
    SelectObject(hdc, hbrushOld);
    DeleteObject(hbrush);
    //********************************************/
    }
    break; case IDCANCEL:
    EndDialog(hDlg, LOWORD(wParam));
    return TRUE;
    break;

    default:
    return TRUE;
    }
    break;
    }
        return FALSE;
    }
      

  3.   

    既然你是用SDK,就不要用MFC的类了,MFC类有些需要CWinApp实例的。直接用::LoadMenu()不行么?