VC++(非MFC)で、EditBox作成時の作法(もしくはコードの誤り)について質問です。
ウィンドウ内に子ウィンドウを作成し、子ウィンドウ上にEditBoxを配置しました。
このEditBoxについて、SetWindowText関数で文字列を表示させることはできるのですが、
キーボードからの入力ができず、マウスでクリックしてもカーソルが表示されません。
親ウィンドウ上にも同じつくり方でEditBoxを配置したところ、こちらは正常に文字入力ができました。
子ウィンドウ上に配置する場合には親ウィンドウ上と何か設定を変える必要があるのでしょうか?
子ウィンドウに配置したEditBoxへの入力ができるようにするには何をすべきか教えていただきたいと思います。
よろしくお願いいたします。
以下、現象を再現させた最低限のコードです。
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChildWndProc(HWND, UINT, WPARAM, LPARAM);
char szClassNme[] = "W001";
char szClassNme2[] = "W002";
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInst,LPSTR lpszCmdLine, int nCmdShow) {
HWND hWnd;
HWND hChildWnd;
MSG msg;
WNDCLASS myProg;
if (!hPreInst) {
myProg.style = CS_HREDRAW | CS_VREDRAW;
myProg.lpfnWndProc = WndProc;
myProg.cbClsExtra = 0;
myProg.cbWndExtra = 0;
myProg.hInstance = hInstance;
myProg.hIcon = NULL;
myProg.hCursor = LoadCursor(NULL, IDC_ARROW);
myProg.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
myProg.lpszMenuName = NULL;
myProg.lpszClassName = szClassNme;
if (!RegisterClass(&myProg)) return FALSE;
myProg.lpfnWndProc = ChildWndProc;
myProg.lpszClassName = szClassNme2;
if (!RegisterClass(&myProg)) return FALSE;
}
// 親ウィンドウを作成する
hWnd = CreateWindow(szClassNme,
"テスト", WS_OVERLAPPEDWINDOW | WS_VISIBLE , 0, 0, 400, 250, NULL, NULL, hInstance, NULL);
// 親ウィンドウ上にエディットボックスを作成する
// これには入力できる。
CreateWindow("EDIT", "", WS_CHILD | WS_VISIBLE | WS_BORDER, 10, 125, 100, 25,
hWnd, (HMENU)1001, hInstance, NULL);
// 子ウィンドウを作成する
hChildWnd = CreateWindow(szClassNme2,
"子ウィンドウ", WS_CHILD | WS_CAPTION | WS_VISIBLE, 0, 0, 200, 100, hWnd, NULL, hInstance, NULL);
// 子ウィンドウ上にエディットボックスを作成する
// これには入力ができない。
CreateWindow("EDIT", "", WS_CHILD | WS_VISIBLE | WS_BORDER, 10, 10, 100, 25,
hChildWnd, (HMENU)1002, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd, msg, wParam, lParam));
}
return (0L);
}
LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd, msg, wParam, lParam));
}
return (0L);
}