Advertisement
vovan333

PRODUCTIONNEUH

Dec 31st, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2. #include <CommCtrl.h>
  3. #include <gdiplus.h>
  4. #include <string>
  5.  
  6. using namespace std;
  7. using namespace Gdiplus;
  8.  
  9. namespace D3DTest1
  10. {
  11.     HINSTANCE hCurrentInstance;
  12.     Image* openedImage;
  13.     Graphics* gc_MainWindow;
  14.     Graphics* gc_Img;
  15.     HWND hMainWindow;
  16.     HWND hMainWindowStatusBar;
  17.     int clientAreaWidth = 800;
  18.     int clientAreaHeight = 600;
  19.  
  20.     enum ToolbarItem
  21.     {
  22.         File_New = 0,
  23.         File_Open = 1,
  24.         File_Save = 2,
  25.         File_Close = 3
  26.     };
  27.  
  28.     OPENFILENAMEW CreateOFN(HWND hOwnerWindow = hMainWindow)
  29.     {
  30.         OPENFILENAMEW ofn;
  31.         WCHAR filename[MAX_PATH];
  32.         ZeroMemory(&ofn, sizeof(ofn));
  33.  
  34.         ofn.lStructSize = sizeof(ofn);
  35.         ofn.hwndOwner = hOwnerWindow;
  36.         ofn.lpstrFile = filename;
  37.         ofn.lpstrFile[0] = '\0';
  38.         ofn.nMaxFile = sizeof(filename);
  39.         ofn.lpstrFilter = L"All\0*.*\0Text\0*.TXT\0";
  40.         ofn.nFilterIndex = 1;
  41.         ofn.lpstrFileTitle = NULL;
  42.         ofn.nMaxFileTitle = 0;
  43.         ofn.lpstrInitialDir = NULL;
  44.         ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  45.  
  46.         return ofn;
  47.     }
  48.  
  49.     int MessageBox(LPCWSTR text, LPCWSTR caption = L"Alert", long type = MB_OK, HWND hWnd = hMainWindow)
  50.     {
  51.         return MessageBox(hWnd, text, caption, type);
  52.     }
  53.  
  54.     void OnKeyDown(WPARAM virtualKeyCode)
  55.     {
  56.         wstring caption = L"You pressed the following key: \n";
  57.         int charCode = MapVirtualKey(virtualKeyCode, MAPVK_VK_TO_CHAR);
  58.         wchar_t key = (wchar_t)charCode;
  59.         wstring text = caption + L"Character: \"" + key + L"\"\nCharcode: " + to_wstring(charCode);
  60.         MessageBox(text.c_str());
  61.     }
  62.  
  63.     HBITMAP LoadBitmap(LPCWSTR filename)
  64.     {
  65.         return (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  66.     }
  67.  
  68.     void SetAppState(LPCWSTR state = L"Ready")
  69.     {
  70.         SendMessage(hMainWindowStatusBar, SB_SETTEXT, 0, (LPARAM)state);
  71.     }
  72.  
  73.     void OpenFile(LPCWSTR filename)
  74.     {
  75.         openedImage = Image::FromFile(filename);
  76.         gc_Img = Graphics::FromImage(openedImage);
  77.     }
  78.  
  79.     LPCWSTR ChooseFile()
  80.     {
  81.         OPENFILENAMEW ofn = CreateOFN();
  82.         GetOpenFileNameW(&ofn);
  83.         return ofn.lpstrFile;
  84.     }
  85.  
  86.     void Render()
  87.     {
  88.         if (openedImage != nullptr)
  89.         {
  90.             Rect clientArea = Rect(0, 0, clientAreaWidth, clientAreaHeight);
  91.             gc_MainWindow->DrawImage(openedImage, clientArea);
  92.         }
  93.     }
  94.  
  95.     LRESULT CALLBACK OnWindowMessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  96.     {
  97.         int mbResult;
  98.         switch (message)
  99.         {
  100.             case WM_CLOSE:
  101.                 mbResult = MessageBox(L"Are you sure you want to exit?", L"Exit?", MB_YESNO | MB_ICONWARNING);
  102.                 if (mbResult == IDYES)
  103.                 {
  104.                     DestroyWindow(hWnd);
  105.                 }
  106.                 return 0;
  107.                 break;
  108.  
  109.             case WM_DESTROY:
  110.                 PostQuitMessage(0);
  111.                 return 0;
  112.                 break;
  113.  
  114.             case WM_KEYDOWN:
  115.                 OnKeyDown(wParam);
  116.                 break;
  117.  
  118.             case WM_SIZE:
  119.                 SendMessage(hMainWindowStatusBar, WM_SIZE, NULL, NULL);
  120.                 break;
  121.  
  122.             case WM_COMMAND:
  123.                 switch (LOWORD(wParam))
  124.                 {
  125.                     case ToolbarItem::File_Open:
  126.                         if (LPCWSTR filename = ChooseFile())
  127.                         {
  128.                             OpenFile(filename);
  129.                         }
  130.                         break;
  131.                 }
  132.                 break;
  133.  
  134.             case WM_PAINT:
  135.                 Render();
  136.                 break;
  137.         }
  138.         return DefWindowProc(hWnd, message, wParam, lParam);
  139.     }
  140.  
  141.     HWND CreateStatusBar(HWND hParentWindow, int id, LPCWSTR text = L"Ready")
  142.     {
  143.         return CreateStatusWindow(WS_CHILD | WS_VISIBLE, text, hParentWindow, id);
  144.     }
  145.  
  146.     HMENU CreateMainWindowToolbar()
  147.     {
  148.         HMENU hToolbar = CreateMenu();
  149.  
  150.         HMENU hFileMenu = CreatePopupMenu();
  151.         AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_Open, L"Open");
  152.         AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_New, L"New");
  153.         AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_Save, L"Save");
  154.         AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_Close, L"Close");
  155.  
  156.         AppendMenu(hToolbar, MF_POPUP, (UINT_PTR)hFileMenu, L"File");
  157.  
  158.         return hToolbar;
  159.     }
  160.  
  161.     int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  162.     {
  163.         WNDCLASSEX wc;
  164.         ZeroMemory(&wc, sizeof(wc));
  165.  
  166.         wc.cbSize = sizeof(WNDCLASSEX);
  167.         wc.style = CS_HREDRAW | CS_VREDRAW;
  168.         wc.lpfnWndProc = OnWindowMessage;
  169.         wc.hInstance = hInstance;
  170.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  171.         wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  172.         wc.lpszClassName = L"WC_Default";
  173.  
  174.         RegisterClassEx(&wc);
  175.  
  176.         RECT wr = { 0, 0, clientAreaWidth, clientAreaHeight };
  177.  
  178.         AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
  179.  
  180.         HMENU hMainWindowMenu = CreateMainWindowToolbar();
  181.  
  182.         hMainWindow = CreateWindowEx
  183.         (
  184.             NULL,
  185.             L"WC_Default",
  186.             L"D3DTest1 Main Window",
  187.             WS_OVERLAPPEDWINDOW,
  188.             300,
  189.             300,
  190.             wr.right - wr.left,
  191.             wr.bottom - wr.top,
  192.             NULL,
  193.             hMainWindowMenu,
  194.             hInstance,
  195.             NULL
  196.         );
  197.  
  198.         hMainWindowStatusBar = CreateStatusBar(hMainWindow, 0);
  199.  
  200.         gc_MainWindow = &Graphics(hMainWindow);
  201.  
  202.         ShowWindow(hMainWindow, nShowCmd);
  203.  
  204.         MSG message;
  205.         while (GetMessage(&message, NULL, 0, 0))
  206.         {
  207.             TranslateMessage(&message);
  208.             DispatchMessage(&message);
  209.         }
  210.  
  211.         return 0;
  212.     }
  213. }
  214.  
  215. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  216. {
  217.     D3DTest1::WinMain(hInstance, hPrevInstance, lpCmdLine, nShowCmd);
  218. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement