问题
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...
来源:https://stackoverflow.com/questions/17790479/dialogproc-function-asking-to-declare-it-self-static