CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Feb 2000
    Location
    Indore, India
    Posts
    1,046

    MFC General: How do I create full screen applications?

    Q: How do I create full screen applications?

    A: A common technique to create full screen applications is to expand the application window such that window elements like title bar, menu bar etc are beyond the visible region of the display. Only the client area will be visible.

    The example shown below modifies the class 'CMainFrame' to achieve the full screen effect. Expanding the application to full screen mode or to normal mode is done by a member function named 'ToggleView()'. The steps to achieve full screen functionality are as follows:

    Define a member variable of type 'CRect' (say 'm_rectFull') for the class 'CMainFrame' indicating the size of the window in full screen mode. 'm_rectFull' is calculated in the constructor of 'CMainFrame'.

    Code:
    CMainFrame::CMainFrame()
    {
      // TODO: add member initialization code here
    
      // Calculates window position and span in full screen mode
    
      // Calculates span of display area
      HDC hDC = ::GetDC(NULL);
      int XSpan = GetDeviceCaps(hDC, HORZRES);
      int YSpan = GetDeviceCaps(hDC, VERTRES);
      ::ReleaseDC(NULL, hDC);
    
      // Calculates size of window elements
      int XBorder = GetSystemMetrics(SM_CXFRAME);
      int YCaption = GetSystemMetrics(SM_CYCAPTION);
      int YMenu = GetSystemMetrics(SM_CYMENU);
      int YBorder = GetSystemMetrics(SM_CYFRAME);
    
      // Calculates window origin and span for full screen mode
      CPoint Origin = CPoint(-XBorder, -YBorder - YCaption - YMenu);
      XSpan += 2 * XBorder, YSpan += 2 * YBorder + YMenu + YCaption;
    
    
      // Calculates full screen window rectangle
      rectFull = CRect(Origin, CSize(XSpan, YSpan));
    }
    In the function 'ToggleView()', toggling of the view is carried out. Toolbar and status bar are hidden after storing their visibility status and the window is expanded such that only the client area will be visible. Size and position of the window is also stored before expanding. Calling 'ToggleView()' a second time, the window is displayed in the original size and position again.

    Here is the code for 'ToggleView()':

    Code:
    void CMainFrame::ToggleView()
    {
      // Previous window status parameters
      static CRect rectNormal;
      static boolFull = FALSE, boolStatus, boolTool;
    
      // Checks SDI / MDI application
      if (IsKindOf(RUNTIME_CLASS(CMDIFrameWnd)))
      {
        // MDI Application
        static nCmdShow;  // Previous child-window status
        CMDIChildWnd* pChild = ((CMDIFrameWnd*) this)->MDIGetActive();
        if (boolFull)
        {
          // Resets child window show status
          pChild->ShowWindow(nCmdShow);
        }
        else
        {
          // Gets child window show status
          WINDOWPLACEMENT WP;
          pChild->GetWindowPlacement(&WP);
          nCmdShow = WP.showCmd;
          
          // Maximizes child window
          pChild->ShowWindow(SW_SHOWMAXIMIZED);
        }
      }
      
      if (boolFull)
      {
        // Toggles from full screen to normal mode
        // Shows status bar and tool bar if they were visible
        if (boolStatus)
          m_wndStatusBar.ShowWindow(SW_SHOW);
      
        if (boolTool)
          m_wndToolBar.ShowWindow(SW_SHOW);
    
        RecalcLayout();
      }
      else
      {
        // Toggles from normal to full screen mode
        // Saves status bar and toolbar status and hides them
        if (boolStatus = m_wndStatusBar.IsWindowVisible())
          m_wndStatusBar.ShowWindow(SW_HIDE);
    
        if (boolTool = m_wndToolBar.IsWindowVisible())
          m_wndToolBar.ShowWindow(SW_HIDE);
      
        RecalcLayout();
      
        // Saves current window position and size
        GetWindowRect(rectNormal);
      }
    
      // Shows window in full screen / normal mode
      MoveWindow(boolFull ? rectNormal : rectFull);
      
      // Toggles window status
      boolFull = !boolFull;
    }
    This works for both for SDI and MDI applications.

    We have to set the maximum size and position of the window by overriding the message handler function 'CMainFrame::OnGetMinMaxInfo()', handling the 'WM_GETMINMAXINFO' message. Here is the message handler function:

    Code:
    void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) 
    {
      // TODO: Add your message handler code here and/or call default
    
      // Resets position and maximum window size for full screen mode
      lpMMI->ptMaxPosition = rectFull.TopLeft();
      lpMMI->ptMaxTrackSize = lpMMI->ptMaxSize = CPoint(rectFull.Size());
      CMDIFrameWnd::OnGetMinMaxInfo(lpMMI);
    }
    With this, a SDI application will expand to full screen mode on calling 'ToggleView()'. One can call this function from anywhere in the application by

    Code:
    ((CMainFrame*) AfxGetMainWnd())->ToggleView();
    One has to include the file 'MainFrm.h' if the 'CMainFrame' class is not defined where you make the function call.

    For an MDI application, you have to handle the 'WM_GETMINMAXINFO' message for the 'CChildFrame' class also to effect the expansion properly. 'CChildFrame::OnGetMinMaxInfo()' can be implemented as

    Code:
    #include "MainFrm.h"
    
    void CChildFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) 
    {
      // TODO: Add your message handler code here and/or call default
    
      // Resets position and maximum child window size for full screen mode
      CSize Span = ((CMainFrame*) AfxGetMainWnd())->rectFull.Size();
      lpMMI->ptMaxPosition = ((CMainFrame*) AfxGetMainWnd())->rectFull.TopLeft();
      lpMMI->ptMaxTrackSize = lpMMI->ptMaxSize = CPoint(Span.cx, Span.cy);
      CMDIChildWnd::OnGetMinMaxInfo(lpMMI);
    }
    Last edited by Andreas Masur; November 13th, 2005 at 08:53 AM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1

    Re: MFC General: How do I create full screen applications?

    Great solution!
    Tried to use it in my application, everything works fine, but had to make some improvements... The problem was - when i clicked the maximize button - the application goes in to fullscreen mode and there is no way to restore it to normal... To avoid this I have created a member variable of type bool m_WhowantsToKnow n the mainframe class, then used it in the OnGetMinMaxInfo function to make it "lie" only for the fulscreen function purposes like this :

    in the CMainFrame function:

    Code:
    CMainFrame::CMainFrame()
    {
    	// TODO: add member initialization code here
    
    	// Calculates window position and span in full screen mode
    
    	// Calculates span of display area
    
    	m_WhoWantsToKnow = TRUE;
    
    	HDC hDC = ::GetDC(NULL);
    	int XSpan = GetDeviceCaps(hDC, HORZRES);
    	int YSpan = GetDeviceCaps(hDC, VERTRES);
    	::ReleaseDC(NULL, hDC);
    
    	m_WhoWantsToKnow = FALSE;
    
    	// Calculates size of window elements
    	int XBorder = GetSystemMetrics(SM_CXFRAME);
    	int YCaption = GetSystemMetrics(SM_CYCAPTION);
    	int YMenu = GetSystemMetrics(SM_CYMENU);
    	int YBorder = GetSystemMetrics(SM_CYFRAME);
    
    	// Calculates window origin and span for full screen mode
    	CPoint Origin = CPoint(-XBorder, -YBorder - YCaption - YMenu);
    	XSpan += 2 * XBorder, YSpan += 2 * YBorder + YMenu + YCaption;
    
    
    	// Calculates full screen window rectangle
    	m_FullRect = CRect(Origin, CSize(XSpan, YSpan));
    }
    and in the ToogleView like this:
    Code:
    void CMainFrame::ToogleView(void)
    {
    	// Previous window status parameters
    	static CRect rectNormal;
    	static int boolFull = FALSE, boolStatus, boolTool;
    
    	// Checks SDI / MDI application
    	if (IsKindOf(RUNTIME_CLASS(CMDIFrameWnd)))
    	{
    	// MDI Application
    	static int nCmdShow;  // Previous child-window status
    	CMDIChildWnd* pChild = ((CMDIFrameWnd*) this)->MDIGetActive();
    	if (boolFull)
    	{
    	  // Resets child window show status
    	  pChild->ShowWindow(nCmdShow);
    	}
    	else
    	{
    	  // Gets child window show status
    	  WINDOWPLACEMENT WP;
    	  pChild->GetWindowPlacement(&WP);
    	  nCmdShow = WP.showCmd;
    	  
    	  // Maximizes child window
    	  pChild->ShowWindow(SW_SHOWMAXIMIZED);
    	}
    	}
    
    	if (boolFull)
    	{
    	// Toggles from full screen to normal mode
    	// Shows status bar and tool bar if they were visible
    	//if (boolStatus)
    	//  m_wndStatusBar.ShowWindow(SW_SHOW);
    
    	//if (boolTool)
    	//  m_wndToolBar.ShowWindow(SW_SHOW);
    
    	RecalcLayout();
    	}
    	else
    	{
    	// Toggles from normal to full screen mode
    	// Saves status bar and toolbar status and hides them
    	if (boolStatus = m_wndStatusBar.IsWindowVisible())
    	  m_wndStatusBar.ShowWindow(SW_HIDE);
    
    	if (boolTool = m_wndToolBar.IsWindowVisible())
    	  m_wndToolBar.ShowWindow(SW_HIDE);
    	
    	RecalcLayout();
    
    	// Saves current window position and size
    	GetWindowRect(rectNormal);
    	}
    
    	this->m_WhoWantsToKnow = TRUE;
    
    	// Shows window in full screen / normal mode
    	MoveWindow(boolFull ? rectNormal : m_FullRect);
    
    	this->m_WhoWantsToKnow = FALSE;
    
    	// Toggles window status
    	boolFull = !boolFull;
    }
    then in the GetMinMaxInfo added check:
    Code:
    void CMainFrame::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
    {
    	// TODO: Add your message handler code here and/or call default
    	// Resets position and maximum window size for full screen mode
    	if(this->m_WhoWantsToKnow == TRUE){
    		lpMMI->ptMaxPosition = m_FullRect.TopLeft();
    		lpMMI->ptMaxTrackSize = lpMMI->ptMaxSize = Point(m_FullRect.Size());
    	}
    	CFrameWnd::OnGetMinMaxInfo(lpMMI);
    }
    This solved my problems in SDI application....

    Nice post!
    Thanks!

  3. #3
    Join Date
    Apr 2009
    Posts
    2

    Re: MFC General: How do I create full screen applications?

    You can put following code to make the application fullscreen with caption, menubar, status bar etc.

    BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
    {
    if( !CMDIFrameWnd::PreCreateWindow(cs) )
    return FALSE;
    // TODO: Modify the Window class or styles here by modifying
    // the CREATESTRUCT cs

    //Comment out if do not want fullscreen
    cs.style = WS_OVERLAPPED | WS_CAPTION | FWS_ADDTOTITLE | WS_SYSMENU | WS_MAXIMIZE;
    cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
    cs.lpszClass = AfxRegisterWndClass (0);

    return TRUE;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured