CRect rect; //GetClientRect(&rect);
GetWindowRect(&rect); HWND static1 = CreateWindowA("Button","Button",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,rect.top+150,rect.left+20,100,40+20,m_hWnd,(HMENU)1000,hInst,(LPVOID)1000) ;
::ShowWindow(static1,1);
这样做出了1个按钮。但是怎么获取他的 clicked 事件呢?LRESULT CALLBACK CclientDlg::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)这里的  WM_COMMAND 好像没作用

解决方案 »

  1.   

    HWND static1 = CreateWindowA("Button","Button",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,rect.top+150,rect.left+20,100,40+20,m_hWnd,(HMENU)1000,hInst,(LPVOID)1000) ; 
    代码中有两个问题:
    1:直接使用CreateWindow来创建窗口.至于调用CreateWindowA或CreateWindowW取决于是否定义_UNICODE
    2:最后一个参数,应为一个指针.MSDN描述如下:lpParam
    [in]  Pointer to a value to be passed to the window through the CREATESTRUCT structure (lpCreateParams member) pointed to by the lParam param of the WM_CREATE message. This message is sent to the created window by this function before it returns.If an application calls CreateWindow to create a multiple-document interface (MDI) client window, lpParam should point to a CLIENTCREATESTRUCT structure. If an MDI client window calls CreateWindow to create an MDI child window, lpParam should point to a MDICREATESTRUCT structure. lpParam may be NULL if no additional data is needed.响应Button clicked事件,应在父窗口的WM_COMMAND中,LOWORD(wParam)为button id,HIWORD(wParam)为事件id。父窗口由CreateWindow函数中的第八个参数指定。
      

  2.   

    响应BN_CLICKED消息
    BN_CLICKED idButton = (int)LOWORD(wParam);
      hwndButton = (HWND) lParam;
    Parameters
    idButton 
    Identifier of the button. 
    hwndButton 
    Handle to the button. 
      

  3.   

    响应 WM_COMMANDvoid XXX_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
    {
        switch (id)
        {
            case [YourID]:
                //...
                break;
        }
    }
      

  4.   

    响应BN_CLICKED消息 
    BN_CLICKED idButton = (int)LOWORD(wParam); 
      hwndButton = (HWND) lParam; 
    Parameters 
    idButton 
    Identifier of the button. 
    hwndButton 
    Handle to the button. 很不清晰?