📄 skin.cpp
字号:
//draw icon
::DrawIconEx( pNewDC->GetSafeHdc(), m_BorderLeftWidth, 5, (HICON)
CopyImage( hi, IMAGE_ICON,
cx, cy, 0), cx, cy, 0, 0, DI_NORMAL);
//draw text
if ( m_title.IsEmpty() )
pWnd->GetWindowText(m_title);
if (m_bActive)
pNewDC->SetTextColor( m_colTitle1 );
else
pNewDC->SetTextColor( m_colTitle2 );
CFont font, *ofont;
font.CreatePointFont( GetSystemMetrics(SM_CYSMCAPTION), "System" );
ofont = pNewDC->SelectObject(&font);
pNewDC->SetBkMode(TRANSPARENT);
pNewDC->DrawText( m_title, CRect( m_textShift, m_textShiftVer, wr.Width() - m_bmpTitle.Width() + m_titleoff2,
m_TitleHeight ), DT_SINGLELINE | DT_LEFT | DT_VCENTER | DT_WORD_ELLIPSIS );
pNewDC->SelectObject(&font);
pDC->BitBlt( 0, 0, wr.Width(),
m_TitleHeight, pNewDC, 0, 0, SRCCOPY );
memDC.SelectObject(obmp);
pDC->SelectClipRgn(NULL);
}
void CSkin::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS *lpncsp)
{
if ( bCalcValidRects )
{
lpncsp->rgrc[0].left = lpncsp->rgrc[0].left + m_BorderLeftWidth;
lpncsp->rgrc[0].right = lpncsp->rgrc[0].right - m_BorderRightWidth;
lpncsp->rgrc[0].top = lpncsp->rgrc[0].top + m_TitleHeight;
lpncsp->rgrc[0].bottom = lpncsp->rgrc[0].bottom - m_BorderBottomHeight;
lpncsp->rgrc[1] = lpncsp->rgrc[0];
}
else
{
Default();
}
}
void CSkin::OnSizing(UINT nSide, LPRECT lpRect)
{
//Default();
CWnd *pWnd = CWnd::FromHandle(m_hWnd);
CRect wr(lpRect);
/*
if ( m_bTrans )
SetWindowRgn( m_hWnd, GetRegion(wr.Width(), wr.Height() ), TRUE );
else
SetWindowRgn( m_hWnd, NULL, TRUE );
*/
}
void CSkin::OnSize(UINT nType, int cx, int cy)
{
//Default();
CWnd *pWnd = CWnd::FromHandle(m_hWnd);
CRect wr;
Default();
pWnd->GetWindowRect(wr);
pWnd->Invalidate();
OnNcPaint(0);
if ( m_bTrans )
SetWindowRgn( m_hWnd, GetRegion(wr.Width(), wr.Height() ), TRUE );
else
SetWindowRgn( m_hWnd, NULL, TRUE );
}
HRGN CSkin::GetRegion(int w, int h)
{
CWnd *pWnd = CWnd::FromHandle(m_hWnd);
CRect wr;
pWnd->GetWindowRect(wr);
CRgn rgn;
if ( m_bTrans )
{
CDC *pDC = pWnd->GetDC();
CDC memDC;
CBitmapEx bmp;
CBitmap *obmp;
memDC.CreateCompatibleDC(pDC);
bmp.CreateCompatibleBitmap( pDC, w, m_TitleHeight );
obmp = memDC.SelectObject(&bmp);
/*
memDC.FillSolidRect( 0, 0, w, h, 0 );
DrawFrame( &memDC, 0, 0, w, h, 0 );
*/
DrawTitle( &memDC, m_BorderLeftWidth , 0,
wr.Width() - m_BorderRightWidth - m_BorderLeftWidth + 1, 0 );
DrawLeft( &memDC, 0, 0, m_bmpLeft.Height(), 0 );
DrawRight( &memDC, wr.Width() - m_BorderRightWidth , 0, m_bmpRight.Height(), 0 );
memDC.SelectObject(obmp);
pWnd->ReleaseDC( pDC );
rgn.CreateRectRgn( 0, m_TitleHeight, wr.Width(), wr.Height() );
HRGN hrgn = bmp.CreateRgnFromFile( m_colTrans );
CRgn newrgn;
newrgn.CreateRectRgn( 0, m_TitleHeight, wr.Width(), wr.Height() );
newrgn.CombineRgn( &rgn, CRgn::FromHandle(hrgn), RGN_XOR );
return (HRGN)newrgn.Detach();
}
else
rgn.CreateRectRgn( 0, 0, wr.Width(), wr.Height() );
return (HRGN)rgn.Detach();
}
BOOL CSkin::OnNcActivate(BOOL bActive)
{
m_bActive = bActive;
OnNcPaint(0);
if ( !bActive )
{
return 1;
}
else
return 0;
}
//当用户鼠标在对话框标题栏上单击左键不放时,移动鼠标,可以改变对话框位置
//此时调用此函数,
UINT CSkin::OnNcHitTest(CPoint point)
{
CWnd *pWnd = CWnd::FromHandle(m_hWnd);
CRect wr;
pWnd->GetWindowRect(wr);
point.x -= wr.left;
point.y -= wr.top;
if ( PtInRect( GetButtonRect(0), point ) )
return HTCLOSE;
if ( PtInRect( GetButtonRect(2), point ) && m_minable )
return HTMINBUTTON;
if ( PtInRect( GetButtonRect(1), point ) && m_maxable )
return HTMAXBUTTON;
CRect r;
int cx = GetSystemMetrics(SM_CXSMICON);
int cy = GetSystemMetrics(SM_CYSMICON);
if ( PtInRect( CRect(m_BorderLeftWidth, 5, m_BorderLeftWidth + cx, cy + 5), point ) )
return HTSYSMENU;
r = CRect( 0, 0, m_BorderLeftWidth, m_TitleHeight );
if ( PtInRect( r, point ) && m_sizable && m_winstate != 1 ) //!IsZoomed(m_hWnd) )
return HTTOPLEFT;
r = CRect( wr.Width() - m_BorderLeftWidth, 0, wr.Width(), m_TitleHeight );
if ( PtInRect( r, point ) && m_sizable && m_winstate != 1 ) //!IsZoomed(m_hWnd) )
return HTTOPRIGHT;
r = CRect( 0, wr.Height() - m_BorderBottomHeight, m_BorderLeftWidth, wr.Height() );
if ( PtInRect( r, point ) && m_sizable && m_winstate != 1 ) //!IsZoomed(m_hWnd) )
return HTBOTTOMLEFT;
r = CRect( wr.Width()-m_BorderRightWidth, wr.Height() - m_BorderBottomHeight, wr.Width(), wr.Height() );
if ( PtInRect( r, point ) && m_sizable && m_winstate != 1 ) //!IsZoomed(m_hWnd) )
return HTBOTTOMRIGHT;
r = CRect( 0, m_TitleHeight, m_BorderLeftWidth, wr.Height() - m_BorderBottomHeight );
if ( PtInRect( r, point ) && m_sizable && m_winstate != 1 ) //!IsZoomed(m_hWnd) )
return HTLEFT;
r = CRect( wr.Width()-m_BorderRightWidth, m_TitleHeight, wr.Width(), wr.Height() - m_BorderBottomHeight );
if ( PtInRect( r, point ) && m_sizable && m_winstate != 1 ) //!IsZoomed(m_hWnd) )
return HTRIGHT;
r = CRect( m_BorderLeftWidth, wr.Height() - m_BorderBottomHeight, wr.Width()-m_BorderRightWidth, wr.Height() );
if ( PtInRect( r, point ) && m_sizable && m_winstate != 1 ) //!IsZoomed(m_hWnd) )
return HTBOTTOM;
r = CRect( m_BorderLeftWidth, 0, wr.Width()-m_BorderRightWidth, m_BorderBottomHeight );
if ( PtInRect( r, point ) && m_sizable && m_winstate != 1 ) //!IsZoomed(m_hWnd) )
return HTTOP;
//set to boder 5
r = CRect( m_BorderLeftWidth, 5 , wr.Width()-m_BorderRightWidth, m_TitleHeight );
if ( PtInRect( r, point ) )
return HTCAPTION;
return HTCLIENT;
}
void CSkin::OnNcLButtonDblClk(UINT nHitTest, CPoint point)
{
if ( nHitTest == HTCAPTION && m_sizable )
{
if ( m_winstate == 1 )
Restore();
else
Maximize(); //ShowWindow(m_hWnd, SW_MAXIMIZE);
m_downHitTest = 0;
m_moveHitTest = 0;
OnNcPaint(0);
}
}
void CSkin::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
m_downHitTest = nHitTest;
m_moveHitTest = m_downHitTest;
OnNcPaint(0);
if ( nHitTest >= HTLEFT && nHitTest <= HTBOTTOMRIGHT ||
nHitTest == HTCAPTION && m_winstate != 1 ) //!IsZoomed(m_hWnd) )
Default();
else if ( nHitTest == HTSYSMENU )
{
PopupSysMenu(point);
/*
CWnd * pWnd = CWnd::FromHandle(m_hWnd);
//a tricky hack
pWnd->ModifyStyle( 0, WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX );
Default();
pWnd->ModifyStyle( WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, 0 );
*/
}
}
void CSkin::OnNcLButtonUp(UINT nHitTest, CPoint point)
{
if ( nHitTest == HTCLOSE )
{
SendMessage(m_hWnd, WM_CLOSE, 0, 0);
return;
}
else if ( nHitTest == HTMINBUTTON )
Minimize();
else if ( nHitTest == HTMAXBUTTON )
{
if ( m_winstate == 1 ) // IsZoomed(m_hWnd) )
Restore(); //ShowWindow(m_hWnd, SW_RESTORE);
else
Maximize(); //ShowWindow(m_hWnd, SW_MAXIMIZE);
}
else
return;
m_downHitTest = 0;
m_moveHitTest = 0;
OnNcPaint(0);
}
void CSkin::OnNcMouseMove(UINT nHitTest, CPoint point)
{
if ( nHitTest >= HTLEFT && nHitTest <= HTBOTTOMRIGHT ||
nHitTest == HTCAPTION && m_winstate != 1 ) //!IsZoomed(m_hWnd) )
Default();
m_moveHitTest = nHitTest;
m_downHitTest = 0;
if ( m_oldHitTest != nHitTest )
{
/*
CRect r;
r = GetButtonRect(0);
r.UnionRect( r, GetButtonRect(1) );
r.UnionRect( r, GetButtonRect(2) );
CRgn rgn;
rgn.CreateRectRgn( r.left, r.top, r.right, r.bottom );
*/
OnNcPaint(0);
m_oldHitTest = nHitTest;
}
}
void CSkin::OnNcRButtonDown(UINT nHitTest, CPoint point)
{
if ( nHitTest == HTCAPTION )
{
/*
CWnd * pWnd = CWnd::FromHandle(m_hWnd);
pWnd->ModifyStyle( 0, WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX );
Default();
pWnd->ModifyStyle( WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, 0 );
*/
PopupSysMenu(point);
}
}
void CSkin::OnNcRButtonUp(UINT nHitTest, CPoint point)
{
}
CRect CSkin::GetButtonRect(int i)
{
CWnd *pWnd = CWnd::FromHandle(m_hWnd);
CRect wr;
pWnd->GetWindowRect(wr);
CRect r;
if ( i == 0 && m_bmpCloseBtn.GetSafeHandle() )
{
//close
r = m_rectCloseBtn;
r.left = wr.Width() - m_rectCloseBtn.right;
r.right = wr.Width() - m_rectCloseBtn.left;
}
if ( i == 1 && m_bmpMaxBtn.GetSafeHandle())
{
//max
if ( m_winstate != 1 || !m_bmpRestoreBtn.GetSafeHandle() )
{
r = m_rectMaxBtn;
r.left = wr.Width() - m_rectMaxBtn.right;
r.right = wr.Width() - m_rectMaxBtn.left;
}
else
{
r = m_rectRestoreBtn;
r.left = wr.Width() - m_rectRestoreBtn.right;
r.right = wr.Width() - m_rectRestoreBtn.left;
}
}
if ( i == 2 && m_bmpMinBtn.GetSafeHandle())
{
//min
r = m_rectMinBtn;
r.left = wr.Width() - m_rectMinBtn.right;
r.right = wr.Width() - m_rectMinBtn.left;
}
return r;
}
BOOL CSkin::DrawButton( CDC * pDC, int i, int state)
{
if ( i == 0 )
{
if ( m_bmpCloseBtn.GetSafeHandle() )
{
CRect r = GetButtonRect(0);
m_bmpCloseBtn.Draw( pDC, r.left, r.top,
CRect( state * r.Width(), 0, (state + 1 ) * r.Width(), m_bmpCloseBtn.Height() ), m_colTrans, m_bTrans );
}
}
if ( i == 1 )
{
CRect r = GetButtonRect(1);
if ( m_maxable && m_bmpMaxBtn.GetSafeHandle() )
if ( m_winstate == 1&& m_bmpRestoreBtn.GetSafeHandle() )
m_bmpRestoreBtn.Draw( pDC, r.left, r.top,
CRect( state * r.Width(), 0, (state + 1 ) * r.Width(), m_bmpCloseBtn.Height() ), m_colTrans, m_bTrans );
else
m_bmpMaxBtn.Draw( pDC, r.left, r.top,
CRect( state * r.Width(), 0, (state + 1 ) * r.Width(), m_bmpCloseBtn.Height() ) , m_colTrans, m_bTrans);
}
if ( i == 2 )
{
if ( m_minable && m_bmpMinBtn.GetSafeHandle() )
{
CRect r = GetButtonRect(2);
m_bmpMinBtn.Draw( pDC, r.left, r.top,
CRect( state * r.Width(), 0, (state + 1 ) * r.Width(), m_bmpCloseBtn.Height() ) , m_colTrans, m_bTrans);
}
}
return TRUE;
}
void CSkin::OnGetMinMaxInfo(MINMAXINFO *lpMMI)
{
lpMMI->ptMinTrackSize = CPoint( m_bmpTitle.Width() + m_bmpLeft.Width() +
m_bmpRight.Width(), m_bmpLeft.Height() + 20 );
}
void CSkin::OnWindowPosChanging(WINDOWPOS *lpwndpos)
{
}
BOOL CSkin::OnEraseBkgnd(CDC *pDC)
{
if ( m_bmpDlg.GetSafeHandle() )
{
CRect r;
GetClientRect( m_hWnd, r);
for ( int i = 0; i < r.Width()/m_bmpDlg.Width() + 1; i++ )
for ( int j = 0; j < r.Height()/m_bmpDlg.Height() + 1; j++ )
{
m_bmpDlg.Draw( pDC, CRect( i * m_bmpDlg.Width(), j * m_bmpDlg.Height(),
i * m_bmpDlg.Width() + m_bmpDlg.Width(), j * m_bmpDlg.Height() + m_bmpDlg.Height() ) );
}
return TRUE;
}
else
return FALSE;
}
CRect CSkin::GetMaximizeRect()
{
CRect r;
SystemParametersInfo(SPI_GETWORKAREA, 0, r, 0);
return r;
}
BOOL CSkin::Maximize()
{
CRect r = GetMaximizeRect();
GetWindowRect( m_hWnd, m_rectRestoreWin );
m_winstate = 1;
::MoveWindow( m_hWnd, r.left, r.top, r.Width(), r.Height(), TRUE );
UpdateWindow( m_hWnd );
return TRUE;
}
BOOL CSkin::Minimize()
{
m_oldwinstate = m_winstate;
m_winstate = 2;
ShowWindow(m_hWnd, SW_MINIMIZE);
return TRUE;
}
BOOL CSkin::Restore()
{
if ( m_winstate == 1 )
{
::MoveWindow( m_hWnd, m_rectRestoreWin.left, m_rectRestoreWin.top,
m_rectRestoreWin.Width(), m_rectRestoreWin.Height(), TRUE );
m_winstate = 0;
UpdateWindow( m_hWnd );
}
return TRUE;
}
//void OnActivate(UINT nState,CWnd* pWndOther, BOOL bMinimized );
void CSkin::OnActivate(UINT nState, CWnd *pWndOther, BOOL bMinimized)
{
//Default();
m_bActive = ( nState == WA_ACTIVE || nState == WA_CLICKACTIVE );
OnNcActivate(m_bActive);
}
LRESULT CSkin::OnSetText(WPARAM wp, LPARAM lp)
{
char * str = ( char *)lp;
Default();
m_title = str;
OnNcPaint(0);
return 0;
}
void CSkin::OnSysCommand(UINT nID, LPARAM lParam)
{
if ( nID == SC_MAXIMIZE )
Maximize();
else if ( nID == SC_RESTORE && m_winstate == 1 )
Restore();
else if ( nID == SC_RESTORE && m_winstate == 2 )
{
ShowWindow( m_hWnd, SW_RESTORE );
m_winstate = m_oldwinstate;
OnNcPaint(0);
}
else
Default();
}
BOOL CSkin::PopupSysMenu(CPoint point)
{
CWnd * pWnd = CWnd::FromHandle(m_hWnd);
CMenu SysMenu;
SysMenu.Attach( GetSystemMenu(m_hWnd, FALSE) );
/*
when gray menuitem, a strange line appears!!
if ( m_winstate == 0 )
{
SysMenu.EnableMenuItem( SC_MINIMIZE, MF_BYCOMMAND|MF_ENABLED );
SysMenu.EnableMenuItem( SC_MAXIMIZE, MF_BYCOMMAND|MF_ENABLED );
SysMenu.EnableMenuItem( SC_RESTORE, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED );
}
else if ( m_winstate == 1 )
{
SysMenu.EnableMenuItem( SC_MAXIMIZE, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED );
SysMenu.EnableMenuItem( SC_RESTORE, MF_BYCOMMAND|MF_ENABLED );
}
else if ( m_winstate == 2 )
{
SysMenu.EnableMenuItem( SC_MINIMIZE, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED );
SysMenu.EnableMenuItem( SC_RESTORE, MF_BYCOMMAND|MF_ENABLED );
}
*/
SysMenu.TrackPopupMenu( 0, point.x, point.y, pWnd );
SysMenu.Detach();
return TRUE;
}
BOOL CSkin::HandleSysCommand(WPARAM wp, LPARAM lp)
{
if ( lp == 0 )
{
CMenu SysMenu;
SysMenu.Attach( GetSystemMenu(m_hWnd, FALSE) );
int count = SysMenu.GetMenuItemCount();
for( int i = 0; i < count ; i++ )
if ( SysMenu.GetMenuItemID(i) == wp )
break;
SysMenu.Detach();
if ( i >= count )
return FALSE;
if ( wp == SC_MAXIMIZE )
Maximize();
else if ( wp == SC_MINIMIZE )
Minimize();
else if ( wp == SC_RESTORE && m_winstate == 1 )
Restore();
else if ( wp == SC_RESTORE && m_winstate == 2 )
{
ShowWindow( m_hWnd, SW_RESTORE );
m_winstate = m_oldwinstate;
OnNcPaint(0);
}
else
SendMessage( m_hWnd, WM_SYSCOMMAND, wp, lp );
return TRUE;
}
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -