Advertisement
bkit4s0

Untitled

Sep 26th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. // Beginning Game Programming
  2. // Chapter 3
  3. // WindowTest program
  4. //header files to include
  5. #include <windows.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. //application title
  9. #define APPTITLE "Hello World"
  10. //function prototypes (forward declarations)
  11. BOOL InitInstance(HINSTANCE,int);
  12. ATOM MyRegisterClass(HINSTANCE);
  13. LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
  14. //the window event callback function
  15. LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
  16.     PAINTSTRUCT ps;
  17.     HDC hdc;
  18.     char *szHello = "Hello World!";
  19.     RECT rt;
  20.     int x, y, n;
  21.     COLORREF c;
  22.     switch (message) {
  23.         case WM_PAINT:
  24.             //get the dimensions of the window
  25.             GetClientRect(hWnd, &rt);
  26.             //start drawing on device context
  27.             hdc = BeginPaint(hWnd, &ps);
  28.             //draw some text
  29.             DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
  30.             //draw 1000 random pixels
  31.             for (n=0; n<3000; n++)
  32.             {
  33.             x = rand() % (rt.right - rt.left);
  34.             y = rand() % (rt.bottom - rt.top);
  35.             c = RGB(rand()%256, rand()%256, rand()%256);
  36.             SetPixel(hdc, x, y, c);
  37.             }
  38.             //stop drawing
  39.             EndPaint(hWnd, &ps);
  40.             break;
  41.         case WM_DESTROY:
  42.             PostQuitMessage(0);
  43.             break;
  44.     }
  45.     return DefWindowProc(hWnd, message, wParam, lParam);
  46. }
  47. //helper function to set up the window properties
  48. ATOM MyRegisterClass(HINSTANCE hInstance) {
  49.     //create the window class structure
  50.     WNDCLASSEX wc;
  51.     wc.cbSize = sizeof(WNDCLASSEX);
  52.     //fill the struct with info
  53.     wc.style = CS_HREDRAW | CS_VREDRAW;
  54.     wc.lpfnWndProc = (WNDPROC)WinProc;
  55.     wc.cbClsExtra = 0;
  56.     wc.cbWndExtra = 0;
  57.     wc.hInstance = hInstance;
  58.     wc.hIcon = NULL;
  59.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  60.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  61.     wc.lpszMenuName = NULL;
  62.     wc.lpszClassName = APPTITLE;
  63.     wc.hIconSm = NULL;
  64.     //set up the window with the class info
  65.     return RegisterClassEx(&wc);
  66. }
  67. //helper function to create the window and refresh it
  68. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {
  69.     HWND hWnd;
  70.     //create a new window
  71.     hWnd = CreateWindow(
  72.     APPTITLE, //window class
  73.     APPTITLE, //title bar
  74.     WS_OVERLAPPEDWINDOW, //window style
  75.     CW_USEDEFAULT, //x position of window
  76.     CW_USEDEFAULT, //y position of window
  77.     500, //width of the window
  78.     400, //height of the window
  79.     NULL, //parent window
  80.     NULL, //menu
  81.     hInstance, //application instance
  82.     NULL); //window parameters
  83.     //was there an error creating the window?
  84.     if (!hWnd)
  85.         return FALSE;
  86.     //display the window
  87.     ShowWindow(hWnd, nCmdShow);
  88.     UpdateWindow(hWnd);
  89.     return TRUE;
  90. }
  91. //entry point for a Windows program
  92. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  93.     // declare variables
  94.     MSG msg;
  95.     // register the class
  96.     MyRegisterClass(hInstance);
  97.     // initialize application
  98.     if (!InitInstance (hInstance, nCmdShow))
  99.         return FALSE;
  100.     //set random number seed
  101.     srand(time(NULL));
  102.     //main message loop
  103.     while (GetMessage(&msg, NULL, 0, 0)) {
  104.         TranslateMessage(&msg);
  105.         DispatchMessage(&msg);
  106.     }
  107.     return msg.wParam;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement