DialogProc function asking to declare it self static [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

This question already has an answer here:

here is my createdialogparam function which is calling DialogProc function from here-

 HRESULT AMEPreviewHandler::CreatePreviewWindow()     {         assert(m_hwndPreview == NULL);         assert(m_hwndParent != NULL);         HRESULT hr = S_OK;          m_hwndPreview = CreateDialogParam( g_hInst,MAKEINTRESOURCE(IDD_MAINDIALOG), m_hwndParent,(DLGPROC)DialogProc, (LPARAM)this); /here the dialog proc function is called         if (m_hwndPreview == NULL)         {           hr = HRESULT_FROM_WIN32(GetLastError());         }     ..........     ...     }

here is the definition of DialogProc function-

BOOL CALLBACK AMEPreviewHandler::DialogProc(HWND m_hwndPreview, UINT Umsg, WPARAM wParam, LPARAM lParam)      {          static RECT m_rcParent ;          switch(Umsg)         {         case WM_INITDIALOG:              {             return 0;             }             break; ........ case WM_COMMAND:             {                   int ctl = LOWORD(wParam);                 int event = HIWORD(wParam);                  if (ctl == IDC_PREVIOUS && event == BN_CLICKED )                  {                               CreateHtmlPreview(); //it must be static now and it is not able to access the non static vraibles delared globally in the program                     return 0;                 }      } }

and the declaration is like this-

static BOOL CALLBACK DialogProc(HWND hWindow, UINT uMsg, WPARAM wParam, LPARAM lParam); //suppose it is static ..it is not giving any error if static ..If it is not declared static it gives error
here -

m_hwndPreview = CreateDialogParam( g_hInst,MAKEINTRESOURCE(IDD_MAINDIALOG), m_hwndParent,(DLGPROC)DialogProc, (LPARAM)this); //error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'DLGPROC'

Is there any way to access the globally declared variables inside the static DialogProc or it is possible to access the globally declared variables inside the dialogproc without declaring the those variables as static because t hey are also used as non static in other part of program ??

回答1:

If by

globally declared variables inside the static DialogProc

you mean member variables in an instance of AMEPreviewHandler, I think you have sent what you need in the LPARAM:

m_hwndPreview = CreateDialogParam( ...(LPARAM)this);

When it calls your DialogProc these go to the last parameter: LPARAM lParam so you can do

AMEPreviewHandler* instance = (AMEPreviewHandler *)lParam; instance->m_Whatever...


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!