C++
一种通用的高级编程语言,作为 C 编程语言的扩展而创建,除了用于低级别内存操作的功能外,还具有面向对象、泛型和功能性等特点。
165 个问题
CreateDialogParamA 的DLGPROC参数类型转换无效怎么解决?
CreateDialogParamA(AfxGetInstanceHandle(), MAKEINTRESOURCEA(IDD_DIALOG1), hwnd, (DLGPROC)GoToProc, NULL);
int iLine; // Receives line number.
BOOL fRelative; // Receives check box status.
BOOL CALLBACK CMainWindow::GoToProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
BOOL fError;
switch (message)
{
case WM_INITDIALOG:
CheckDlgButton((INT)hwndDlg, IDC_BUTTON);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
fRelative = IsDlgButtonChecked((INT)hwndDlg);
iLine = GetDlgItemInt((INT)hwndDlg,0, fError);
if (fError)
{
MessageBox(L"ok", L"hello", 123);
}
else
// Notify the owner window to carry out the task.
return TRUE;
case IDCANCEL:
DestroyWindow();
hwndGoto = NULL;
return TRUE;
}
}
return FALSE;
}
您好,
在C++中,将类的成员函数作为回调函数传递给另一个函数时,需要注意成员函数的类型和指针的类型之间的差异。因为成员函数有一个隐含的 this
指针参数,无法直接传递成员函数指针作为普通的回调函数。
使用静态成员函数或全局函数
INT_PTR CALLBACK MyDialogClass::s_DlgProc(
HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
MyDialogClass *pThis; // our "this" pointer will go here
if (uMsg == WM_INITDIALOG) {
// Recover the "this" pointer which was passed as the last parameter
// to the ...Dialog...Param function.
pThis = reinterpret_cast<MyDialogClass*>(lParam);
// Put the value in a safe place for future use
SetWindowLongPtr(hdlg, DWLP_USER,
reinterpret_cast<LONG_PTR>(pThis));
} else {
// Recover the "this" pointer from where our WM_INITDIALOG handler
// stashed it.
pThis = reinterpret_cast<MyDialogClass*>(
GetWindowLongPtr(hdlg, DWLP_USER));
}
if (pThis) {
// Now that we have recovered our "this" pointer, let the
// member function finish the job.
return pThis->DlgProc(hwnd, uMsg, wParam, lParam);
}
// We don't know what our "this" pointer is, so just do the default
// thing. Hopefully, we didn't need to customize the behavior yet.
return FALSE; // returning FALSE means "do the default thing"
}
class MyWindowClass
{
... other class stuff goes here ...
// This is the static callback that we register
static LRESULT CALLBACK s_WndProc(
HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
// The static callback recovers the "this" pointer and then
// calls this member function.
LRESULT WndProc(
HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
};
void MyWindowClass::SomeMemberFunction()
{
// to register the class
WNDCLASS wc;
... fill out the window class as normal ...
wc.lpfnWndProc = MyWindowClass::s_WndProc;
wc.lpszClassName = TEXT("MyWindowClass");
RegisterClass(&wc);
// to create a window
hwnd = CreateWindow(TEXT("MyWindowClass"),
... other parameters as usual ...,
this);
}
class MyDialogClass
{
... other class stuff goes here ...
// This is the static callback that we register
static INT_PTR CALLBACK s_DlgProc(
HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
// The static callback recovers the "this" pointer and then
// calls this member function.
INT_PTR DlgProc(
HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
};
void MyDialogClass::SomeMemberFunction()
{
// to create the dialog box
DialogBoxParam(... other parameters as usual ...,
reinterpret_cast<LPARAM>(this));
}
如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。
注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。