Guest User

Untitled

a guest
Nov 14th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #include <windows.h>
  2. #include <gdiplus.h>
  3.  
  4. LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM param, LPARAM lparam);
  5. void draw(HDC hdc);
  6.  
  7. int WINAPI WinMain(HINSTANCE currentInstance, HINSTANCE previousInstance, PSTR cmdLine, INT cmdCount) {
  8. // Initialize GDI+
  9. Gdiplus::GdiplusStartupInput gdiplusStartupInput;
  10. ULONG_PTR gdiplusToken;
  11. Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
  12.  
  13. // Register the window class
  14. const char *CLASS_NAME = "myWin32WindowClass";
  15. WNDCLASS wc{};
  16. wc.hInstance = currentInstance;
  17. wc.lpszClassName = CLASS_NAME;
  18. wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
  19. wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  20. wc.lpfnWndProc = WindowProcessMessages;
  21. RegisterClass(&wc);
  22.  
  23. // Create the window
  24. CreateWindow(CLASS_NAME, "Win32 Drawing with GDI+ Tutorial",
  25. WS_OVERLAPPEDWINDOW | WS_VISIBLE, // Window style
  26. CW_USEDEFAULT, CW_USEDEFAULT, // Window initial position
  27. 800, 600, // Window size
  28. nullptr, nullptr, nullptr, nullptr);
  29.  
  30. // Window loop
  31. MSG msg{};
  32. while (GetMessage(&msg, nullptr, 0, 0)) {
  33. TranslateMessage(&msg);
  34. DispatchMessage(&msg);
  35. }
  36.  
  37. Gdiplus::GdiplusShutdown(gdiplusToken);
  38. return 0;
  39. }
  40.  
  41. LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM param, LPARAM lparam) {
  42. HDC hdc;
  43. PAINTSTRUCT ps;
  44.  
  45. switch (msg) {
  46. case WM_PAINT:
  47. hdc = BeginPaint(hwnd, &ps);
  48. draw(hdc);
  49. EndPaint(hwnd, &ps);
  50. return 0;
  51. case WM_DESTROY:
  52. PostQuitMessage(0);
  53. return 0;
  54. default:
  55. return DefWindowProc(hwnd, msg, param, lparam);
  56. }
  57. }
  58.  
  59. void draw(HDC hdc) {
  60. Gdiplus::Graphics gf(hdc);
  61. Gdiplus::Pen pen(Gdiplus::Color(255, 255, 0, 0)); // For lines, rectangles and curves
  62. Gdiplus::SolidBrush brush(Gdiplus::Color(255, 0, 255, 0)); // For filled shapes
  63.  
  64. gf.DrawLine(&pen, 0, 0, 500, 500);
  65. gf.FillRectangle(&brush, 320, 200, 100, 100);
  66. gf.DrawRectangle(&pen, 600, 400, 100, 150);
  67.  
  68. Gdiplus::Bitmap bmp(L"water_small.png");
  69. gf.DrawImage(&bmp, 430, 10);
  70.  
  71. gf.FillEllipse(&brush, 50, 400, 200, 100);
  72. }
Add Comment
Please, Sign In to add comment