现在的位置: 首页 > 综合 > 正文

MFC风格设置

2018年04月17日 ⁄ 综合 ⁄ 共 1900字 ⁄ 字号 评论关闭

1.对话框背景

在CWinApp中的初始化函数中,在第一行添加

	this->SetDialogBkColor(RGB(220,233,248));//设置对话框背景颜色

还可以定义一个CBrush的变量m_brush;然后创建新的画刷

m_brush.CreateSolidBrush(RGB(220,233,248));

然后在color控制事件中返回新的画刷即可

HBRUSH CMy_PCSCDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	
	// TODO: Return a different brush if the default is not desired
	return m_brush;
}

如果返回hbr则为默认的背景,这个方法可以设置特定控件的背景

2.设置位图背景

首先加载位图到文件中然后创建位图画刷

	bitmap.LoadBitmap(IDB_BITMAP1);
	m_brush.CreateSolidBrush(RGB(220,233,248));
	m_BitBrush.CreatePatternBrush(&bitmap);

在ctlcolor中返回这个画刷即可

HBRUSH CMy_PCSCDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if (pWnd->GetDlgCtrlID() == IDC_STATIC_MESSAGE)
	{
		pDC->SelectObject(&m_fontLogo);
		pDC->SetBkMode(TRANSPARENT);
	}	

	return m_BitBrush;
	
	// TODO: Return a different brush if the default is not desired
//	return hbr;
}

3. 对话框全屏

添加如下代码即可实现对画框全屏,但是控件的相对位置并没有变化,还需要优化

	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); 
	/*将除控制条外的客户区全屏显示到从(0,0)到(nFullWidth, nFullHeight)区域, 
	**将(0,0)和(nFullWidth, nFullHeight)两个点外扩充原窗口和除控制条之外的 
	**客户区位置间的差值, 就得到全屏显示的窗口位置 
	*/
	m_FullScreenRect.left=WindowRect.left-ClientRect.left; 
	m_FullScreenRect.top=WindowRect.top-ClientRect.top; 
	m_FullScreenRect.right=WindowRect.right-ClientRect.right+nFullWidth; 
	m_FullScreenRect.bottom=WindowRect.bottom-ClientRect.bottom+nFullHeight; 
	m_bFullScreen=TRUE; //设置全屏显示标志为 TRUE 
	//进入全屏显示状态 
	WINDOWPLACEMENT wndpl; 
	wndpl.length=sizeof(WINDOWPLACEMENT); 
	wndpl.flags=0; 
	wndpl.showCmd=SW_SHOWNORMAL; 
	wndpl.rcNormalPosition=m_FullScreenRect; 
	SetWindowPlacement(&wndpl);

退出全屏

	ShowWindow(SW_HIDE); 
	SetWindowPlacement(&m_OldWndPlacement);	

抱歉!评论已关闭.