CodePlsRun

Win32 CreatePen NULL error

Sep 28th, 2020
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  4. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
  5. {
  6.     static TCHAR szAppName[] = TEXT("Random Walk");
  7.     HWND hwnd;
  8.     MSG msg;
  9.     WNDCLASS wndclass;
  10.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  11.     wndclass.lpfnWndProc = WndProc;
  12.     wndclass.cbClsExtra = 0;
  13.     wndclass.cbWndExtra = 0;
  14.     wndclass.hInstance = hInstance;
  15.     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  16.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  17.     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  18.     wndclass.lpszMenuName = NULL;
  19.     wndclass.lpszClassName = szAppName;
  20.     if (!RegisterClass(&wndclass))
  21.     {
  22.         MessageBox(NULL, TEXT("Program requires Windows NT !"), szAppName, MB_ICONERROR);
  23.         return 0;
  24.     }
  25.     hwnd = CreateWindow(szAppName, TEXT("Random Walk"),
  26.                         WS_OVERLAPPEDWINDOW,
  27.                         CW_USEDEFAULT, CW_USEDEFAULT,
  28.                         CW_USEDEFAULT, CW_USEDEFAULT,
  29.                         NULL, NULL, hInstance, NULL);
  30.     ShowWindow(hwnd, iCmdShow);
  31.     UpdateWindow(hwnd);
  32.     while (GetMessage(&msg, NULL, 0, 0))
  33.     {
  34.         TranslateMessage(&msg);
  35.         DispatchMessage(&msg);
  36.     }
  37.     return msg.wParam;
  38. }
  39. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  40. {
  41.     static int cxClient, cyClient;
  42.     HDC hdc;
  43.     HPEN hPen;
  44.     PAINTSTRUCT ps;
  45.     switch (message)
  46.     {
  47.     case WM_SIZE:
  48.         cxClient = LOWORD(lParam);
  49.         cyClient = HIWORD(lParam);
  50.         return 0;
  51.  
  52.     case WM_PAINT:
  53.         printf("%d\t%d\n", cxClient, cyClient); //Printing the resolution
  54.         hdc = BeginPaint(hwnd, &ps);
  55.         MoveToEx(hdc, 0, cyClient / 2, NULL);
  56.         int count = 0;
  57.  
  58.         //Plotting 256 lines each of length (cxClient/256) with different color
  59.         for (int i = 0; i < cxClient; i += cxClient / 256)
  60.         {
  61.             if ((hPen = CreatePen(PS_SOLID, 10, RGB(count, 255, 255))) == NULL)
  62.             {
  63.                 DWORD lasterror;
  64.                 lasterror = GetLastError();
  65.                 printf("We got a NULL Value and last error code was %lu\n", lasterror);
  66.                 break;
  67.             }
  68.             else
  69.                 SelectObject(hdc, hPen);
  70.  
  71.             count += 1; // For uniform gradient across width
  72.             LineTo(hdc, i, cyClient / 2);
  73.         }
  74.         DeleteObject(hPen);
  75.         EndPaint(hwnd, &ps);
  76.         return 0;
  77.  
  78.     case WM_DESTROY:
  79.         PostQuitMessage(0);
  80.         return 0;
  81.     }
  82.     return DefWindowProc(hwnd, message, wParam, lParam);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment