Advertisement
Guest User

windows7_fullscreen_modal_dialog_pb

a guest
Mar 4th, 2010
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.81 KB | None | 0 0
  1. //----------------------------------------------------------------------------
  2. //----------------------------------------------------------------------------
  3. // Small Win32 sample application made to illustrate a problem encountered on
  4. // Windows 7 with OpenGL in a window covering the whole screen.
  5. //
  6. // In this context, opening a modal dialog on Windows 7 leads the dialog not
  7. // to be shown correctly. The application looks like being freezed while the
  8. // dialog in unvisible. You have to 'Alt-Tab' the application to see the dialog.
  9. //
  10. // On Windows XP, the modal dialog behaves and is properly visible.
  11. //
  12. // PRESS SPACE TO PROVOKE THE DIALOG OPENING
  13. //
  14. // PRESS ESCAPE OR ALT-F4 TO QUIT
  15. //----------------------------------------------------------------------------
  16. //----------------------------------------------------------------------------
  17.  
  18. #define WIN32_LEAN_AND_MEAN
  19. #include <windows.h>
  20. #include <stdlib.h>
  21. #include <tchar.h>
  22.  
  23. #include <math.h>
  24.  
  25. #include <mmsystem.h>
  26. #pragma comment (lib, "winmm.lib")
  27. #include <GL/GL.h>
  28. #pragma comment (lib, "opengl32.lib")
  29.  
  30. HINSTANCE hInst;
  31. TCHAR const szWindowClass[] = TEXT("FullscreenWindowClass");
  32. HDC g_gl_hdc = NULL;
  33.  
  34. ATOM                MyRegisterClass(HINSTANCE hInstance);
  35. BOOL                InitInstance(HINSTANCE, int);
  36. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  37.  
  38. //----------------------------------------------------------------------------
  39.  
  40. int APIENTRY _tWinMain(HINSTANCE hInstance,
  41.                      HINSTANCE hPrevInstance,
  42.                      LPTSTR    lpCmdLine,
  43.                      int       nCmdShow)
  44. {
  45.   TCHAR const text[] =
  46.     TEXT("// Small Win32 sample application made to illustrate a problem encountered on\n")
  47.     TEXT("// Windows 7 with OpenGL in a window covering the whole screen.\n")
  48.     TEXT("//\n")
  49.     TEXT("// In this context, opening a modal dialog on Windows 7 leads the dialog not\n")
  50.     TEXT("// to be shown correctly. The application looks like being freezed while the\n")
  51.     TEXT("// dialog in unvisible. You have to 'Alt-Tab' the application to see the dialog.\n")
  52.     TEXT("//\n")
  53.     TEXT("// On Windows XP, the modal dialog behaves and is properly visible.\n")
  54.     TEXT("//\n")
  55.     TEXT("// PRESS SPACE TO PROVOKE THE DIALOG OPENING\n")
  56.     TEXT("//\n")
  57.     TEXT("// PRESS ESCAPE OR ALT-F4 TO QUIT\n")
  58.     ;
  59.  
  60.   if (MessageBox(NULL, text, TEXT("Continue ?"), MB_OKCANCEL) == IDCANCEL)
  61.     return 0;
  62.  
  63.     UNREFERENCED_PARAMETER(hPrevInstance);
  64.     UNREFERENCED_PARAMETER(lpCmdLine);
  65.  
  66.     MSG msg;
  67.  
  68.     MyRegisterClass(hInstance);
  69.     if (!InitInstance (hInstance, nCmdShow))
  70.     {
  71.         return FALSE;
  72.     }
  73.  
  74.     // Main message loop:
  75.  
  76.   bool quit = false;
  77.  
  78.   while (!quit)
  79.   {
  80.     if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  81.     {
  82.       do
  83.       {
  84.         if (msg.message == WM_QUIT) quit = true;
  85.             TranslateMessage(&msg);
  86.             DispatchMessage(&msg);
  87.       }
  88.       while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE));
  89.     }
  90.  
  91.     DWORD time = ::timeGetTime();
  92.  
  93.     float color_r = 0.5f + 0.5f * cos( 1e-3f * time );
  94.     float color_g = 0.5f + 0.5f * sin( 5e-4f * time );
  95.  
  96.     glClearColor(color_r, color_g,0,0);
  97.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  98.  
  99.     ::SwapBuffers(g_gl_hdc);
  100.   }
  101.  
  102.     return (int) msg.wParam;
  103. }
  104.  
  105. //----------------------------------------------------------------------------
  106.  
  107. ATOM MyRegisterClass(HINSTANCE hInstance)
  108. {
  109.     WNDCLASSEX wcex;
  110.  
  111.     wcex.cbSize = sizeof(WNDCLASSEX);
  112.  
  113.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  114.     wcex.lpfnWndProc    = WndProc;
  115.     wcex.cbClsExtra     = 0;
  116.     wcex.cbWndExtra     = 0;
  117.     wcex.hInstance      = hInstance;
  118.     wcex.hIcon          = NULL;
  119.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  120.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  121.     wcex.lpszMenuName   = NULL;
  122.     wcex.lpszClassName  = szWindowClass;
  123.     wcex.hIconSm        = NULL;
  124.  
  125.     return RegisterClassEx(&wcex);
  126. }
  127.  
  128. //----------------------------------------------------------------------------
  129.  
  130. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  131. {
  132.   HWND hWnd;
  133.  
  134.   hInst = hInstance; // Store instance handle in our global variable
  135.  
  136.   int width  = GetSystemMetrics(SM_CXSCREEN);
  137.   int height = GetSystemMetrics(SM_CYSCREEN);
  138.   hWnd = CreateWindow(szWindowClass, TEXT("Fullscreen window"), WS_POPUP,
  139.     0, 0, width, height, NULL, NULL, hInstance, NULL);
  140.  
  141.   if (!hWnd)
  142.   {
  143.     return FALSE;
  144.   }
  145.  
  146.   // OpenGL
  147.  
  148.   PIXELFORMATDESCRIPTOR pfd;
  149.    memset(&pfd, 0, sizeof(pfd));
  150.    pfd.nSize = sizeof(pfd);
  151.    pfd.nVersion = 1;
  152.    pfd.dwFlags =
  153.     PFD_DRAW_TO_WINDOW |
  154.         PFD_SUPPORT_OPENGL |
  155.         PFD_DOUBLEBUFFER |
  156.         PFD_GENERIC_ACCELERATED |
  157.     PFD_SWAP_EXCHANGE
  158.     ;
  159.    pfd.iPixelType = PFD_TYPE_RGBA;
  160.    pfd.cColorBits = 32;
  161.    pfd.cAlphaBits = 8;
  162.    pfd.cDepthBits = 24;
  163.    pfd.cStencilBits = 8;
  164.    pfd.iLayerType = PFD_MAIN_PLANE;
  165.  
  166.   int  selected_pf;
  167.  
  168.   HDC hdc = ::GetDC(hWnd);
  169.   if ( ( selected_pf = ChoosePixelFormat( hdc, &pfd ) ) == 0 )
  170.   {
  171.     return FALSE;
  172.   }
  173.  
  174.   if ( !SetPixelFormat( hdc, selected_pf, &pfd) )
  175.   {
  176.     return FALSE;
  177.   }
  178.  
  179.   HGLRC hglrc = wglCreateContext(hdc);
  180.   if (hglrc == 0)
  181.   {
  182.     return FALSE;
  183.   }
  184.  
  185.   if (!wglMakeCurrent(hdc, hglrc))
  186.   {
  187.     return FALSE;
  188.   }
  189.  
  190.   g_gl_hdc = hdc;
  191.  
  192.   //
  193.  
  194.   ShowWindow(hWnd, nCmdShow);
  195.   UpdateWindow(hWnd);
  196.  
  197.   return TRUE;
  198. }
  199.  
  200. //----------------------------------------------------------------------------
  201.  
  202. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  203. {
  204.     switch (message)
  205.     {
  206.   case WM_KEYUP:
  207.     if (wParam == VK_SPACE)
  208.             MessageBox(hWnd, TEXT("This dialog box should open in front of the main window.\n\nDoes it ? On my Win7, it does not."), TEXT("Hello"), MB_OK);
  209.     if (wParam == VK_ESCAPE)
  210.       DestroyWindow(hWnd);
  211.     break;
  212.     case WM_DESTROY:
  213.         PostQuitMessage(0);
  214.         break;
  215.     default:
  216.         return DefWindowProc(hWnd, message, wParam, lParam);
  217.     }
  218.     return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement