ユーザーからの文字入力を受け付けるのが、エディットコントロールです。
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND + 1;
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("REVIEW");
RegisterClass(&wc);
hwnd = CreateWindow(TEXT("REVIEW"),
TEXT("Windows API Primer"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
NULL, NULL, 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)
{
RECT rect;
HFONT font;
HWND label1, label2, label3;
static
HWND edit1, edit2, edit3;
// サイズを変更するコントロールは static 指定する必要があります。
switch (msg)
{
case WM_CREATE:
GetClientRect(hwnd, &rect);
font = GetStockObject(DEFAULT_GUI_FONT);
label1 = CreateWindow(TEXT("STATIC"),
TEXT("Title"),
WS_CHILD | WS_VISIBLE | SS_RIGHT | SS_CENTERIMAGE,
5, 5, 40, 20,
hwnd, NULL, NULL, NULL);
SendMessage(label1, WM_SETFONT, (WPARAM)font, 0);
label2 = CreateWindow(TEXT("STATIC"),
TEXT("Author"),
WS_CHILD | WS_VISIBLE | SS_RIGHT | SS_CENTERIMAGE,
5, 30, 40, 20,
hwnd, NULL, NULL, NULL);
SendMessage(label2, WM_SETFONT, (WPARAM)font, 0);
label3 = CreateWindow(TEXT("STATIC"),
TEXT("Review"),
WS_CHILD | WS_VISIBLE | SS_RIGHT | SS_CENTERIMAGE,
5, 55, 40, 20,
hwnd, NULL, NULL, NULL);
SendMessage(label3, WM_SETFONT, (WPARAM)font, 0);
edit1 = CreateWindow(TEXT("EDIT"),
NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER,
50, 5, rect.right - 55, 20,
hwnd, NULL, NULL, NULL);
SendMessage(edit1 , WM_SETFONT, (WPARAM)font, 0);
edit2 = CreateWindow(TEXT("EDIT"),
NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER,
50, 30, rect.right - 55, 20,
hwnd, NULL, NULL, NULL);
SendMessage(edit2, WM_SETFONT, (WPARAM)font, 0);
edit3 = CreateWindow(TEXT("EDIT"),
NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
50, 55, rect.right - 55, rect.bottom - 60,
hwnd, NULL, NULL, NULL);
SendMessage(edit3, WM_SETFONT, (WPARAM)font, 0);
break;
case WM_SIZE:
GetClientRect(hwnd, &rect);
SetWindowPos(edit1 ,HWND_TOP, 0, 0, rect.right - 55, 20, SWP_NOMOVE);
SetWindowPos(edit2, HWND_TOP, 0, 0, rect.right - 55, 20, SWP_NOMOVE);
SetWindowPos(edit3, HWND_TOP, 0, 0, rect.right - 55, rect.bottom - 60,
SWP_NOMOVE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND + 1;
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("REVIEW");
RegisterClass(&wc);
hwnd = CreateWindow(TEXT("REVIEW"),
TEXT("Windows API Primer"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
if (!IsDialogMessage(hwnd, &msg))
{
TranslateMessage(&msg);
DispatchMessage (&msg);
}
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
RECT rect;
HFONT font;
HWND label1, label2, label3;
static HWND edit1, edit2, edit3;
switch (msg)
{
case WM_CREATE:
GetClientRect(hwnd, &rect);
font = GetStockObject(DEFAULT_GUI_FONT);
label1 = CreateWindow(TEXT("STATIC"),
TEXT("Title"),
WS_CHILD | WS_VISIBLE | SS_RIGHT | SS_CENTERIMAGE,
5, 5, 40, 20,
hwnd, NULL, NULL, NULL);
SendMessage(label1, WM_SETFONT, (WPARAM)font, 0);
label2 = CreateWindow(TEXT("STATIC"),
TEXT("Author"),
WS_CHILD | WS_VISIBLE | SS_RIGHT | SS_CENTERIMAGE,
5, 30, 40, 20,
hwnd, NULL, NULL, NULL);
SendMessage(label2, WM_SETFONT, (WPARAM)font, 0);
label3 = CreateWindow(TEXT("STATIC"),
TEXT("Review"),
WS_CHILD | WS_VISIBLE | SS_RIGHT | SS_CENTERIMAGE,
5, 55, 40, 20,
hwnd, NULL, NULL, NULL);
SendMessage(label3, WM_SETFONT, (WPARAM)font, 0);
edit1 = CreateWindow(TEXT("EDIT"),
NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP,
50, 5, rect.right - 55, 20,
hwnd, NULL, NULL, NULL);
SendMessage(edit1 , WM_SETFONT, (WPARAM)font, 0);
edit2 = CreateWindow(TEXT("EDIT"),
NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP,
50, 30, rect.right - 55, 20,
hwnd, NULL, NULL, NULL);
SendMessage(edit2, WM_SETFONT, (WPARAM)font, 0);
edit3 = CreateWindow(TEXT("EDIT"),
NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
50, 55, rect.right - 55, rect.bottom - 60,
hwnd, NULL, NULL, NULL);
SendMessage(edit3, WM_SETFONT, (WPARAM)font, 0);
SetFocus (edit1);
break;
case WM_SIZE:
GetClientRect(hwnd, &rect);
SetWindowPos(edit1 ,HWND_TOP, 0, 0, rect.right - 55, 20, SWP_NOMOVE);
SetWindowPos(edit2, HWND_TOP, 0, 0, rect.right - 55, 20, SWP_NOMOVE);
SetWindowPos(edit3, HWND_TOP, 0, 0, rect.right - 55, rect.bottom - 60,
SWP_NOMOVE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}