Advertisement
vovan333

i LPCSTRed Microsoft Developers' mothers yesterday

Jan 2nd, 2017
139
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.     LPCWSTR openImageFilename;
  13.     Image* pOpenImage;
  14.     HWND hMainWindow;
  15.     HWND hMainWindowStatusBar;
  16.     int clientAreaWidth = 800;
  17.     int clientAreaHeight = 600;
  18.     UINT windowStyle = WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU;
  19.     BOOL debug = TRUE;
  20.     HANDLE hConsole;
  21.  
  22.     enum ToolbarItem
  23.     {
  24.         File_New = 0,
  25.         File_Open = 1,
  26.         File_Save = 2,
  27.         File_Close = 3
  28.     };
  29.  
  30.     OPENFILENAME CreateOFN(HWND hOwnerWindow = hMainWindow)
  31.     {
  32.         OPENFILENAME ofn;
  33.         WCHAR filename[MAX_PATH];
  34.         ZeroMemory(&ofn, sizeof(ofn));
  35.  
  36.         ofn.lStructSize = sizeof(ofn);
  37.         ofn.hwndOwner = hOwnerWindow;
  38.         ofn.lpstrFile = filename;
  39.         ofn.lpstrFile[0] = '\0';
  40.         ofn.nMaxFile = sizeof(filename);
  41.         ofn.lpstrFilter = L"All\0*.*\0Text\0*.TXT\0";
  42.         ofn.nFilterIndex = 1;
  43.         ofn.lpstrFileTitle = NULL;
  44.         ofn.nMaxFileTitle = 0;
  45.         ofn.lpstrInitialDir = NULL;
  46.         ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  47.  
  48.         return ofn;
  49.     }
  50.  
  51.     int MessageBox(LPCWSTR text, LPCWSTR caption = L"Alert", long type = MB_OK, HWND hWnd = hMainWindow)
  52.     {
  53.         return MessageBox(hWnd, text, caption, type);
  54.     }
  55.  
  56.     void SetAppState(LPCWSTR state = L"Ready")
  57.     {
  58.         SendMessage(hMainWindowStatusBar, SB_SETTEXT, 0, (LPARAM)state);
  59.     }
  60.  
  61.     void OnKeyDown(WPARAM virtualKeyCode)
  62.     {
  63.         wstring yptfk = L"You pressed the following key: \n";
  64.         int charCode = MapVirtualKey(virtualKeyCode, MAPVK_VK_TO_CHAR);
  65.         wchar_t key = (wchar_t)charCode;
  66.         wstring text = yptfk + L"Character: \"" + key + L"\"\nCharcode: " + to_wstring(charCode);
  67.         MessageBox(text.c_str());
  68.     }
  69.  
  70.     Size AdjustWindowSize(int width, int height)
  71.     {
  72.         RECT wr = { 0, 0, width, height };
  73.         AdjustWindowRect(&wr, windowStyle, FALSE);
  74.         return Size(wr.right - wr.left, wr.bottom - wr.top);
  75.     }
  76.  
  77.     void SetClientAreaSize(int width, int height, HWND hWnd = hMainWindow)
  78.     {
  79.         clientAreaWidth = width;
  80.         clientAreaHeight = height;
  81.         Size size = AdjustWindowSize(width, height);
  82.         MoveWindow(hWnd, 0, 0, size.Width, size.Height, TRUE);
  83.     }
  84.  
  85.     void LoadFile(LPCWSTR filename)
  86.     {
  87.         // Disabled due to malfunction.
  88.         // openImageFilename = filename;
  89.         // pOpenImage = Image::FromFile(filename);
  90.  
  91.         filename = L"C:/Users/vovan/Desktop/sample.bmp";
  92.         openImageFilename = filename;
  93.         pOpenImage = Image::FromFile(filename);
  94.         int width = pOpenImage->GetWidth();
  95.         int height = pOpenImage->GetHeight();
  96.         SetClientAreaSize(width, height);
  97.     }
  98.  
  99.     LPCWSTR ChooseFile()
  100.     {
  101.         OPENFILENAME ofn = CreateOFN();
  102.         GetOpenFileName(&ofn);
  103.         return ofn.lpstrFile;
  104.     }
  105.  
  106.     void Render(HDC hdc)
  107.     {
  108.         if (pOpenImage != nullptr)
  109.         {
  110.             Graphics gc(hdc);
  111.             Rect clientArea(0, 0, clientAreaWidth, clientAreaHeight);
  112.             gc.DrawImage(pOpenImage, clientArea);
  113.         }
  114.     }
  115.  
  116.     LRESULT CALLBACK OnWindowMessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  117.     {
  118.         int mbResult;
  119.         switch (message)
  120.         {
  121.             case WM_CLOSE:
  122.                 mbResult = MessageBox(L"Are you sure you want to exit?", L"Exit?", MB_YESNO | MB_ICONWARNING);
  123.                 if (mbResult == IDYES)
  124.                 {
  125.                     DestroyWindow(hWnd);
  126.                 }
  127.                 return 0;
  128.                 break;
  129.  
  130.             case WM_DESTROY:
  131.                 PostQuitMessage(0);
  132.                 return 0;
  133.                 break;
  134.  
  135.             case WM_KEYDOWN:
  136.                 OnKeyDown(wParam);
  137.                 break;
  138.  
  139.             case WM_SIZE:
  140.                 SendMessage(hMainWindowStatusBar, WM_SIZE, NULL, NULL);
  141.                 break;
  142.  
  143.             case WM_COMMAND:
  144.                 switch (LOWORD(wParam))
  145.                 {
  146.                     case ToolbarItem::File_Open:
  147.                         if (LPCWSTR filename = ChooseFile())
  148.                         {
  149.                             LoadFile(filename);
  150.                         }
  151.                         break;
  152.                 }
  153.                 break;
  154.  
  155.             case WM_PAINT:
  156.                 PAINTSTRUCT ps;
  157.                 HDC hdc = BeginPaint(hWnd, &ps);
  158.                 Render(hdc);
  159.                 EndPaint(hWnd, &ps);
  160.                 break;
  161.         }
  162.         return DefWindowProc(hWnd, message, wParam, lParam);
  163.     }
  164.  
  165.     HWND CreateStatusBar(HWND hParentWindow, int id, LPCWSTR text = L"Ready")
  166.     {
  167.         return CreateStatusWindow(WS_CHILD | WS_VISIBLE, text, hParentWindow, id);
  168.     }
  169.  
  170.     HMENU CreateMainWindowToolbar()
  171.     {
  172.         HMENU hToolbar = CreateMenu();
  173.  
  174.         HMENU hFileMenu = CreatePopupMenu();
  175.         AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_Open, L"Open");
  176.         AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_New, L"New");
  177.         AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_Save, L"Save");
  178.         AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_Close, L"Close");
  179.  
  180.         AppendMenu(hToolbar, MF_POPUP, (UINT_PTR)hFileMenu, L"File");
  181.  
  182.         return hToolbar;
  183.     }
  184.  
  185.     void WriteFileA(HANDLE hFile, LPCSTR data)
  186.     {
  187.         WriteFile(hFile, data, strlen(data), 0, NULL);
  188.     }
  189.  
  190.     void Log(LPCSTR data)
  191.     {
  192.         if (debug)
  193.         {
  194.             WriteFileA(hConsole, data);
  195.             WriteFileA(hConsole, "\n");
  196.         }
  197.     }
  198.  
  199.     void Log(int data)
  200.     {
  201.         Log(to_string(data).c_str());
  202.     }
  203.  
  204.     void LoadConsole()
  205.     {
  206.         AllocConsole();
  207.         hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  208.     }
  209.  
  210.     int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  211.     {
  212.         if (debug)
  213.         {
  214.             LoadConsole();
  215.         }
  216.  
  217.         WNDCLASSEX wc;
  218.         ZeroMemory(&wc, sizeof(wc));
  219.  
  220.         wc.cbSize = sizeof(WNDCLASSEX);
  221.         wc.style = CS_HREDRAW | CS_VREDRAW;
  222.         wc.lpfnWndProc = OnWindowMessage;
  223.         wc.hInstance = hInstance;
  224.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  225.         wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  226.         wc.lpszClassName = L"WC_Default";
  227.  
  228.         RegisterClassEx(&wc);
  229.  
  230.         Size sz = AdjustWindowSize(clientAreaWidth, clientAreaHeight);
  231.         HMENU hMainWindowMenu = CreateMainWindowToolbar();
  232.  
  233.         GdiplusStartupInput gdipsi;
  234.         ULONG_PTR gdipToken;
  235.         GdiplusStartup(&gdipToken, &gdipsi, NULL);
  236.  
  237.         hMainWindow = CreateWindowEx
  238.         (
  239.             NULL,
  240.             L"WC_Default",
  241.             L"D3DTest1 Main Window",
  242.             windowStyle,
  243.             300,
  244.             300,
  245.             sz.Width,
  246.             sz.Height,
  247.             NULL,
  248.             hMainWindowMenu,
  249.             hInstance,
  250.             NULL
  251.         );
  252.  
  253.         hMainWindowStatusBar = CreateStatusBar(hMainWindow, 0);
  254.         ShowWindow(hMainWindow, nShowCmd);
  255.  
  256.         MSG message;
  257.         while (GetMessage(&message, NULL, 0, 0))
  258.         {
  259.             TranslateMessage(&message);
  260.             DispatchMessage(&message);
  261.         }
  262.  
  263.         GdiplusShutdown(gdipToken);
  264.  
  265.         return 0;
  266.     }
  267. }
  268.  
  269. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  270. {
  271.     D3DTest1::WinMain(hInstance, hPrevInstance, lpCmdLine, nShowCmd);
  272. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement