📄 sxbutton.cpp
字号:
}
// Get control text
CString strTitle;
this->GetWindowText(strTitle);
//
// Draw Image
//
if( HasImage() )
{
CPoint pt;
if( m_bUseOffset )
{
pt.x = strTitle.IsEmpty() ? SXBUTTON_CENTER : rectControl.left + m_nImageOffsetFromBorder;
pt.y = SXBUTTON_CENTER;
}
else
pt = m_pointImage;
CheckPointForCentering( pt, m_nImageWidth, m_nImageHeight );
pt.Offset( nOffset, nOffset );
if( m_hIcon )
m_pDC->DrawState( pt, CSize(m_nImageWidth, m_nImageHeight), (HICON)m_hIcon, DST_ICON | nStateFlag, (CBrush *)NULL );
else if( m_hBitmap )
m_pDC->DrawState( pt, CSize(m_nImageWidth, m_nImageHeight), (HBITMAP)hBitmapToDraw, DST_BITMAP | nStateFlag );
}
//
// Draw Text
//
if ( !strTitle.IsEmpty() )
{
CPoint pt;
CSize sizeText = m_pDC->GetTextExtent(strTitle);
if( m_bUseOffset )
{
pt.x = !HasImage() ? SXBUTTON_CENTER : m_nImageWidth + m_nTextOffsetFromImage + m_nImageOffsetFromBorder;
pt.y = SXBUTTON_CENTER;
}
else
pt = m_pointText;
// If we are centering the text vertically, it looks best of we
// center based on the height of the text, then move it up 1 more pixel
int nOffsetFixY = pt.y == SXBUTTON_CENTER ? -1 : 0;
CheckPointForCentering( pt, sizeText.cx, sizeText.cy );
pt.Offset( nOffset, nOffset + nOffsetFixY );
m_pDC->DrawState( pt, CSize(0,0), strTitle, DST_PREFIXTEXT|nStateFlag, TRUE, 0, (CBrush*)NULL );
}
} // End !focus only
//
// Draw focus rectange
//
if( !( nNewState & ODS_DISABLED ) ) // Make sure it's not disabled
{
// Redraw the focus if:
// 1. There is a change in focus state
// OR 2. The entire control was just redrawn and Focus is set
if( ( nNewState && ODS_FOCUS ) ^ ( m_nOldState && ODS_FOCUS ) ||
( !bDRAWFOCUSONLY && ( nNewState & ODS_FOCUS ) ) )
{
#define FOCUSOFFSET 3
CRect rect( rectControl );
// As control gets smaller, decrease focus size
int nDeflate = min( FOCUSOFFSET,
min( rect.Width(), rect.Height() ) >> 2 );
rect.DeflateRect( nDeflate, nDeflate);
m_pDC->DrawFocusRect(&rect);
}
}
m_nOldAction = nNewAction;
m_nOldState = nNewState;
m_pDC = NULL;
}
void CSXButton::DrawPlainBitmap()
{
if( m_hBitmap ) {
BITMAP bm;
int height,height2,width,width2;
GetObject( m_hBitmap,sizeof(BITMAP),&bm);
CPoint size(bm.bmWidth,bm.bmHeight);
HDC dcmem = CreateCompatibleDC(m_pDC->m_hDC);
HBITMAP old = (HBITMAP)SelectObject(dcmem,m_hBitmap);
if (bm.bmWidth > bm.bmHeight)
{
height = m_rcItem.bottom - m_rcItem.top;
float ratio = (float)bm.bmHeight/(float)bm.bmWidth;
m_rcItem.bottom = (long) (m_rcItem.top + (m_rcItem.right-m_rcItem.left)*ratio);
height2 = (height - (m_rcItem.bottom - m_rcItem.top))/2;
m_rcItem.top += height2;
m_rcItem.bottom += height2;
}
else
{
width = m_rcItem.right - m_rcItem.left;
float ratio = (float)bm.bmWidth/(float)bm.bmHeight;
m_rcItem.right = (long) (m_rcItem.left + (m_rcItem.bottom-m_rcItem.top)*ratio);
width2 = (width - (m_rcItem.right - m_rcItem.left))/2;
m_rcItem.left += width2;
m_rcItem.right += width2;
}
CRect r = m_rcItem;
r.DeflateRect( FRAME_BORDER, FRAME_BORDER );
StretchBlt( m_pDC->m_hDC,
r.left,r.top,
r.right-r.left,
r.bottom-r.top,
dcmem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
SelectObject(dcmem,old);
// draw crosshair at hotspot if there is one.
if( m_bHotSpot ) {
if( SetPen( PS_SOLID, 3, RGB(255,0,0)) ) {
#define _P1 2
#define _P2 8
int HX = m_nHotX * 2 + 5;
int HY = m_nHotY * 2 + 5;
DrawLine( HX-_P1, HY, HX-_P2, HY );
DrawLine( HX+_P1, HY, HX+_P2, HY );
DrawLine( HX, HY-_P1, HX, HY-_P2 );
DrawLine( HX, HY+_P1, HX, HY+_P2 );
ResetPen();
}
}
if( m_bWantBackground ) {
m_clrBackground = ::GetPixel( m_pDC->m_hDC, r.left,r.top );
}
DeleteDC(dcmem);
}
}
void CSXButton::DrawGraphicBitmap()
{
if( m_hBitmap == NULL )
return;
BITMAP bm;
int height,height2,width,width2;
PaintBkgnd();
GetObject( m_hBitmap,sizeof(BITMAP),&bm);
CPoint size(bm.bmWidth,bm.bmHeight);
HDC dcmem = CreateCompatibleDC(m_pDC->m_hDC);
HBITMAP old = (HBITMAP)SelectObject(dcmem,m_hBitmap);
// start from the given width and scale to the multipler
width = bm.bmWidth;
height = bm.bmHeight;
width2 = bm.bmWidth * m_nScale;
height2 = bm.bmHeight* m_nScale;
CRect r = m_rcItem;
r.DeflateRect( FRAME_BORDER, FRAME_BORDER );
// determine the offset so we can center bitmap
m_nOffsetX = (r.right - width2) / 2;
m_nOffsetY = (r.bottom - height2) / 2;
m_nVHeight = (r.bottom - r.top);
m_nVWidth = (r.right - r.left);
m_nHeight = height;
m_nWidth = width;
m_nSHeight = height2;
m_nSWidth = width2;
StretchBlt( m_pDC->m_hDC,
r.left+m_nOffsetX,r.top+m_nOffsetY,
r.left + width2,
r.top + height2,
dcmem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
SelectObject(dcmem,old);
DeleteDC(dcmem);
// get the current information into the local blitmap buffer.
GetLines();
if( m_bShowBoxes )
DrawBoxes();
if( m_bShowLines )
DrawLines();
if( m_bShowDimensions )
DrawDimensions();
if( m_bShowMagnets )
DrawMagnets();
if( m_bIsCaptured )
DrawLocation();
}
#if 0 // old 1/22/01
void CSXButton::DrawFontText(void)
{
// we don't have the font to support drawing this..
if( m_bFontExists == FALSE ) {
CRect r = m_rcItem;
CBrush br( m_clrBack );
r.DeflateRect( 1,1 );
// fill the entire rectangle first.
m_pDC->FillRect( &r, &br );
return;
}
// create bitmap if we don't have it..
if( m_hBitmap == NULL ) {
BITMAP bm;
m_hBitmap = CGUIBuilderDoc::GetFontBitmap( m_pDC, m_strFont, m_strText, m_clrFore, m_clrBack );
// if font or other doesn't work - leave now.
if( m_hBitmap == 0 )
return;
GetObject(m_hBitmap,sizeof(BITMAP),&bm);
m_nImageWidth = bm.bmWidth;
m_nImageHeight = bm.bmHeight;
}
HDC hmemDC = CreateCompatibleDC( m_pDC->m_hDC );
HBITMAP holdBitmap = (HBITMAP)::SelectObject( hmemDC, m_hBitmap );
CRect r = m_rcItem;
int x,y,w,h;
r.DeflateRect( 1,1 );
x = r.left;
y = r.top;
w = r.right - x;
h = r.bottom - y;
if( w > m_nImageWidth )
w = m_nImageWidth;
if( h > m_nImageHeight )
h = m_nImageHeight;
CBrush br( m_clrBack );
// fill the entire rectangle first.
m_pDC->FillRect( &r, &br );
SetTextColor( m_pDC->m_hDC, m_clrFore );
SetBkColor( m_pDC->m_hDC, m_clrBack );
BitBlt( m_pDC->m_hDC, x,y,w,h, hmemDC, 0,0, SRCCOPY );
SelectObject( hmemDC, holdBitmap );
DeleteDC(hmemDC);
}
#endif
void CSXButton::DrawFontText(void)
{
}
void CSXButton::DrawFontHelper(void)
{
}
void CSXButton::OnLButtonDown(UINT nFlags, CPoint point)
{
BOOL bTry = m_bGraphic && m_bEdit;
BOOL bLine, bMagnet;
m_ptLast = point;
if( bTry ) {
bLine = bMagnet = FALSE;
// if the magnets are up, only select magnets.
if( m_bShowMagnets )
bMagnet = FindMagnet( point );
else
bLine = FindLine( point );
}
if( bTry && (bLine || bMagnet) )
{
SetCapture();
m_bIsCaptured = (GetCapture() == this);
m_bIsCaptureLine= bLine;
m_ptDown = point;
// force the numbers to calculate and showup
if( m_bIsCaptureLine )
MoveLine( point );
else
MoveMagnet( point );
} else if( !m_bGraphic )
CButton::OnLButtonDown(nFlags, point);
}
void CSXButton::OnMouseMove(UINT nFlags, CPoint point)
{
if( m_bIsCaptured )
{
if( m_bIsCaptureLine )
MoveLine( point );
else
MoveMagnet( point );
} else if( !m_bGraphic )
CButton::OnMouseMove(nFlags, point);
if( m_pWnd ) {
CString strTitle;
m_pWnd->GetWindowText( strTitle );
if( strTitle != m_strTitle )
m_pWnd->SetWindowText( (LPCSTR)m_strTitle );
}
}
void CSXButton::OnLButtonUp(UINT nFlags, CPoint point)
{
m_ptLast = point;
if( m_bIsCaptured ) {
ReleaseCapture();
if( m_bIsCaptureLine ) {
MoveLine( point );
// if there are magnets, determine if they are good or bad now.
ValidateAllMagnets();
} else {
MoveMagnet( point );
MoveMagnetDone();
}
// tell the parent window that there is a change.
GetParent()->SendMessage( WM_LBUTTONUP, 0, 0 );
m_bIsCaptured = FALSE;
} else if( !m_bGraphic )
CButton::OnLButtonUp(nFlags, point);
}
void CSXButton::SetHotSpot( const BOOL bEnable, int nX, int nY )
{
m_bHotSpot = bEnable;
m_nHotX = nX; // we have to recalculate this when rendering.
m_nHotY = nY;
Redraw();
}
BOOL CSXButton::SetPen( const int nPenStyle, const int nWidth, COLORREF crColor )
{
ASSERT( m_pCurPen == NULL );
ASSERT( m_pOldPen == NULL );
m_pCurPen = new CPen(nPenStyle, nWidth, crColor );
if( m_pCurPen )
{
m_pOldPen = m_pDC->SelectObject( m_pCurPen );
}
return !!m_pCurPen;
}
void CSXButton::ResetPen(void)
{
if( m_pOldPen )
{
(void*)m_pDC->SelectObject( m_pOldPen );
delete m_pCurPen;
m_pCurPen = NULL;
m_pOldPen = NULL;
}
}
void CSXButton::DrawLine( const int nX, const int nY, const int nX2, const int nY2 )
{
m_pDC->MoveTo( nX, nY );
m_pDC->LineTo( nX2, nY2 );
}
void CSXButton::DrawOffset( const int nX, const int nY, const int nX2, const int nY2,
BOOL bVertical, int nIndex, int nLineIndex, int nOffset )
{
CString strMsg;
int nAmount = nIndex * OFFSET;
BOOL bOdd = (nIndex & 1);
CBrush colorbrush;
CBrush *pOldBrush;
CRect r;
COLORREF color = RGB(192,192,192);
POINT pts[4];
int dx, dy;
if( bVertical ) {
if( bOdd ) {
r.left = nX - MOFFSET;
r.right = nX;
} else {
r.left = nX;
r.right = nX + MOFFSET;
}
r.top = FRAME_BORDER + 10 + nAmount;
r.bottom= r.top + MOFFSET;
} else {
r.left = FRAME_BORDER + 10 + nAmount;
r.right = r.left + MOFFSET;
if( bOdd ) {
r.top = nY - MOFFSET;
r.bottom= nY;
} else {
r.top = nY;
r.bottom= nY + MOFFSET;
}
}
dx = r.Width() / 2;
dy = r.Height() / 2;
pts[0].x = r.left + dx;
pts[0].y = r.top + dy;
pts[3] = pts[0];
if( bVertical ) {
if( bOdd ) {
pts[1].x = pts[0].x + dx;
pts[1].y = pts[0].y - MOFFSET;
pts[2].x = pts[0].x + dx;
pts[2].y = pts[0].y + MOFFSET;
} else {
pts[1].x = pts[0].x - dx;
pts[1].y = pts[0].y - MOFFSET;
pts[2].x = pts[0].x - dx;
pts[2].y = pts[0].y + MOFFSET;
}
} else {
if( bOdd ) {
pts[1].x = pts[0].x - MOFFSET;
pts[1].y = pts[0].y + dy;
pts[2].x = pts[0].x + MOFFSET;
pts[2].y = pts[0].y + dy;
} else {
pts[1].x = pts[0].x - MOFFSET;
pts[1].y = pts[0].y - dy;
pts[2].x = pts[0].x + MOFFSET;
pts[2].y = pts[0].y - dy;
}
}
strMsg.Format("%d", nOffset );
m_pDC->MoveTo( nX, nY );
m_pDC->LineTo( nX2, nY2 );
colorbrush.CreateSolidBrush( color );
pOldBrush = m_pDC->SelectObject( &colorbrush );
m_pDC->FillRect( &r, &colorbrush );
m_pDC->Polygon( pts, 4 );
m_pDC->DrawText(strMsg, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
m_pDC->SelectObject( pOldBrush );
m_rectHandles[nLineIndex] = r;
}
void CSXButton::DrawLines(void)
{
int i;
int x1, y1, x2, y2;
int xn, yn, cn;
LineInfo *pLI = &m_sLines[0];
if( m_nLines == 0 ) return;
xn = yn = 1;
for( i = 0; i < m_nLines; i++, pLI++ )
{
// is this a placeholder line?
if( pLI->nOffset <= PLACEHOLDER )
continue;
// determine the coordinates of the line in screen units.
if( pLI->bVertical )
{
x1 = m_nOffsetX + pLI->nOffset * m_nScale + FRAME_BORDER;
y1 = FRAME_BORDER;
x2 = x1;
y2 = m_nVHeight + FRAME_BORDER * 2;
cn = yn++;
} else
{
x1 = FRAME_BORDER;
y1 = m_nOffsetY + pLI->nOffset * m_nScale + FRAME_BORDER;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -