Advertisement
vovan333

Win32API Shitcode

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