#include #include #include #include using namespace std; using namespace Gdiplus; namespace D3DTest1 { HINSTANCE hCurrentInstance; LPCWSTR openImageFilename; Image* pOpenImage; HWND hMainWindow; HWND hMainWindowStatusBar; int clientAreaWidth = 800; int clientAreaHeight = 600; UINT windowStyle = WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU; BOOL debug = TRUE; HANDLE hConsole; enum ToolbarItem { File_New = 0, File_Open = 1, File_Save = 2, File_Close = 3 }; OPENFILENAME CreateOFN(HWND hOwnerWindow = hMainWindow) { OPENFILENAME ofn; WCHAR filename[MAX_PATH]; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hOwnerWindow; ofn.lpstrFile = filename; ofn.lpstrFile[0] = '\0'; ofn.nMaxFile = sizeof(filename); ofn.lpstrFilter = L"All\0*.*\0Text\0*.TXT\0"; ofn.nFilterIndex = 1; ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = 0; ofn.lpstrInitialDir = NULL; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; return ofn; } int MessageBox(LPCWSTR text, LPCWSTR caption = L"Alert", long type = MB_OK, HWND hWnd = hMainWindow) { return MessageBox(hWnd, text, caption, type); } void SetAppState(LPCWSTR state = L"Ready") { SendMessage(hMainWindowStatusBar, SB_SETTEXT, 0, (LPARAM)state); } void OnKeyDown(WPARAM virtualKeyCode) { wstring yptfk = L"You pressed the following key: \n"; int charCode = MapVirtualKey(virtualKeyCode, MAPVK_VK_TO_CHAR); wchar_t key = (wchar_t)charCode; wstring text = yptfk + L"Character: \"" + key + L"\"\nCharcode: " + to_wstring(charCode); MessageBox(text.c_str()); } Size AdjustWindowSize(int width, int height) { RECT wr = { 0, 0, width, height }; AdjustWindowRect(&wr, windowStyle, FALSE); return Size(wr.right - wr.left, wr.bottom - wr.top); } void SetClientAreaSize(int width, int height, HWND hWnd = hMainWindow) { clientAreaWidth = width; clientAreaHeight = height; Size size = AdjustWindowSize(width, height); MoveWindow(hWnd, 0, 0, size.Width, size.Height, TRUE); } void LoadFile(LPCWSTR filename) { // Disabled due to malfunction. // openImageFilename = filename; // pOpenImage = Image::FromFile(filename); filename = L"C:/Users/vovan/Desktop/sample.bmp"; openImageFilename = filename; pOpenImage = Image::FromFile(filename); int width = pOpenImage->GetWidth(); int height = pOpenImage->GetHeight(); SetClientAreaSize(width, height); } LPCWSTR ChooseFile() { OPENFILENAME ofn = CreateOFN(); GetOpenFileName(&ofn); return ofn.lpstrFile; } void Render(HDC hdc) { if (pOpenImage != nullptr) { Graphics gc(hdc); Rect clientArea(0, 0, clientAreaWidth, clientAreaHeight); gc.DrawImage(pOpenImage, clientArea); } } LRESULT CALLBACK OnWindowMessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int mbResult; switch (message) { case WM_CLOSE: mbResult = MessageBox(L"Are you sure you want to exit?", L"Exit?", MB_YESNO | MB_ICONWARNING); if (mbResult == IDYES) { DestroyWindow(hWnd); } return 0; break; case WM_DESTROY: PostQuitMessage(0); return 0; break; case WM_KEYDOWN: OnKeyDown(wParam); break; case WM_SIZE: SendMessage(hMainWindowStatusBar, WM_SIZE, NULL, NULL); break; case WM_COMMAND: switch (LOWORD(wParam)) { case ToolbarItem::File_Open: if (LPCWSTR filename = ChooseFile()) { LoadFile(filename); } break; } break; case WM_PAINT: PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); Render(hdc); EndPaint(hWnd, &ps); break; } return DefWindowProc(hWnd, message, wParam, lParam); } HWND CreateStatusBar(HWND hParentWindow, int id, LPCWSTR text = L"Ready") { return CreateStatusWindow(WS_CHILD | WS_VISIBLE, text, hParentWindow, id); } HMENU CreateMainWindowToolbar() { HMENU hToolbar = CreateMenu(); HMENU hFileMenu = CreatePopupMenu(); AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_Open, L"Open"); AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_New, L"New"); AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_Save, L"Save"); AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_Close, L"Close"); AppendMenu(hToolbar, MF_POPUP, (UINT_PTR)hFileMenu, L"File"); return hToolbar; } void WriteFileA(HANDLE hFile, LPCSTR data) { WriteFile(hFile, data, strlen(data), 0, NULL); } void Log(LPCSTR data) { if (debug) { WriteFileA(hConsole, data); WriteFileA(hConsole, "\n"); } } void Log(int data) { Log(to_string(data).c_str()); } void LoadConsole() { AllocConsole(); hConsole = GetStdHandle(STD_OUTPUT_HANDLE); } int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { if (debug) { LoadConsole(); } WNDCLASSEX wc; ZeroMemory(&wc, sizeof(wc)); wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = OnWindowMessage; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)COLOR_WINDOW; wc.lpszClassName = L"WC_Default"; RegisterClassEx(&wc); Size sz = AdjustWindowSize(clientAreaWidth, clientAreaHeight); HMENU hMainWindowMenu = CreateMainWindowToolbar(); GdiplusStartupInput gdipsi; ULONG_PTR gdipToken; GdiplusStartup(&gdipToken, &gdipsi, NULL); hMainWindow = CreateWindowEx ( NULL, L"WC_Default", L"D3DTest1 Main Window", windowStyle, 300, 300, sz.Width, sz.Height, NULL, hMainWindowMenu, hInstance, NULL ); hMainWindowStatusBar = CreateStatusBar(hMainWindow, 0); ShowWindow(hMainWindow, nShowCmd); MSG message; while (GetMessage(&message, NULL, 0, 0)) { TranslateMessage(&message); DispatchMessage(&message); } GdiplusShutdown(gdipToken); return 0; } } int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { D3DTest1::WinMain(hInstance, hPrevInstance, lpCmdLine, nShowCmd); };