Ember

CreateWindow

Dec 26th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1.     // Application initialization ---------------------------------------------------------
  2.     bool Engine::CreateWindow(Int screenWidth, Int screenHeight, bool fullscreen)
  3.     {
  4.         WNDCLASSEX wc;
  5.         DEVMODE dmScreenSettings;
  6.         Int posX, posY;
  7.         bool result;
  8.  
  9.         // Initialize the Windows API (and create a window)
  10.  
  11.         // Set display settings
  12.         mScreenWidth = screenWidth;
  13.         mScreenHeight = screenHeight;
  14.         mFullscreen = fullscreen;
  15.  
  16.         // Get an external pointer to this object
  17.         ApplicationHandle = this;
  18.  
  19.         // Get the instance of this application
  20.         mHINSTANCE = GetModuleHandle(NULL);
  21.  
  22.         // Give the application a name
  23.         mApplicationName = L"E-ngine";
  24.  
  25.         // Setup the windows class with default settings
  26.         wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  27.         wc.lpfnWndProc = WndProc;
  28.         wc.cbClsExtra = 0;
  29.         wc.cbWndExtra = 0;
  30.         wc.hInstance = mHINSTANCE;
  31.         wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
  32.         wc.hIconSm = wc.hIcon;
  33.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  34.         wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  35.         wc.lpszMenuName = NULL;
  36.         wc.lpszClassName = mApplicationName;
  37.         wc.cbSize = sizeof(WNDCLASSEX);
  38.  
  39.         // Register the window class
  40.         RegisterClassEx(&wc);
  41.  
  42.         // Setup the screen settings depending on whether it is running in full screen or in windowed mode
  43.         if(mFullscreen)
  44.         {
  45.             // If full screen set the screen to maximum size of the users desktop and 32bit
  46.             mScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  47.             mScreenHeight = GetSystemMetrics(SM_CYSCREEN);
  48.             //--//
  49.             memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
  50.             dmScreenSettings.dmSize = sizeof(dmScreenSettings);
  51.             dmScreenSettings.dmPelsWidth = (uLong)mScreenWidth;
  52.             dmScreenSettings.dmPelsHeight = (uLong)mScreenHeight;
  53.             dmScreenSettings.dmBitsPerPel = 32;
  54.             dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  55.  
  56.             // Change the display settings to full screen
  57.             ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
  58.  
  59.             // Set the position of the window to the top left corner
  60.             posX = posY = 0;
  61.         }
  62.         else
  63.         {
  64.             // Place the window in the middle of the screen
  65.             posX = (GetSystemMetrics(SM_CXSCREEN) - mScreenWidth) / 2;
  66.             posY = (GetSystemMetrics(SM_CYSCREEN) - mScreenHeight) / 2;
  67.         }
  68.  
  69.         // Create the window with the screen settings and get the handle to it
  70.         mHWND = CreateWindowExW(WS_EX_APPWINDOW, mApplicationName, mApplicationName, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, posX, posY, mScreenWidth, mScreenHeight, NULL, NULL, mHINSTANCE, NULL);
  71.        
  72.         // Bring the window up on the screen and set it as main focus
  73.         ShowWindow(mHWND, SW_SHOW);
  74.         SetForegroundWindow(mHWND);
  75.         SetFocus(mHWND);
  76.  
  77.         // Show/Hide the mouse cursor
  78.         ShowCursor(true);
  79.  
  80.         return true;
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment