// DialogTest01.cpp // // Dialog display sample. // Dialog are defined in resource file. #include #include #include #include #include #include "framework.h" #include "DialogTest01.h" #define MAX_LOADSTRING 100 #define IDC_BUTTON_OPENDLG 110 // global work HINSTANCE hInst; WCHAR szTitle[MAX_LOADSTRING]; // title bar text WCHAR szWindowClass[MAX_LOADSTRING]; // main window class name static WCHAR txt[1024]; // message work // global paramater work static int waitValue = 15; static int speedValue = 1000; static int numberValue = 1000; static int fps_display = 1; // prototype LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM); // entry point int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // init global sstring LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW(hInstance, IDC_DIALOGTEST01, szWindowClass, MAX_LOADSTRING); // Register window class { WNDCLASSEXW wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DIALOGTEST01)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.lpszMenuName = nullptr; // set menu wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); RegisterClassExW(&wcex); } // init application. create main window { hInst = hInstance; HWND hWnd = CreateWindowW( szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, // x, y 320, 240, // width, height nullptr, nullptr, hInstance, nullptr ); if (!hWnd) return FALSE; ShowWindow(hWnd, nCmdShow); // display main window UpdateWindow(hWnd); } MSG msg; // main message loop while (GetMessage(&msg, nullptr, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } // main window procedure LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { // static WCHAR txt[1024]; switch (message) { case WM_CREATE: // create main window // create "Open Dialog" button CreateWindow( L"BUTTON", L"Open Dialog", WS_CHILD | WS_VISIBLE, 20, 20, 100, 24, // x, y, width, height hWnd, (HMENU)IDC_BUTTON_OPENDLG, hInst, NULL ); break; case WM_COMMAND: { int wmId = LOWORD(wParam); switch (wmId) { case IDC_BUTTON_OPENDLG: { // push "Open Dialog" button // Open dialog int ret; ret = DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, DlgProc); // dialog results switch (ret) { case IDOK: // push "OK" button wcscpy_s(txt, L"IDOK"); // set text message break; case IDCANCEL: // push "Cancel" button wcscpy_s(txt, L"IDCANCEL"); // set text message break; } InvalidateRect(hWnd, NULL, TRUE); } break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_PAINT: { // draw main window PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); // draw message TextOut(hdc, 20, 70, txt, lstrlen(txt)); // draw paramater value { WCHAR s[256]; swprintf_s(s, L"wait=%d", waitValue); TextOut(hdc, 20, 90, s, lstrlen(s)); swprintf_s(s, L"speed=%d", speedValue); TextOut(hdc, 20, 110, s, lstrlen(s)); swprintf_s(s, L"number=%d", numberValue); TextOut(hdc, 20, 130, s, lstrlen(s)); swprintf_s(s, L"fps_display=%d", fps_display); TextOut(hdc, 20, 150, s, lstrlen(s)); } EndPaint(hWnd, &ps); } break; case WM_DESTROY: // destroy main window PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // dialog procedure INT_PTR CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: { // initialize dialog HWND cHwnd; // wait slider (trackbar) cHwnd = GetDlgItem(hDlg, IDC_SLIDER1); // get hundle SendMessage(cHwnd, TBM_SETRANGE, TRUE, MAKELPARAM(5, 200)); // set range SendMessage(cHwnd, TBM_SETTICFREQ, 5, 0); // set tick SendMessage(cHwnd, TBM_SETPOS, TRUE, (LPARAM)waitValue); // set position // speed slider cHwnd = GetDlgItem(hDlg, IDC_SLIDER2); SendMessage(cHwnd, TBM_SETRANGE, TRUE, MAKELPARAM(100, 4000)); SendMessage(cHwnd, TBM_SETTICFREQ, 100, 0); SendMessage(cHwnd, TBM_SETPOS, TRUE, (LPARAM)speedValue); // number slider cHwnd = GetDlgItem(hDlg, IDC_SLIDER3); SendMessage(cHwnd, TBM_SETRANGE, TRUE, MAKELPARAM(10, 4000)); SendMessage(cHwnd, TBM_SETTICFREQ, 100, 0); SendMessage(cHwnd, TBM_SETPOS, TRUE, (LPARAM)numberValue); // init checkbox SendMessage(GetDlgItem(hDlg, IDC_CHECK1), BM_SETCHECK, ((fps_display == 0) ? BST_UNCHECKED : BST_CHECKED), 0); } return (INT_PTR)TRUE; case WM_COMMAND: switch (LOWORD(wParam)) { case IDOK: // push "OK" button // get paramater and set global work waitValue = SendMessage(GetDlgItem(hDlg, IDC_SLIDER1), TBM_GETPOS, NULL, NULL); speedValue = SendMessage(GetDlgItem(hDlg, IDC_SLIDER2), TBM_GETPOS, NULL, NULL); numberValue = SendMessage(GetDlgItem(hDlg, IDC_SLIDER3), TBM_GETPOS, NULL, NULL); if (SendMessage(GetDlgItem(hDlg, IDC_CHECK1), BM_GETCHECK, 0, 0) == BST_UNCHECKED) fps_display = 0; else fps_display = 1; EndDialog(hDlg, IDOK); return (INT_PTR)TRUE; case IDCANCEL: // push "Cancel" button EndDialog(hDlg, IDCANCEL); return (INT_PTR)TRUE; case IDC_BUTTON_RESET: { // push "Reset" button // change status text SetWindowText(GetDlgItem(hDlg, IDC_STATUS1), TEXT("Reset")); // set slider position SendMessage(GetDlgItem(hDlg, IDC_SLIDER1), TBM_SETPOS, TRUE, 15); SendMessage(GetDlgItem(hDlg, IDC_SLIDER2), TBM_SETPOS, TRUE, 1000); SendMessage(GetDlgItem(hDlg, IDC_SLIDER3), TBM_SETPOS, TRUE, 1000); // change checkbox SendMessage(GetDlgItem(hDlg, IDC_CHECK1), BM_SETCHECK, BST_CHECKED, 0); } return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; }