Advertisement
vovan333

Untitled

Jan 2nd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2. #include <windowsx.h>
  3. #include <CommCtrl.h>
  4. #include <gdiplus.h>
  5. #include <string>
  6.  
  7. using namespace std;
  8. using namespace Gdiplus;
  9.  
  10. namespace D3DTest1
  11. {
  12.     HINSTANCE hCurrentInstance;
  13.     LPCWSTR openImageFilename;
  14.     Image* pOpenImage;
  15.     HWND hMainWindow;
  16.     HWND hMainWindowStatusBar;
  17.     int clientAreaWidth = 800;
  18.     int clientAreaHeight = 600;
  19.     UINT windowStyle = WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU;
  20.     BOOL debug = TRUE;
  21.     HANDLE hConsole;
  22.     BOOL isDrawing = FALSE;
  23.     Point prevMousePos;
  24.     Point curMousePos;
  25.     Color color = Color::Black;
  26.  
  27.     enum ToolbarItem
  28.     {
  29.         File_New = 0,
  30.         File_Open = 1,
  31.         File_Save = 2,
  32.         File_Close = 3,
  33.  
  34.         Tool_Line = 4,
  35.         Tool_Brush = 5,
  36.         Tool_ColorPicker = 6
  37.     };
  38.  
  39.     enum Tool
  40.     {
  41.         Brush = 0,
  42.         Line = 1
  43.     };
  44.    
  45.     Tool tool = Tool::Line;
  46.  
  47.     OPENFILENAME CreateOFN(HWND hOwnerWindow = hMainWindow)
  48.     {
  49.         OPENFILENAME ofn;
  50.         WCHAR filename[MAX_PATH];
  51.         ZeroMemory(&ofn, sizeof(ofn));
  52.  
  53.         ofn.lStructSize = sizeof(ofn);
  54.         ofn.hwndOwner = hOwnerWindow;
  55.         ofn.lpstrFile = filename;
  56.         ofn.lpstrFile[0] = '\0';
  57.         ofn.nMaxFile = sizeof(filename);
  58.         ofn.lpstrFilter = L"All\0*.*\0Text\0*.TXT\0";
  59.         ofn.nFilterIndex = 1;
  60.         ofn.lpstrFileTitle = NULL;
  61.         ofn.nMaxFileTitle = 0;
  62.         ofn.lpstrInitialDir = NULL;
  63.         ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  64.  
  65.         return ofn;
  66.     }
  67.  
  68.     CHOOSECOLOR CreateCC(HWND hOwnerWindow = hMainWindow)
  69.     {
  70.         CHOOSECOLOR cc;
  71.         COLORREF cr[16];
  72.         ZeroMemory(&cc, sizeof(CHOOSECOLOR));
  73.  
  74.         cc.lStructSize = sizeof(CHOOSECOLOR);
  75.         cc.hwndOwner = hOwnerWindow;
  76.         cc.rgbResult = RGB(255, 255, 255);
  77.         cc.lpCustColors = cr;
  78.         cc.Flags = CC_RGBINIT | CC_FULLOPEN;
  79.  
  80.         return cc;
  81.     }
  82.  
  83.     int MessageBox(LPCWSTR text, LPCWSTR caption = L"Alert", long type = MB_OK, HWND hWnd = hMainWindow)
  84.     {
  85.         return MessageBox(hWnd, text, caption, type);
  86.     }
  87.  
  88.     void SetAppState(LPCWSTR state = L"Ready")
  89.     {
  90.         SendMessage(hMainWindowStatusBar, SB_SETTEXT, 0, (LPARAM)state);
  91.     }
  92.  
  93.     void OnKeyDown(WPARAM virtualKeyCode)
  94.     {
  95.         wstring yptfk = L"You pressed the following key: \n";
  96.         int charCode = MapVirtualKey(virtualKeyCode, MAPVK_VK_TO_CHAR);
  97.         wchar_t key = (wchar_t)charCode;
  98.         wstring text = yptfk + L"Character: \"" + key + L"\"\nCharcode: " + to_wstring(charCode);
  99.         MessageBox(text.c_str());
  100.     }
  101.  
  102.     Size AdjustWindowSize(int width, int height)
  103.     {
  104.         RECT wr = { 0, 0, width, height };
  105.         AdjustWindowRect(&wr, windowStyle, FALSE);
  106.         return Size(wr.right - wr.left, wr.bottom - wr.top);
  107.     }
  108.  
  109.     void SetClientAreaSize(int width, int height, HWND hWnd = hMainWindow)
  110.     {
  111.         clientAreaWidth = width;
  112.         clientAreaHeight = height;
  113.         Size size = AdjustWindowSize(width, height);
  114.         MoveWindow(hWnd, 0, 0, size.Width, size.Height, TRUE);
  115.     }
  116.  
  117.     // Does not work.
  118.  
  119.     void SetWindowBackgroundImage(LPCWSTR filename, HWND hWnd = hMainWindow)
  120.     {
  121.         HBITMAP hImage = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  122.         SendMessage(hWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage);
  123.     }
  124.  
  125.     void RefreshWindow(HWND hWnd = hMainWindow)
  126.     {
  127.         if (pOpenImage != nullptr)
  128.         {
  129.             Graphics gc(GetDC(hWnd));
  130.             Rect clientArea(0, 0, clientAreaWidth, clientAreaHeight);
  131.             gc.DrawImage(pOpenImage, clientArea);
  132.         }
  133.     }
  134.  
  135.     void LoadFile(LPCWSTR filename)
  136.     {
  137.         // Disabled due to malfunction.
  138.         // openImageFilename = filename;
  139.         // pOpenImage = Image::FromFile(filename);
  140.  
  141.         filename = L"C:/Users/vovan/Desktop/sample.bmp";
  142.         openImageFilename = filename;
  143.         pOpenImage = Image::FromFile(filename);
  144.         int width = pOpenImage->GetWidth();
  145.         int height = pOpenImage->GetHeight();
  146.         SetClientAreaSize(width, height);
  147.         RefreshWindow();
  148.         SetAppState(L"Loaded image");
  149.     }
  150.  
  151.     LPCWSTR ChooseFile()
  152.     {
  153.         OPENFILENAME ofn = CreateOFN();
  154.         GetOpenFileName(&ofn);
  155.         return ofn.lpstrFile;
  156.     }
  157.    
  158.     void WriteFileA(HANDLE hFile, LPCSTR data)
  159.     {
  160.         WriteFile(hFile, data, strlen(data), 0, NULL);
  161.     }
  162.  
  163.     void Log(LPCSTR data)
  164.     {
  165.         if (debug)
  166.         {
  167.             WriteFileA(hConsole, data);
  168.             WriteFileA(hConsole, "\n");
  169.         }
  170.     }
  171.  
  172.     void Log(int data)
  173.     {
  174.         Log(to_string(data).c_str());
  175.     }
  176.  
  177.     void DrawLine(Point pt1 = prevMousePos, Point pt2 = curMousePos)
  178.     {
  179.         Graphics gc(pOpenImage);
  180.         Pen pen(color, 10);
  181.         gc.DrawLine(&pen, pt1, pt2);
  182.         RefreshWindow();
  183.     }
  184.  
  185.     Point GetMousePos(LPARAM lParam)
  186.     {
  187.         Point mousePos;
  188.         mousePos.X = GET_X_LPARAM(lParam);
  189.         mousePos.Y = GET_Y_LPARAM(lParam);
  190.         return mousePos;
  191.     }
  192.  
  193.     void FlipMousePositions(LPARAM lParam)
  194.     {
  195.         prevMousePos = curMousePos;
  196.         curMousePos = GetMousePos(lParam);
  197.     }
  198.  
  199.     void LogMousePos(Point pos)
  200.     {
  201.         Log("X:");
  202.         Log(pos.X);
  203.         Log("Y:");
  204.         Log(pos.Y);
  205.     }
  206.  
  207.     void PickAColor()
  208.     {
  209.         CHOOSECOLOR cc = CreateCC();
  210.         ChooseColor(&cc);
  211.         COLORREF cr = cc.rgbResult;
  212.         BYTE R = GetRValue(cr);
  213.         BYTE G = GetGValue(cr);
  214.         BYTE B = GetBValue(cr);
  215.         color = Color(R, G, B);
  216.     }
  217.  
  218.     LRESULT CALLBACK OnWindowMessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  219.     {
  220.         int mbResult;
  221.         switch (message)
  222.         {
  223.             case WM_CLOSE:
  224.                 mbResult = MessageBox(L"Are you sure you want to exit?", L"Exit?", MB_YESNO | MB_ICONWARNING);
  225.                 if (mbResult == IDYES)
  226.                 {
  227.                     DestroyWindow(hWnd);
  228.                 }
  229.                 return 0;
  230.             break;
  231.  
  232.             case WM_DESTROY:
  233.                 PostQuitMessage(0);
  234.                 return 0;
  235.             break;
  236.  
  237.             case WM_KEYDOWN:
  238.                 OnKeyDown(wParam);
  239.             break;
  240.  
  241.             case WM_SIZE:
  242.                 SendMessage(hMainWindowStatusBar, WM_SIZE, NULL, NULL);
  243.             break;
  244.  
  245.             case WM_PAINT:
  246.                 RefreshWindow();
  247.             break;
  248.  
  249.             case WM_COMMAND:
  250.                 switch (LOWORD(wParam))
  251.                 {
  252.                     case ToolbarItem::File_Open:
  253.                         if (LPCWSTR filename = ChooseFile())
  254.                         {
  255.                             LoadFile(filename);
  256.                         }
  257.                     break;
  258.  
  259.                     case ToolbarItem::Tool_Brush:
  260.                         tool = Tool::Brush;
  261.                     break;
  262.  
  263.                     case ToolbarItem::Tool_Line:
  264.                         tool = Tool::Line;
  265.                     break;
  266.  
  267.                     case ToolbarItem::Tool_ColorPicker:
  268.                         PickAColor();
  269.                     break;
  270.                 }
  271.             break;
  272.            
  273.             case WM_MOUSEMOVE:
  274.                 if (isDrawing)
  275.                 {
  276.                     switch (tool)
  277.                     {
  278.                         case Tool::Brush:
  279.                             FlipMousePositions(lParam);
  280.                             DrawLine();
  281.                         break;
  282.                     }
  283.                 }
  284.             break;
  285.  
  286.             case WM_LBUTTONDOWN:
  287.                 if (TRUE)
  288.                 {
  289.                     switch (tool)
  290.                     {
  291.                         case Tool::Brush:
  292.                             isDrawing = TRUE;
  293.                             curMousePos = GetMousePos(lParam);
  294.                         break;
  295.  
  296.                         case Tool::Line:
  297.                             isDrawing = TRUE;
  298.                             curMousePos = GetMousePos(lParam);
  299.                         break;
  300.                     }
  301.                 }
  302.             break;
  303.  
  304.             case WM_LBUTTONUP:
  305.                 switch (tool)
  306.                 {
  307.                     case Tool::Brush:
  308.                         isDrawing = FALSE;
  309.                     break;
  310.  
  311.                     case Tool::Line:
  312.                         isDrawing = FALSE;
  313.                         FlipMousePositions(lParam);
  314.                         DrawLine();
  315.                     break;
  316.                 }
  317.             break;
  318.         }
  319.         return DefWindowProc(hWnd, message, wParam, lParam);
  320.     }
  321.  
  322.     HWND CreateStatusBar(HWND hParentWindow, int id, LPCWSTR text = L"Ready")
  323.     {
  324.         return CreateStatusWindow(WS_CHILD | WS_VISIBLE, text, hParentWindow, id);
  325.     }
  326.  
  327.     HMENU CreateMainWindowToolbar()
  328.     {
  329.         HMENU hToolbar = CreateMenu();
  330.  
  331.         HMENU hFileMenu = CreatePopupMenu();
  332.         AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_Open, L"Open");
  333.         AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_New, L"New");
  334.         AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_Save, L"Save");
  335.         AppendMenu(hFileMenu, MF_STRING, ToolbarItem::File_Close, L"Close");
  336.  
  337.         HMENU hToolMenu = CreatePopupMenu();
  338.         AppendMenu(hToolMenu, MF_STRING, ToolbarItem::Tool_Line, L"Line");
  339.         AppendMenu(hToolMenu, MF_STRING, ToolbarItem::Tool_Brush, L"Brush");
  340.         AppendMenu(hToolMenu, MF_STRING, ToolbarItem::Tool_ColorPicker, L"Color Picker");
  341.        
  342.         AppendMenu(hToolbar, MF_POPUP, (UINT_PTR)hFileMenu, L"File");
  343.         AppendMenu(hToolbar, MF_POPUP, (UINT_PTR)hToolMenu, L"Tool");
  344.  
  345.         return hToolbar;
  346.     }
  347.  
  348.     void LoadConsole()
  349.     {
  350.         AllocConsole();
  351.         hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  352.     }
  353.  
  354.     int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  355.     {
  356.         if (debug)
  357.         {
  358.             LoadConsole();
  359.         }
  360.  
  361.         WNDCLASSEX wc;
  362.         ZeroMemory(&wc, sizeof(wc));
  363.  
  364.         wc.cbSize = sizeof(WNDCLASSEX);
  365.         wc.style = CS_HREDRAW | CS_VREDRAW;
  366.         wc.lpfnWndProc = OnWindowMessage;
  367.         wc.hInstance = hInstance;
  368.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  369.         wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  370.         wc.lpszClassName = L"WC_Default";
  371.  
  372.         RegisterClassEx(&wc);
  373.  
  374.         Size sz = AdjustWindowSize(clientAreaWidth, clientAreaHeight);
  375.         HMENU hMainWindowMenu = CreateMainWindowToolbar();
  376.  
  377.         GdiplusStartupInput gdipsi;
  378.         ULONG_PTR gdipToken;
  379.         GdiplusStartup(&gdipToken, &gdipsi, NULL);
  380.  
  381.         hMainWindow = CreateWindowEx
  382.         (
  383.             NULL,
  384.             L"WC_Default",
  385.             L"D3DTest1 Main Window",
  386.             windowStyle,
  387.             300,
  388.             300,
  389.             sz.Width,
  390.             sz.Height,
  391.             NULL,
  392.             hMainWindowMenu,
  393.             hInstance,
  394.             NULL
  395.         );
  396.  
  397.         hMainWindowStatusBar = CreateStatusBar(hMainWindow, 0);
  398.         ShowWindow(hMainWindow, nShowCmd);
  399.  
  400.         MSG message;
  401.         while (GetMessage(&message, NULL, 0, 0))
  402.         {
  403.             TranslateMessage(&message);
  404.             DispatchMessage(&message);
  405.         }
  406.  
  407.         GdiplusShutdown(gdipToken);
  408.  
  409.         return 0;
  410.     }
  411. }
  412.  
  413. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  414. {
  415.     D3DTest1::WinMain(hInstance, hPrevInstance, lpCmdLine, nShowCmd);
  416. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement