Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <CommCtrl.h>
- #include <gdiplus.h>
- #include <string>
- using namespace std;
- using namespace Gdiplus;
- namespace D3DTest1
- {
- HINSTANCE hCurrentInstance;
- Image* openedImage;
- Graphics* gc_MainWindow;
- Graphics* gc_Img;
- HWND hMainWindow;
- HWND hMainWindowStatusBar;
- int clientAreaWidth = 800;
- int clientAreaHeight = 600;
- enum ToolbarItem
- {
- File_New = 0,
- File_Open = 1,
- File_Save = 2,
- File_Close = 3
- };
- OPENFILENAMEW CreateOFN(HWND hOwnerWindow = hMainWindow)
- {
- OPENFILENAMEW 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 OnKeyDown(WPARAM virtualKeyCode)
- {
- wstring caption = L"You pressed the following key: \n";
- int charCode = MapVirtualKey(virtualKeyCode, MAPVK_VK_TO_CHAR);
- wchar_t key = (wchar_t)charCode;
- wstring text = caption + L"Character: \"" + key + L"\"\nCharcode: " + to_wstring(charCode);
- MessageBox(text.c_str());
- }
- HBITMAP LoadBitmap(LPCWSTR filename)
- {
- return (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
- }
- void SetAppState(LPCWSTR state = L"Ready")
- {
- SendMessage(hMainWindowStatusBar, SB_SETTEXT, 0, (LPARAM)state);
- }
- void OpenFile(LPCWSTR filename)
- {
- openedImage = Image::FromFile(filename);
- gc_Img = Graphics::FromImage(openedImage);
- }
- LPCWSTR ChooseFile()
- {
- OPENFILENAMEW ofn = CreateOFN();
- GetOpenFileNameW(&ofn);
- return ofn.lpstrFile;
- }
- void Render()
- {
- if (openedImage != nullptr)
- {
- Rect clientArea = Rect(0, 0, clientAreaWidth, clientAreaHeight);
- gc_MainWindow->DrawImage(openedImage, 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())
- {
- OpenFile(filename);
- }
- break;
- }
- break;
- case WM_PAINT:
- Render();
- 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;
- }
- int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
- {
- 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);
- RECT wr = { 0, 0, clientAreaWidth, clientAreaHeight };
- AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
- HMENU hMainWindowMenu = CreateMainWindowToolbar();
- hMainWindow = CreateWindowEx
- (
- NULL,
- L"WC_Default",
- L"D3DTest1 Main Window",
- WS_OVERLAPPEDWINDOW,
- 300,
- 300,
- wr.right - wr.left,
- wr.bottom - wr.top,
- NULL,
- hMainWindowMenu,
- hInstance,
- NULL
- );
- hMainWindowStatusBar = CreateStatusBar(hMainWindow, 0);
- gc_MainWindow = &Graphics(hMainWindow);
- ShowWindow(hMainWindow, nShowCmd);
- MSG message;
- while (GetMessage(&message, NULL, 0, 0))
- {
- TranslateMessage(&message);
- DispatchMessage(&message);
- }
- return 0;
- }
- }
- int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
- {
- D3DTest1::WinMain(hInstance, hPrevInstance, lpCmdLine, nShowCmd);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement