mfc 窗口控件全屏显示

时间:2021-09-07 10:06:05

1、对话框全屏显示

。h文件

// 123Dlg.h : header file
//

#pragma once
#include "afxwin.h"


// CMy123Dlg dialog
class CMy123Dlg : public CDialog
{
// Construction
public:
	CMy123Dlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	enum { IDD = IDD_MY123_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support

	BOOL m_bOpen;
	//全屏显示
	//成员变量
	BOOL            m_bFullScreen;     // 全屏标志
	CRect           m_FullScreenRect;  // 整个屏幕尺寸
	WINDOWPLACEMENT m_OldWndPlacement; // 全屏时对话框的位置属性
	WINDOWPLACEMENT m_NewWndPlacement; // 全屏后对话框的位置属性

	void OnFullShow();
	void NormalShow(void);

// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
public:
	virtual BOOL PreTranslateMessage(MSG* pMsg);
};

。cpp文件

// 123Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "123.h"
#include "123Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CMy123Dlg dialog




CMy123Dlg::CMy123Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy123Dlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy123Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CMy123Dlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// CMy123Dlg message handlers

BOOL CMy123Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	m_bOpen = TRUE;
	m_bFullScreen = FALSE;

	GetDlgItem(IDC_SHOW_IMAGE)->MoveWindow(20,20,50,50);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMy123Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMy123Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMy123Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//成员函数
void CMy123Dlg::OnFullShow()
{
	// TODO: Add your command handler code here

	if ((TRUE == m_bOpen) && (FALSE == m_bFullScreen))
	{
		m_bFullScreen = TRUE;

		//获取普通窗口位置
		GetWindowPlacement(&m_OldWndPlacement);
		CRect WindowRect;
		GetWindowRect(&WindowRect);  

		//获取各个控制条之外的客户区位置
		CRect ClientRect;
		RepositionBars(0, 0xffff, AFX_IDW_PANE_FIRST, reposQuery, &ClientRect);
		ClientToScreen(&ClientRect);

		//获取屏幕的分辨率
		int nFullWidth  = GetSystemMetrics(SM_CXSCREEN);
		int nFullHeight = GetSystemMetrics(SM_CYSCREEN);

		//对话框全屏显示
		m_FullScreenRect.left = WindowRect.left - ClientRect.left;
		int m_top = WindowRect.top - ClientRect.top;
		m_FullScreenRect.top    = m_top;
		m_FullScreenRect.right  = WindowRect.right - ClientRect.right + nFullWidth;
		m_FullScreenRect.bottom = WindowRect.bottom - ClientRect.bottom + nFullHeight;

		//进入全屏显示
		m_NewWndPlacement.length           = sizeof(WINDOWPLACEMENT);
		m_NewWndPlacement.flags            = 0;
		m_NewWndPlacement.showCmd          = SW_SHOWNORMAL;
		m_NewWndPlacement.rcNormalPosition = m_FullScreenRect;
		SetWindowPlacement(&m_NewWndPlacement);

		//Picture控件全屏显示
		GetDlgItem(IDC_SHOW_IMAGE)->MoveWindow(0,0,nFullWidth,nFullHeight);
		//隐藏控件
		//GetDlgItem(IDC_PLAY)->ShowWindow(SW_HIDE);
		
		Invalidate();
	}
}

void CMy123Dlg::NormalShow(void)
{
	if ((TRUE == m_bOpen) && (TRUE == m_bFullScreen))
	{
		m_bFullScreen = FALSE;

		//恢复默认窗口
		SetWindowPlacement(&m_OldWndPlacement);

		GetDlgItem(IDC_SHOW_IMAGE)->MoveWindow(20,20,50,50);
		//显示控件
		//GetDlgItem(IDC_PLAY)->ShowWindow(SW_SHOW);

		Invalidate();
	}
}

BOOL CMy123Dlg::PreTranslateMessage(MSG *pMsg)
{
	if (TRUE == m_bOpen)
	{
		if (WM_LBUTTONDBLCLK == pMsg->message) // 1 鼠标双击响应
		{
			//获取鼠标位置
			CPoint ptCursor;
			GetCursorPos(&ptCursor);

			CRect rc;
			GetDlgItem(IDC_SHOW_IMAGE)->GetWindowRect(&rc);

			//1.1 双击时鼠标位于Picture控件范围内才响应
			if (rc.PtInRect(ptCursor))
			{
				if (FALSE == m_bFullScreen)
				{
					OnFullShow(); //全屏显示
				}
				else
				{
					NormalShow();  //恢复默认显示
				}
			}
		} //end of if (WM_LBUTTONDBLCLK == pMsg->message)
		else if (WM_KEYDOWN == pMsg->message) // 2 键盘输入响应
		{
			// 2.1键盘输入Esc
			if (VK_ESCAPE == pMsg->wParam)
			{
				if (TRUE == m_bFullScreen)
				{
					NormalShow();  //恢复默认显示
				}                
			}

			// 2.2键盘输入Enter
			if (VK_RETURN == pMsg->wParam)
			{
				if (FALSE == m_bFullScreen)
				{
					OnFullShow(); //全屏显示
				}
				else
				{
					NormalShow();  //恢复默认显示
				}
			}
		} //end of if (WM_KEYDOWN == pMsg->message)
	} // end of if (TRUE == m_bOpen)

	return 0;
}

2、单文档全屏

view全屏

//成员函数
void CMainFrame::OnFullShow()
{
	// TODO: Add your command handler code here

	//m_bToolBarWasVisible=(m_wndToolBar.IsWindowVisible()!=0);//隐藏工具栏之前工具栏的显示状态    
	//m_wndToolBar.ShowWindow(SW_HIDE);//隐藏工具栏 

	//m_bStatusBarWasVisible=(m_wndStatusBar.IsWindowVisible()!=0);//隐藏状态栏之前状态栏的显示状态 
	//m_wndStatusBar.ShowWindow(SW_HIDE);//隐藏状态栏 

	//隐藏菜单栏 
	SetMenu(NULL); 

	//// 保存以前的位置信息 
	//GetWindowRect(&m_mainRect); 

	// 去除主窗口的标题 
	LONG style=::GetWindowLong(m_hWnd,GWL_STYLE); 
	style&=~WS_CAPTION; 
	::SetWindowLong(m_hWnd,GWL_STYLE,style); 

	//得到当前系统的分辨率 
	int screenx=GetSystemMetrics(SM_CXSCREEN); 
	int screeny=GetSystemMetrics(SM_CYSCREEN); 

	// 全屏显示 
	SetWindowPos(NULL,0,0,screenx,screeny,SWP_NOZORDER); 

	// 设置最大化,就不会显示周围一条蓝边
	ShowWindow(SW_MAXIMIZE);


	////设置全屏显示标志 
	//m_bFullScreenMode=true; 
}

void CMainFrame::FullScreenModeOff() 
{ 
	//恢复窗口标题 
	LONG style=::GetWindowLong(m_hWnd,GWL_STYLE); 
	style|=WS_CAPTION; 
	::SetWindowLong(m_hWnd,GWL_STYLE,style); 
	//如果需要,显示工具栏 
	if(m_bToolBarWasVisible) 
		m_wndToolBar.ShowWindow(SW_SHOW); 
	//如果需要,显示状态栏 
	if(m_bStatusBarWasVisible) 
		m_wndStatusBar.ShowWindow(SW_SHOW); 
	//恢复窗口以前的大小 
	MoveWindow(&m_mainRect); 
	//恢复菜单栏 
	this->SetMenu(&m_menuMainWnd); 
	//设置全屏显示标志 
	m_bFullScreenMode=false; 
} 
void CMainFrame::FullScreenModeSwitch() 
{ 
	if(this->m_bFullScreenMode) 
	{ 
		this->FullScreenModeOff(); 
	} 
	else 
	{ 
		this->FullScreenModeOn(); 
	} 
}