Advertisement
Guest User

windows7_fullscreen_modal_dialog_pb_not_with_directx

a guest
Mar 5th, 2010
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.03 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 USE_OPENGL 1
  19.  
  20. #define POPUP_MODAL_ON_TIMER 0
  21.  
  22. //----------------------------------------------------------------------------
  23. //----------------------------------------------------------------------------
  24.  
  25. #define WIN32_LEAN_AND_MEAN
  26. #include <windows.h>
  27. #include <stdlib.h>
  28. #include <tchar.h>
  29.  
  30. #include <math.h>
  31.  
  32. #include <mmsystem.h>
  33. #pragma comment (lib, "winmm.lib")
  34.  
  35. #if USE_OPENGL
  36.  
  37. #include <GL/GL.h>
  38. #pragma comment (lib, "opengl32.lib")
  39.  
  40. HDC g_gl_hdc = NULL;
  41.  
  42. BOOL initOpenGL(HWND hWnd);
  43.  
  44. #else
  45.  
  46. #include <d3d9.h>
  47. #include <d3dx9.h>
  48. #pragma comment (lib, "d3d9.lib")
  49.  
  50. LPDIRECT3D9       g_pD3D       = NULL;
  51. LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
  52.  
  53. BOOL initDirectX(HWND hWnd);
  54.  
  55. #endif
  56.  
  57. HINSTANCE hInst;
  58. TCHAR const szWindowClass[] = TEXT("FullscreenWindowClass");
  59.  
  60. ATOM                MyRegisterClass(HINSTANCE hInstance);
  61. BOOL                InitInstance(HINSTANCE, int);
  62. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  63.  
  64. //----------------------------------------------------------------------------
  65. //----------------------------------------------------------------------------
  66.  
  67. int APIENTRY _tWinMain(HINSTANCE hInstance,
  68.                      HINSTANCE hPrevInstance,
  69.                      LPTSTR    lpCmdLine,
  70.                      int       nCmdShow)
  71. {
  72.   TCHAR const text[] =
  73.     TEXT("// Small Win32 sample application made to illustrate a problem encountered on\n")
  74.     TEXT("// Windows 7 with OpenGL in a window covering the whole screen.\n")
  75.     TEXT("//\n")
  76. #if USE_OPENGL
  77.     TEXT("// * OPENGL MODE\n")
  78. #else
  79.     TEXT("// * DIRECTX MODE\n")
  80. #endif
  81.     TEXT("//\n")
  82.     TEXT("// In this context, opening a modal dialog on Windows 7 leads the dialog not\n")
  83.     TEXT("// to be shown correctly. The application looks like being freezed while the\n")
  84.     TEXT("// dialog in unvisible. You have to 'Alt-Tab' the application to see the dialog.\n")
  85.     TEXT("//\n")
  86.     TEXT("// On Windows XP, the modal dialog behaves and is properly visible.\n")
  87.     TEXT("//\n")
  88.     TEXT("// PRESS SPACE TO PROVOKE THE DIALOG OPENING\n")
  89.     TEXT("//\n")
  90.     TEXT("// PRESS ESCAPE OR ALT-F4 TO QUIT\n")
  91.     ;
  92.  
  93.   if (MessageBox(NULL, text, TEXT("Continue ?"), MB_OKCANCEL) == IDCANCEL)
  94.     return 0;
  95.  
  96.     UNREFERENCED_PARAMETER(hPrevInstance);
  97.     UNREFERENCED_PARAMETER(lpCmdLine);
  98.  
  99.     MSG msg;
  100.  
  101.     MyRegisterClass(hInstance);
  102.     if (!InitInstance (hInstance, nCmdShow))
  103.     {
  104.         return FALSE;
  105.     }
  106.  
  107.     // Main message loop:
  108.  
  109.   bool quit = false;
  110.  
  111.   while (!quit)
  112.   {
  113.     if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  114.     {
  115.       do
  116.       {
  117.         if (msg.message == WM_QUIT) quit = true;
  118.             TranslateMessage(&msg);
  119.             DispatchMessage(&msg);
  120.       }
  121.       while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE));
  122.     }
  123.  
  124.     DWORD time = ::timeGetTime();
  125.  
  126.     float color_r = 0.5f + 0.5f * cos( 1e3f * time );
  127.     float color_g = 0.5f + 0.5f * sin( 5e4f * time );
  128.  
  129. #if USE_OPENGL
  130.  
  131.     //------------------------------------------------------------------------
  132.  
  133.     glClearColor(color_r, color_g,0,0);
  134.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  135.  
  136.     ::SwapBuffers(g_gl_hdc);
  137.  
  138. #else
  139.  
  140.     //------------------------------------------------------------------------
  141.  
  142.     g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
  143.                          D3DCOLOR_COLORVALUE(color_r, color_g, 0.0f, 1.0f), 1.0f, 0 );
  144.  
  145.     g_pd3dDevice->BeginScene();
  146.  
  147.     g_pd3dDevice->EndScene();
  148.  
  149.     g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
  150.  
  151. #endif
  152.  
  153.     //------------------------------------------------------------------------
  154.   }
  155.  
  156.     return (int) msg.wParam;
  157. }
  158.  
  159. //----------------------------------------------------------------------------
  160.  
  161. ATOM MyRegisterClass(HINSTANCE hInstance)
  162. {
  163.     WNDCLASSEX wcex;
  164.  
  165.     wcex.cbSize = sizeof(WNDCLASSEX);
  166.  
  167.     wcex.style          = CS_HREDRAW | CS_VREDRAW;
  168.     wcex.lpfnWndProc    = WndProc;
  169.     wcex.cbClsExtra     = 0;
  170.     wcex.cbWndExtra     = 0;
  171.     wcex.hInstance      = hInstance;
  172.     wcex.hIcon          = NULL;
  173.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  174.     wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  175.     wcex.lpszMenuName   = NULL;
  176.     wcex.lpszClassName  = szWindowClass;
  177.     wcex.hIconSm        = NULL;
  178.  
  179.     return RegisterClassEx(&wcex);
  180. }
  181.  
  182. //----------------------------------------------------------------------------
  183.  
  184. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  185. {
  186.   HWND hWnd;
  187.  
  188.   hInst = hInstance; // Store instance handle in our global variable
  189.  
  190.   int width  = GetSystemMetrics(SM_CXSCREEN);
  191.   int height = GetSystemMetrics(SM_CYSCREEN);
  192.   hWnd = CreateWindow(szWindowClass, TEXT("Fullscreen window"), WS_POPUP,
  193.     0, 0, width, height, NULL, NULL, hInstance, NULL);
  194.  
  195.   if (!hWnd)
  196.   {
  197.     return FALSE;
  198.   }
  199.  
  200.   //
  201.  
  202. #if USE_OPENGL
  203.  
  204.   if (!initOpenGL(hWnd))
  205.     return FALSE;
  206.  
  207. #else
  208.  
  209.   if (!initDirectX(hWnd))
  210.     return FALSE;
  211.  
  212. #endif
  213.  
  214.   //
  215.  
  216. #if POPUP_MODAL_ON_TIMER
  217.  
  218.   SetTimer(hWnd, 123, 5000, NULL);
  219.  
  220. #endif
  221.  
  222.   //
  223.  
  224.   ShowWindow(hWnd, nCmdShow);
  225.   UpdateWindow(hWnd);
  226.  
  227.   return TRUE;
  228. }
  229.  
  230. #if USE_OPENGL
  231.  
  232. //----------------------------------------------------------------------------
  233.  
  234. BOOL initOpenGL(HWND hWnd)
  235. {
  236.   HMODULE library = LoadLibrary( TEXT("DWMAPI.DLL") );
  237.   if (library != 0)
  238.   {
  239.     typedef HRESULT (__stdcall *PFN)(UINT);
  240.     PFN func = (PFN)GetProcAddress( library, "DwmEnableComposition" );
  241.     if (func != 0)
  242.     {
  243.       func( /*DWM_EC_DISABLECOMPOSITION*/ 0 );
  244.     }
  245.     FreeLibrary (library);
  246.   }
  247.  
  248.   // OpenGL
  249.  
  250.   PIXELFORMATDESCRIPTOR pfd;
  251.    memset(&pfd, 0, sizeof(pfd));
  252.    pfd.nSize = sizeof(pfd);
  253.    pfd.nVersion = 1;
  254.    pfd.dwFlags =
  255.     PFD_DRAW_TO_WINDOW |
  256.         PFD_SUPPORT_OPENGL |
  257.         PFD_DOUBLEBUFFER |
  258.         PFD_GENERIC_ACCELERATED |
  259.     PFD_SWAP_EXCHANGE
  260.     ;
  261.    pfd.iPixelType = PFD_TYPE_RGBA;
  262.    pfd.cColorBits = 32;
  263.    pfd.cAlphaBits = 8;
  264.    pfd.cDepthBits = 24;
  265.    pfd.cStencilBits = 8;
  266.    pfd.iLayerType = PFD_MAIN_PLANE;
  267.  
  268.   int  selected_pf;
  269.  
  270.   HDC hdc = ::GetDC(hWnd);
  271.   if ( ( selected_pf = ChoosePixelFormat( hdc, &pfd ) ) == 0 )
  272.   {
  273.     return FALSE;
  274.   }
  275.  
  276.   if ( !SetPixelFormat( hdc, selected_pf, &pfd) )
  277.   {
  278.     return FALSE;
  279.   }
  280.  
  281.   HGLRC hglrc = wglCreateContext(hdc);
  282.   if (hglrc == 0)
  283.   {
  284.     return FALSE;
  285.   }
  286.  
  287.   if (!wglMakeCurrent(hdc, hglrc))
  288.   {
  289.     return FALSE;
  290.   }
  291.  
  292.   g_gl_hdc = hdc;
  293.  
  294.   return TRUE;
  295. }
  296.  
  297. #else
  298.  
  299. //----------------------------------------------------------------------------
  300.  
  301. BOOL initDirectX(HWND hWnd)
  302. {
  303.   g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
  304.  
  305.   if( g_pD3D == NULL )
  306.   {
  307.     return FALSE;
  308.   }
  309.  
  310.   D3DDISPLAYMODE d3ddm;
  311.  
  312.   if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
  313.   {
  314.     return FALSE;
  315.   }
  316.  
  317.   HRESULT hr;
  318.  
  319.   if( FAILED( hr = g_pD3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
  320.     d3ddm.Format, D3DUSAGE_DEPTHSTENCIL,
  321.     D3DRTYPE_SURFACE, D3DFMT_D16 ) ) )
  322.   {
  323.     if( hr == D3DERR_NOTAVAILABLE )
  324.       return FALSE;
  325.   }
  326.  
  327.   //
  328.   // Do we support hardware vertex processing? if so, use it.
  329.   // If not, downgrade to software.
  330.   //
  331.  
  332.   D3DCAPS9 d3dCaps;
  333.  
  334.   if( FAILED( g_pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT,
  335.     D3DDEVTYPE_HAL, &d3dCaps ) ) )
  336.   {
  337.     return FALSE;
  338.   }
  339.  
  340.   DWORD dwBehaviorFlags = 0;
  341.  
  342.   if( d3dCaps.VertexProcessingCaps != 0 )
  343.     dwBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
  344.   else
  345.     dwBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  346.  
  347.   //
  348.   // Everything checks out - create a simple, windowed device.
  349.   //
  350.  
  351.   D3DPRESENT_PARAMETERS d3dpp;
  352.   memset(&d3dpp, 0, sizeof(d3dpp));
  353.  
  354.   d3dpp.BackBufferCount        = 1;
  355.   d3dpp.BackBufferFormat       = d3ddm.Format;
  356.   d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
  357.   d3dpp.Windowed               = TRUE;
  358.   d3dpp.EnableAutoDepthStencil = TRUE;
  359.   d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
  360.   d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_ONE;
  361.  
  362.   if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  363.     dwBehaviorFlags, &d3dpp, &g_pd3dDevice ) ) )
  364.   {
  365.     return FALSE;
  366.   }
  367.  
  368.   return TRUE;
  369. }
  370.  
  371. #endif
  372.  
  373. //----------------------------------------------------------------------------
  374.  
  375. void modal_dialog(HWND hWnd)
  376. {
  377. #if USE_OPENGL
  378.       MessageBox(hWnd, TEXT("OpenGL mode\n\nThis dialog box should open in front of the main window.\n\nDoes it ? On my Win7, it does not."), TEXT("Hello"), MB_OK);
  379. #else
  380.       MessageBox(hWnd, TEXT("DirectX mode\n\nThis dialog box should open in front of the main window.\n\nDoes it ? On my Win7, it does... Damn OpenGL..."), TEXT("Hello"), MB_OK);
  381. #endif
  382. }
  383.  
  384. //----------------------------------------------------------------------------
  385.  
  386. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  387. {
  388.     switch (message)
  389.     {
  390. #if POPUP_MODAL_ON_TIMER
  391.  
  392.   case WM_TIMER:
  393.     modal_dialog(hWnd);
  394.     break;
  395.  
  396. #endif
  397.  
  398.   case WM_RBUTTONUP:
  399.     modal_dialog(hWnd);
  400.     break;
  401.  
  402.   case WM_KEYUP:
  403.     if (wParam == VK_SPACE)
  404.       modal_dialog(hWnd);
  405.     if (wParam == VK_ESCAPE)
  406.       DestroyWindow(hWnd);
  407.     break;
  408.    
  409.   case WM_DESTROY:
  410.         PostQuitMessage(0);
  411.         break;
  412.    
  413.   default:
  414.         return DefWindowProc(hWnd, message, wParam, lParam);
  415.     }
  416.     return 0;
  417. }
  418.  
  419. //----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement