Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <windowsx.h>
- #include <CommCtrl.h>
- #include <gdiplus.h>
- #include <string>
- using namespace std;
- using namespace Gdiplus;
- namespace D3DTest1
- {
- HINSTANCE hCurrentInstance;
- BOOL isFileOpen;
- LPCWSTR openImageFilename;
- Image* pOpenImage;
- HWND hMainWindow;
- HWND hMainWindowStatusBar;
- int clientAreaWidth = 800;
- int clientAreaHeight = 600;
- UINT windowStyle = WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU;
- BOOL debug = TRUE;
- HANDLE hConsole;
- BOOL isDrawing = FALSE;
- Point prevMousePos;
- Point curMousePos;
- Color color = Color::Black;
- enum ToolbarItem
- {
- File_New = 0,
- File_Open = 1,
- File_Save = 2,
- File_Close = 3,
- Tool_Line = 4,
- Tool_Brush = 5,
- Tool_ColorPicker = 6
- };
- enum Tool
- {
- Brush = 0,
- Line = 1
- };
- Tool tool = Tool::Line;
- OPENFILENAME CreateOFN(HWND hOwnerWindow = hMainWindow)
- {
- OPENFILENAME ofn;
- WCHAR filename[MAX_PATH] = L"";
- 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;
- }
- CHOOSECOLOR CreateCC(HWND hOwnerWindow = hMainWindow)
- {
- CHOOSECOLOR cc;
- COLORREF cr[16];
- ZeroMemory(&cc, sizeof(CHOOSECOLOR));
- cc.lStructSize = sizeof(CHOOSECOLOR);
- cc.hwndOwner = hOwnerWindow;
- cc.rgbResult = RGB(255, 255, 255);
- cc.lpCustColors = cr;
- cc.Flags = CC_RGBINIT | CC_FULLOPEN;
- return cc;
- }
- int MessageBox(LPCWSTR text, LPCWSTR caption = L"Alert", long type = MB_OK, HWND hWnd = hMainWindow)
- {
- return MessageBox(hWnd, text, caption, type);
- }
- void SetAppState(LPCWSTR state = L"Ready")
- {
- SendMessage(hMainWindowStatusBar, SB_SETTEXT, 0, (LPARAM)state);
- }
- void OnKeyDown(WPARAM virtualKeyCode)
- {
- wstring yptfk = L"You pressed the following key: \n";
- int charCode = MapVirtualKey(virtualKeyCode, MAPVK_VK_TO_CHAR);
- wchar_t key = (wchar_t)charCode;
- wstring text = yptfk + L"Character: \"" + key + L"\"\nCharcode: " + to_wstring(charCode);
- MessageBox(text.c_str());
- }
- Size AdjustWindowSize(int width, int height)
- {
- RECT wr = { 0, 0, width, height };
- AdjustWindowRect(&wr, windowStyle, FALSE);
- return Size(wr.right - wr.left, wr.bottom - wr.top);
- }
- void SetClientAreaSize(int width, int height, HWND hWnd = hMainWindow)
- {
- clientAreaWidth = width;
- clientAreaHeight = height;
- Size size = AdjustWindowSize(width, height);
- MoveWindow(hWnd, 0, 0, size.Width, size.Height, TRUE);
- }
- // Does not work.
- void SetWindowBackgroundImage(LPCWSTR filename, HWND hWnd = hMainWindow)
- {
- HBITMAP hImage = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
- SendMessage(hWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage);
- }
- void RefreshWindow(HWND hWnd = hMainWindow)
- {
- if (pOpenImage != nullptr)
- {
- Graphics gc(GetDC(hWnd));
- Rect clientArea(0, 0, clientAreaWidth, clientAreaHeight);
- gc.DrawImage(pOpenImage, clientArea);
- }
- }
- void SaveFile(LPWSTR filename)
- {
- CLSID clsid;
- pOpenImage->Save(filename, &clsid);
- }
- void LoadFile(LPCWSTR filename)
- {
- // Disabled due to malfunction.
- // openImageFilename = filename;
- // pOpenImage = Image::FromFile(filename);
- filename = L"C:/Users/vovan/Desktop/sample.bmp";
- isFileOpen = TRUE;
- openImageFilename = filename;
- pOpenImage = Image::FromFile(filename);
- int width = pOpenImage->GetWidth();
- int height = pOpenImage->GetHeight();
- SetClientAreaSize(width, height);
- RefreshWindow();
- SetAppState(L"Loaded image");
- }
- void WriteFileA(HANDLE hFile, LPCSTR data)
- {
- WriteFile(hFile, data, strlen(data), 0, NULL);
- }
- void Log(LPCSTR data)
- {
- if (debug)
- {
- WriteFileA(hConsole, data);
- WriteFileA(hConsole, "\n");
- }
- }
- void Log(int data)
- {
- Log(to_string(data).c_str());
- }
- LPCWSTR GetFilenameToOpen()
- {
- OPENFILENAME ofn = CreateOFN();
- GetOpenFileName(&ofn);
- return ofn.lpstrFile;
- }
- LPWSTR GetFilenameToSave()
- {
- OPENFILENAME ofn = CreateOFN();
- GetSaveFileName(&ofn);
- MessageBox(ofn.lpstrFile);
- char* src = "";
- wcstombs(src, L"FAGGOT", 100);
- Log(src);
- return ofn.lpstrFile;
- }
- void DrawLine(Point pt1 = prevMousePos, Point pt2 = curMousePos)
- {
- Graphics gc(pOpenImage);
- Pen pen(color, 10);
- gc.DrawLine(&pen, pt1, pt2);
- RefreshWindow();
- }
- Point GetMousePos(LPARAM lParam)
- {
- Point mousePos;
- mousePos.X = GET_X_LPARAM(lParam);
- mousePos.Y = GET_Y_LPARAM(lParam);
- return mousePos;
- }
- void FlipMousePositions(LPARAM lParam)
- {
- prevMousePos = curMousePos;
- curMousePos = GetMousePos(lParam);
- }
- void LogMousePos(Point pos)
- {
- Log("X:");
- Log(pos.X);
- Log("Y:");
- Log(pos.Y);
- }
- void PickAColor()
- {
- CHOOSECOLOR cc = CreateCC();
- ChooseColor(&cc);
- COLORREF cr = cc.rgbResult;
- BYTE R = GetRValue(cr);
- BYTE G = GetGValue(cr);
- BYTE B = GetBValue(cr);
- color = Color(R, G, B);
- }
- 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_PAINT:
- RefreshWindow();
- break;
- case WM_COMMAND:
- switch (LOWORD(wParam))
- {
- case ToolbarItem::File_Open:
- if (LPCWSTR filename = GetFilenameToOpen())
- {
- LoadFile(filename);
- }
- break;
- case ToolbarItem::File_Save:
- {
- if (pOpenImage != nullptr)
- {
- if (LPWSTR filename = GetFilenameToSave())
- {
- //Log(filename);
- SaveFile(filename);
- }
- }
- }
- case ToolbarItem::Tool_Brush:
- tool = Tool::Brush;
- break;
- case ToolbarItem::Tool_Line:
- tool = Tool::Line;
- break;
- case ToolbarItem::Tool_ColorPicker:
- PickAColor();
- break;
- }
- break;
- case WM_MOUSEMOVE:
- if (isDrawing)
- {
- switch (tool)
- {
- case Tool::Brush:
- FlipMousePositions(lParam);
- DrawLine();
- break;
- }
- }
- break;
- case WM_LBUTTONDOWN:
- if (TRUE)
- {
- switch (tool)
- {
- case Tool::Brush:
- isDrawing = TRUE;
- curMousePos = GetMousePos(lParam);
- break;
- case Tool::Line:
- isDrawing = TRUE;
- curMousePos = GetMousePos(lParam);
- break;
- }
- }
- break;
- case WM_LBUTTONUP:
- switch (tool)
- {
- case Tool::Brush:
- isDrawing = FALSE;
- break;
- case Tool::Line:
- isDrawing = FALSE;
- FlipMousePositions(lParam);
- DrawLine();
- break;
- }
- 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");
- HMENU hToolMenu = CreatePopupMenu();
- AppendMenu(hToolMenu, MF_STRING, ToolbarItem::Tool_Line, L"Line");
- AppendMenu(hToolMenu, MF_STRING, ToolbarItem::Tool_Brush, L"Brush");
- AppendMenu(hToolMenu, MF_STRING, ToolbarItem::Tool_ColorPicker, L"Color Picker");
- AppendMenu(hToolbar, MF_POPUP, (UINT_PTR)hFileMenu, L"File");
- AppendMenu(hToolbar, MF_POPUP, (UINT_PTR)hToolMenu, L"Tool");
- return hToolbar;
- }
- void LoadConsole()
- {
- AllocConsole();
- hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- }
- int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
- {
- if (debug)
- {
- LoadConsole();
- }
- 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);
- Size sz = AdjustWindowSize(clientAreaWidth, clientAreaHeight);
- HMENU hMainWindowMenu = CreateMainWindowToolbar();
- GdiplusStartupInput gdipsi;
- ULONG_PTR gdipToken;
- GdiplusStartup(&gdipToken, &gdipsi, NULL);
- hMainWindow = CreateWindowEx
- (
- NULL,
- L"WC_Default",
- L"D3DTest1 Main Window",
- windowStyle,
- 300,
- 300,
- sz.Width,
- sz.Height,
- NULL,
- hMainWindowMenu,
- hInstance,
- NULL
- );
- hMainWindowStatusBar = CreateStatusBar(hMainWindow, 0);
- ShowWindow(hMainWindow, nShowCmd);
- MSG message;
- while (GetMessage(&message, NULL, 0, 0))
- {
- TranslateMessage(&message);
- DispatchMessage(&message);
- }
- GdiplusShutdown(gdipToken);
- 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