#include #include #include #include #include #include #define ID_STTL 577 #define ID_SWDT 587 #define ID_SHGT 597 #define ID_SADD 549 #define ID_SINFO 519 #define ID_SNEW 1000 #define MAX_CHARS 16 HWND hMain; HINSTANCE cur; TCHAR buf[MAX_CHARS], title[MAX_CHARS]; int width, height, tmp; int maxWidth = 170, maxHeight = 170; int btnCount = ID_SNEW, hCur = 160; LONG WINAPI WndProc(HWND, UINT, WPARAM,LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASS w; memset(&w, 0, sizeof(WNDCLASS)); w.style = CS_HREDRAW | CS_VREDRAW; w.lpfnWndProc = WndProc; w.hInstance = hInstance; cur = hInstance; w.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); w.lpszClassName = L"Lab3"; w.hIcon = LoadIcon(NULL, IDI_EXCLAMATION); w.hCursor = LoadCursor(NULL, IDC_ARROW); RegisterClass(&w); hMain = CreateWindow(L"Lab3", L"Nepryakhin 3121", WS_CAPTION | WS_POPUPWINDOW, 200, 200, 200, 800, NULL, NULL, hInstance, NULL); // create 3 textboxes (btn title, width, height) HWND hTitle = CreateWindow(L"Edit", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | ES_LEFT, 120, 8, 60, 20, hMain, (HMENU)ID_STTL, hInstance, 0); HWND hWidth = CreateWindow(L"Edit", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | ES_LEFT | ES_NUMBER, 120, 38, 60, 20, hMain, (HMENU)ID_SWDT, hInstance, 0); HWND hHeight = CreateWindow(L"Edit", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | ES_LEFT | ES_NUMBER, 120, 68, 60, 20, hMain, (HMENU)ID_SHGT, hInstance, 0); HWND hInfo = CreateWindow(L"Edit", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | ES_LEFT | ES_READONLY, 10, 128, 170, 20, hMain, (HMENU)ID_SINFO, hInstance, 0); SetWindowText(hTitle, L"Btn"); // def title SetWindowText(hWidth, L"50"); // def width SetWindowText(hHeight, L"20"); // def height // create button to create new btns HWND hAddBtn = CreateWindow(L"Button", NULL, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 10, 98, 170, 20, hMain, (HMENU)ID_SADD, hInstance, 0); SetWindowText(hAddBtn, L"Add Button"); ShowWindow(hMain, nCmdShow); UpdateWindow(hMain); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } void SetText(int CID, int data) { StringCbPrintf(buf, sizeof(buf), L"%d", data); SetDlgItemText(hMain, CID, buf); } void TextBoxHandle(WPARAM param) { // get text, set btn title if (LOWORD(param) == ID_STTL) GetDlgItemText(hMain, ID_STTL, title, MAX_CHARS); // get width if (LOWORD(param) == ID_SWDT) { // get text, convert to int GetDlgItemText(hMain, ID_SWDT, buf, MAX_CHARS); tmp = _tstoi(buf); // check max, set value if (tmp <= maxWidth) width = tmp; else SetText(ID_SWDT, maxWidth); } if (LOWORD(param) == ID_SHGT) { // get text, convert to int GetDlgItemText(hMain, ID_SHGT, buf, MAX_CHARS); tmp = _tstoi(buf); // check max, set value if (tmp <= maxHeight) height = tmp; else SetText(ID_SHGT, maxHeight); } } class Button { public: // w/ params Button(LPCWSTR ttl, int w, int h) { HWND hNewBtn = CreateWindow(L"Button", NULL, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 10, hCur, w, h, hMain, (HMENU)btnCount++, cur, 0); SetWindowText(hNewBtn, ttl); addHeight(h); } public: // default Button() { HWND hNewBtn = CreateWindow(L"Button", NULL, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 10, hCur, 50, 20, hMain, (HMENU)btnCount++, cur, 0); SetWindowText(hNewBtn, L"Default"); addHeight(20); } private: // add btn h and gap to total h void addHeight(int val) { hCur = hCur + val + 10; } }; LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam) { HDC hdc; PAINTSTRUCT ps; switch (Message){ case WM_DESTROY: PostQuitMessage(0); break; case WM_COMMAND: if (HIWORD(wparam) == EN_UPDATE) TextBoxHandle(wparam); // goto tbox handle if (HIWORD(wparam) == BN_CLICKED) if (LOWORD(wparam) == ID_SADD) // 'new btn add' clicked if (width == 0 || height == 0 || wcslen(title) == 0) Button *btn = new Button(); // some value is null, use def else // or use params Button *btn = new Button(title, width, height); else { // other btn cliked, set to tb its title GetDlgItemText(hMain, LOWORD(wparam), buf, MAX_CHARS); StringCbPrintf(buf, sizeof(buf), L"%s clicked", buf); SetDlgItemText(hMain, ID_SINFO, buf); } break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); TextOut(hdc, 10, 10, L"button title:", 13); TextOut(hdc, 10, 40, L"button width:", 13); TextOut(hdc, 10, 70, L"button height:", 14); EndPaint(hwnd, &ps); break; default: return DefWindowProc(hwnd, Message, wparam, lparam); } return 0; }