我要做的是在类中创建编辑控件和过程。我尝试了各种各样的东西,并尝试使用我问过的类似问题的一部分:Win32 C++ Create a Window and Procedure Within a Class。
此刻,我把他们拆开了。
main_class.h
class MainClass {
private:
HWND hwndMain; // main windows handle
HINSTANCE hInstanceMain; // main windows instance
HWND hTextarea;
public:
bool init(HWND _hwnd, HINSTANCE _hInstance);
bool ShowInfoTextarea();
};
main_class.cpp
// Heres the question
bool MainClass::ShowInfoTextarea() {
if (hTextarea != NULL) return true; // if it is not null, the textarea is likely already displayed.
// Creating the EDIT textarea
hTextarea = CreateWindowEx(0, L"EDIT", L"",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
SCREEN_WIDTH+5, 0, WINDOW_WIDTH-SCREEN_WIDTH-10, WINDOW_HEIGHT-30, hwndMain, (HMENU)IDC_CTRL_EDIT, GetModuleHandle(NULL), NULL);
if (hTextarea == NULL) { MessageBox(NULL, L"Could not create the test text control. The program will now close.", NULL, MB_OK | MB_ICONEXCLAMATION); return false; }
// Dozens of attempts with something like:
// lpEditWndProc = (WNDPROC)SetWindowLongPtr(hTextarea, GWLP_WNDPROC, (LONG_PTR)EditControlProc);
// lpEditWndProc = (WNDPROC)SetWindowLongPtr(hTextarea, GWLP_WNDPROC, (LONG_PTR)MainClass::EditControlProc);
// tried static callback functions, etc.
//Every try the compiler said: Are there missing braces ( ) with EditControlProc
}
// To get working, I separated:
WNDPROC lpEditWndProc;
LRESULT CALLBACK EditControlProc(HWND hwndEdit, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
default:
return CallWindowProc(lpEditWndProc, hwndEdit, uMsg, wParam, lParam);
}
return 0; // DONE
}
通过我的(各种)尝试,编译器说:在尝试定义lpEditWndProc时,是否缺少带有EditControlProc的大括号(),或者说类型不匹配。
我可能错过了一些简单的事情?
用途:
main.cpp
MainClass mainclass;
mainclass.ShowInfoTextarea();
感谢。如果遗漏任何信息,请告诉我。
答案 0 :(得分:0)
Jonathan Potter的评论是解决方案。
对于任何读者:
mainclass.h
class MainClass {
private:
HWND hwndMain;
HINSTANCE hInstanceMain;
HWND hTextarea;
static LRESULT CALLBACK EditControlProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
public:
bool init(HWND _hwnd, HINSTANCE _hInstance);
bool ShowInfoTextarea();
};
mainclass.cpp
// To Show the Textarea
bool MainClass::ShowInfoTextarea() {
if (hTextarea != NULL) return true; // if it is not null, the textarea is likely already displayed.
// Creating the EDIT textarea
hTextarea = CreateWindowEx(0, L"EDIT", L"",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
SCREEN_WIDTH+5, 0, WINDOW_WIDTH-SCREEN_WIDTH-10, WINDOW_HEIGHT-30, hwndMain, (HMENU)IDC_CTRL_EDIT, GetModuleHandle(NULL), NULL);
if (hTextarea == NULL) { MessageBox(NULL, L"Could not create the test text control. The program will now close.", NULL, MB_OK | MB_ICONEXCLAMATION); return false; }
SetWindowSubclass(hTextarea, EditControlProc, 0, 0);
}
// Now Works
LRESULT CALLBACK MainClass::EditControlProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
switch (msg) {
case WM_KEYDOWN:
MessageBox(0,0,0,0);
break;
default:
return DefSubclassProc(hWnd, msg, wParam, lParam);
break;
}
return 0; // DONE
}