ResizeDlg
Visual C++ 2005 Express Edition
Windows Server 2003 SP1 Platform SDK
Windowsアプリケーション / 空のプロジェクト
Windows Programming Tips
ResizeDlg.c
// マルチバイト文字セット #include
#include "resource.h" #define CLASS_WINDOW1 "Window1" // グローバル変数 static HINSTANCE g_hInstance; static HWND g_hWindow1; static int g_iWindow1Y; // プロトタイプ宣言 INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); void OnSize(HWND hDlg); void OnGetMinMaxInfo(LPARAM lParam); void OnInitDialog(HWND hDlg); void CenterWindow(HWND hWnd); LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wcex; g_hInstance = hInstance; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = NULL; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.lpszMenuName = NULL; wcex.lpszClassName = CLASS_WINDOW1; wcex.hIconSm = NULL; RegisterClassEx(&wcex); DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN_DIALOG), NULL, DlgProc); return 0; } INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { BOOL bRetVal = TRUE; switch (uMsg) { case WM_COMMAND: switch (LOWORD(wParam)) { case IDC_BUTTON1: MessageBox(hDlg, "hello, world", "Hello", MB_OK); break; } break; case WM_SIZE: OnSize(hDlg); break; case WM_GETMINMAXINFO: OnGetMinMaxInfo(lParam); break; case WM_INITDIALOG: OnInitDialog(hDlg); bRetVal = TRUE; // TRUEならデフォルトフォーカスを使用 break; case WM_CLOSE: EndDialog(hDlg, IDOK); break; default: bRetVal = FALSE; } return bRetVal; } void OnSize(HWND hDlg) { RECT rc; int iWidth; int iHeight; GetClientRect(hDlg, &rc); iWidth = rc.right - 8; iHeight = rc.bottom - (g_iWindow1Y + 4); MoveWindow(g_hWindow1, 4, g_iWindow1Y, iWidth, iHeight, TRUE); } void OnGetMinMaxInfo(LPARAM lParam) { MINMAXINFO *pmmi; pmmi = (MINMAXINFO*)lParam; pmmi->ptMinTrackSize.x = 480; pmmi->ptMinTrackSize.y = 240; } void OnInitDialog(HWND hDlg) { RECT rc; int iDluY; // 中央 CenterWindow(hDlg); // DLU rc.top = 8; MapDialogRect(hDlg, &rc); iDluY = rc.top; g_iWindow1Y = (40 * iDluY) / 8; g_hWindow1 = CreateWindow(CLASS_WINDOW1, NULL, WS_CHILD | WS_VISIBLE | WS_HSCROLL, 0, 0, 0, 0, hDlg, 0, g_hInstance, NULL); OnSize(hDlg); } void CenterWindow(HWND hWnd) { RECT rcParent; RECT rcThis; int iWidth; int iHeight; GetWindowRect(GetDesktopWindow(), &rcParent); GetWindowRect(hWnd, &rcThis); iWidth = rcThis.right - rcThis.left; iHeight = rcThis.bottom - rcThis.top; MoveWindow(hWnd, (rcParent.right - iWidth) / 2, (rcParent.bottom - iHeight) / 2, iWidth, iHeight, FALSE); } LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return 0; }
resource.h
#define IDD_MAIN_DIALOG 100 #define IDC_BUTTON1 1000 #define IDC_EDIT1 1001 #define IDC_WINDOW1 1002
ResizeDlg.rc
// リソーススクリプト #include
#include "resource.h" IDD_MAIN_DIALOG DIALOGEX 0, 0, 320, 240 STYLE WS_POPUPWINDOW | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX EXSTYLE WS_EX_APPWINDOW CAPTION "ResizeDlg" FONT 9, "MS Pゴシック" BEGIN PUSHBUTTON "Button1",IDC_BUTTON1,4,4,64,16 EDITTEXT IDC_EDIT1,4,24,64,12,ES_AUTOHSCROLL END