1、首先在xxxApp文件中的InitInstance()
// 初始化窗体
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
InitCtrls.dwICC = ICC_WIN95_CLASSES;
// 注册窗口
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
// 创建主窗口
CMainFrame* pFrame = new CMainFrame;
if (!pFrame)
return FALSE;
CRect rect;
::SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
rect.left *= 0.8;
rect.right *= 0.8;
rect.top *= 0.9;
rect.bottom *= 0.9;
pFrame->CreateEx(0, _T("xxxx"), _T("xxxx"), WS_OVERLAPPEDWINDOW, rect, NULL, NULL);
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->CenterWindow();
pFrame->UpdateWindow();
pFrame->MaxSize();
pFrame->BringWindowToTop();
2、在CMainFrame中添加PreCreateWindow
设置窗体属性
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CAsbCtrlContainer::PreCreateWindow(cs) )
return FALSE;
WNDCLASS wndcls;
ZeroMemory(&wndcls, sizeof(wndcls));
wndcls.style = CS_DBLCLKS;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.hInstance = AfxGetInstanceHandle();
wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
wndcls.lpszClassName = cs.lpszClass;
wndcls.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
wndcls.hbrBackground = 0;
AfxRegisterClass(&wndcls);
return TRUE;
}
3、在OnCreate中设置窗口模式
ModifyStyle(WS_CAPTION, 0, SWP_FRAMECHANGED);
4、在OnSize中,创建其它控件之前要先判断窗体是否创建成功
if ( m_wndDeviceInfo.GetSafeHwnd() != NULL &&
m_wndTitleBar.GetSafeHwnd() != NULL &&
m_wndStatusBar.GetSafeHwnd() != NULL &&
m_wndMainView.GetSafeHwnd() !=NULL )
{
CRect rcClient;
GetClientRect(rcClient);
if (rcClient.IsRectEmpty())
return;
....................
....................
}
完成这几步后就可以去掉系统标题栏,不管放大还是缩小,系统标题栏都不会出现
但是,会发现窗口外面有一层白色的框框,现在来去掉白色框框
5、在PreCreateWindow函数设置了一个窗口消息
wndcls.lpfnWndProc = ::DefWindowProc;
利用在这个消息可以去掉白色边框
在CMainFrame添加虚函数
virtual LRESULT DefWindowProc( UINT message, WPARAM wParam, LPARAM lParam );
LRESULT CMainFrame::DefWindowProc( UINT message, WPARAM wParam, LPARAM lParam )
{
LRESULT lrst=CAsbCtrlContainer::DefWindowProc(message, wParam, lParam);
if (!::IsWindow(m_hWnd))
return lrst;
if (message==WM_MOVE||message==WM_PAINT||message==WM_NCPAINT||message==WM_NCACTIVATE ||message == WM_NOTIFY)
{
//如果已经最大化,则不再需要绘制边框,以免挡住了图标。
CRect rtWnd,rect;
GetWindowRect(&rtWnd);
::SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
if (rtWnd.Height() == rect.Height())
{
return lrst;
}
CDC* pWinDC = GetWindowDC();
if (pWinDC)
DrawBorder(pWinDC);
ReleaseDC(pWinDC);
//InvalidateRect(NULL);
}
return lrst;
}
6、然后在DrawBorder(pWinDC);中把边框用颜色填充
void CMainFrame::DrawBorder(CDC *pDC)
{
if (m_hWnd )
{
CBrush Brush(RGB(0,0,0));
CBrush* pOldBrush = pDC->SelectObject(&Brush);
CRect rtWnd;
GetWindowRect(&rtWnd);
CPoint point;
//填充顶部框架
point.x = rtWnd.Width();
point.y = GetSystemMetrics(SM_CYFRAME);
pDC->PatBlt(0, 0, point.x, point.y, PATCOPY);
//填充左侧框架
point.x = GetSystemMetrics(SM_CXFRAME);
point.y = rtWnd.Height();
pDC->PatBlt(0, 0, point.x, point.y, PATCOPY);
//填充底部框架
point.x = rtWnd.Width();
point.y = GetSystemMetrics(SM_CYFRAME) ;
pDC->PatBlt(0, rtWnd.Height()-point.y , point.x, point.y, PATCOPY);
//填充右侧框架
point.x = GetSystemMetrics(SM_CXFRAME) ;
point.y = rtWnd.Height();
pDC->PatBlt(rtWnd.Width()-point.x , 0, point.x, point.y, PATCOPY);
pDC->SelectObject(pOldBrush);
}
}
可以完美实现去图标题栏
另外要在动态创建的标题栏上双击缩放窗口
1、首先要在标题栏添加鼠标点击和双击事件
void CTitleBarWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
GetParent()->PostMessage(WM_TITLE_LBUTTONDOWN, point.x,point.y);
CAsbCtrlContainer::OnLButtonDown(nFlags, point);
}
void CTitleBarWnd::OnLButtonDblClk(UINT nFlags, CPoint point)
{
GetParent()->PostMessage(WM_TITLE_LBUTTONDBLCLK, point.x,point.y);
CAsbCtrlContainer::OnLButtonDblClk(nFlags, point);
}
发送WM_TITLE_LBUTTONDOWN和WM_TITLE_LBUTTONDOWN消息到主程序窗口
2、在主程序窗口也要添加消息事件
// 双击标题栏
LRESULT CMainFrame::OnNcLButtonDblClk(WPARAM wParam, LPARAM lParam)
{
// if ( m_wndDeviceInfo.GetSafeHwnd() || m_wndMainView.GetSafeHwnd() )
// return 0;
MaxSize();
return 0;
}
MaxSize()函数中缩放窗口大小
void CMainFrame::MaxSize()
{
static CRect s_rcCurrentMax(0,0,0,0);
DWORD dwStyle = GetWindowLong(GetSafeHwnd(), GWL_STYLE);
if (dwStyle & WS_THICKFRAME)
{
dwStyle &= ~WS_THICKFRAME;
SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle);
GetWindowRect(s_rcCurrentMax);
//int nScreenW = GetSystemMetrics(SM_CXSCREEN);
//int nScreenH = GetSystemMetrics(SM_CYSCREEN);
CRect rect;
::SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
MoveWindow(rect);
Relayout();
//m_wndTitleBar.UpdateMaxbtnImage(TRUE);
m_wndTitleBar.Invalidate();
m_wndTitleBar.UpdateFullScreenBtnStatus( TRUE );
}
else
{
dwStyle |= WS_THICKFRAME;
SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle);
MoveWindow(s_rcCurrentMax);
Relayout();
//m_wndTitleBar.UpdateMaxbtnImage(FALSE);
m_wndTitleBar.Invalidate();
m_wndTitleBar.UpdateFullScreenBtnStatus( FALSE );
}
}
// 单击标题栏
LRESULT CMainFrame::OnNcLButtonDown(WPARAM wParam, LPARAM lParam)
{
// if ( m_wndDeviceInfo.GetSafeHwnd() || m_wndMainView.GetSafeHwnd() )
// return 0;
DWORD dwStyle = GetWindowLong(GetSafeHwnd(), GWL_STYLE);
if (dwStyle & WS_THICKFRAME) // 没有最大化时才能拖动标题栏
{
CPoint point;
point.x = wParam;
point.y = lParam;
PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x,point.y));
}
return 0;
}
WM_NCLBUTTONDOWN消息使鼠标点击标题栏时能拖动窗口