📄 wingdi.cpp
字号:
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#ifdef AFX_CORE2_SEG
#pragma code_seg(AFX_CORE2_SEG)
#endif
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Diagnostic Output
#ifdef _DEBUG
CDumpContext& AFXAPI operator<<(CDumpContext& dc, SIZE size)
{
return dc << "(" << size.cx << " x " << size.cy << ")";
}
CDumpContext& AFXAPI operator<<(CDumpContext& dc, POINT point)
{
return dc << "(" << point.x << ", " << point.y << ")";
}
CDumpContext& AFXAPI operator<<(CDumpContext& dc, const RECT& rect)
{
return dc << "(L " << rect.left << ", T " << rect.top << ", R " <<
rect.right << ", B " << rect.bottom << ")";
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDC
CDC::CDC()
{
m_hDC = NULL;
m_hAttribDC = NULL;
m_bPrinting = FALSE;
}
#ifdef _DEBUG
void CDC::AssertValid() const
{
CObject::AssertValid();
}
void CDC::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
dc << "m_hDC = " << (UINT)m_hDC;
dc << "\nm_hAttribDC = " << (UINT)m_hAttribDC;
dc << "\nm_bPrinting = " << m_bPrinting;
dc << "\n";
}
#endif //_DEBUG
#include "fixalloc.h"
class CTempDC : public CDC
{
DECLARE_DYNCREATE(CTempDC)
DECLARE_FIXED_ALLOC(CTempDC);
};
CHandleMap* PASCAL afxMapHDC(BOOL bCreate)
{
AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
if (pState->m_pmapHDC == NULL && bCreate)
{
BOOL bEnable = AfxEnableMemoryTracking(FALSE);
#ifndef _AFX_PORTABLE
_PNH pnhOldHandler = AfxSetNewHandler(&AfxCriticalNewHandler);
#endif
pState->m_pmapHDC = new CHandleMap(RUNTIME_CLASS(CTempDC),
offsetof(CDC, m_hDC), 2);
#ifndef _AFX_PORTABLE
AfxSetNewHandler(pnhOldHandler);
#endif
AfxEnableMemoryTracking(bEnable);
}
return pState->m_pmapHDC;
}
CDC* PASCAL CDC::FromHandle(HDC hDC)
{
CHandleMap* pMap = afxMapHDC(TRUE); //create map if not exist
ASSERT(pMap != NULL);
CDC* pDC = (CDC*)pMap->FromHandle(hDC);
ASSERT(pDC == NULL || pDC->m_hDC == hDC);
return pDC;
}
BOOL CDC::Attach(HDC hDC)
{
ASSERT(m_hDC == NULL); // only attach once, detach on destroy
ASSERT(m_hAttribDC == NULL); // only attach to an empty DC
if (hDC == NULL)
return FALSE;
CHandleMap* pMap = afxMapHDC(TRUE); // create map if not exist
ASSERT(pMap != NULL);
pMap->SetPermanent(m_hDC = hDC, this);
SetAttribDC(m_hDC); // Default to same as output
return TRUE;
}
HDC CDC::Detach()
{
HDC hDC = m_hDC;
if (hDC != NULL)
{
CHandleMap* pMap = afxMapHDC(); // don't create if not exist
if (pMap != NULL)
pMap->RemoveHandle(m_hDC);
}
ReleaseAttribDC();
m_hDC = NULL;
return hDC;
}
BOOL CDC::DeleteDC()
{
if (m_hDC == NULL)
return FALSE;
return ::DeleteDC(Detach());
}
CDC::~CDC()
{
if (m_hDC != NULL)
::DeleteDC(Detach());
}
void CDC::SetAttribDC(HDC hDC) // Set the Attribute DC
{
m_hAttribDC = hDC;
}
void CDC::SetOutputDC(HDC hDC) // Set the Output DC
{
#ifdef _DEBUG
CHandleMap* pMap = afxMapHDC();
if (pMap != NULL && pMap->LookupPermanent(m_hDC) == this)
{
TRACE0("Cannot Set Output hDC on Attached CDC.\n");
ASSERT(FALSE);
}
#endif
m_hDC = hDC;
}
void CDC::ReleaseAttribDC() // Release the Attribute DC
{
m_hAttribDC = NULL;
}
void CDC::ReleaseOutputDC() // Release the Output DC
{
#ifdef _DEBUG
CHandleMap* pMap = afxMapHDC();
if (pMap != NULL && pMap->LookupPermanent(m_hDC) == this)
{
TRACE0("Cannot Release Output hDC on Attached CDC.\n");
ASSERT(FALSE);
}
#endif
m_hDC = NULL;
}
/////////////////////////////////////////////////////////////////////////////
// Out-of-line routines
int CDC::StartDoc(LPCTSTR lpszDocName)
{
DOCINFO di;
memset(&di, 0, sizeof(DOCINFO));
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = lpszDocName;
return StartDoc(&di);
}
int CDC::SaveDC()
{
ASSERT(m_hDC != NULL);
int nRetVal = 0;
if (m_hAttribDC != NULL)
nRetVal = ::SaveDC(m_hAttribDC);
if (m_hDC != m_hAttribDC && ::SaveDC(m_hDC) != 0)
nRetVal = -1; // -1 is the only valid restore value for complex DCs
return nRetVal;
}
BOOL CDC::RestoreDC(int nSavedDC)
{
// if two distinct DCs, nSavedDC can only be -1
ASSERT(m_hDC != NULL);
ASSERT(m_hDC == m_hAttribDC || nSavedDC == -1);
BOOL bRetVal = TRUE;
if (m_hDC != m_hAttribDC)
bRetVal = ::RestoreDC(m_hDC, nSavedDC);
if (m_hAttribDC != NULL)
bRetVal = (bRetVal && ::RestoreDC(m_hAttribDC, nSavedDC));
return bRetVal;
}
CGdiObject* PASCAL CDC::SelectGdiObject(HDC hDC, HGDIOBJ h)
{
return CGdiObject::FromHandle(::SelectObject(hDC, h));
}
CGdiObject* CDC::SelectStockObject(int nIndex)
{
ASSERT(m_hDC != NULL);
HGDIOBJ hObject = ::GetStockObject(nIndex);
HGDIOBJ hOldObj = NULL;
ASSERT(hObject != NULL);
if (m_hDC != m_hAttribDC)
hOldObj = ::SelectObject(m_hDC, hObject);
if (m_hAttribDC != NULL)
hOldObj = ::SelectObject(m_hAttribDC, hObject);
return CGdiObject::FromHandle(hOldObj);
}
CPen* CDC::SelectObject(CPen* pPen)
{
ASSERT(m_hDC != NULL);
HGDIOBJ hOldObj = NULL;
if (m_hDC != m_hAttribDC)
hOldObj = ::SelectObject(m_hDC, pPen->GetSafeHandle());
if (m_hAttribDC != NULL)
hOldObj = ::SelectObject(m_hAttribDC, pPen->GetSafeHandle());
return (CPen*)CGdiObject::FromHandle(hOldObj);
}
CBrush* CDC::SelectObject(CBrush* pBrush)
{
ASSERT(m_hDC != NULL);
HGDIOBJ hOldObj = NULL;
if (m_hDC != m_hAttribDC)
hOldObj = ::SelectObject(m_hDC, pBrush->GetSafeHandle());
if (m_hAttribDC != NULL)
hOldObj = ::SelectObject(m_hAttribDC, pBrush->GetSafeHandle());
return (CBrush*)CGdiObject::FromHandle(hOldObj);
}
CFont* CDC::SelectObject(CFont* pFont)
{
ASSERT(m_hDC != NULL);
HGDIOBJ hOldObj = NULL;
if (m_hDC != m_hAttribDC)
hOldObj = ::SelectObject(m_hDC, pFont->GetSafeHandle());
if (m_hAttribDC != NULL)
hOldObj = ::SelectObject(m_hAttribDC, pFont->GetSafeHandle());
return (CFont*)CGdiObject::FromHandle(hOldObj);
}
int CDC::SelectObject(CRgn* pRgn)
{
ASSERT(m_hDC != NULL);
int nRetVal = GDI_ERROR;
if (m_hDC != m_hAttribDC)
nRetVal = (int)::SelectObject(m_hDC, pRgn->GetSafeHandle());
if (m_hAttribDC != NULL)
nRetVal = (int)::SelectObject(m_hAttribDC, pRgn->GetSafeHandle());
return nRetVal;
}
CPalette* CDC::SelectPalette(CPalette* pPalette, BOOL bForceBackground)
{
ASSERT(m_hDC != NULL);
return (CPalette*) CGdiObject::FromHandle(::SelectPalette(m_hDC,
(HPALETTE)pPalette->GetSafeHandle(), bForceBackground));
}
COLORREF CDC::SetBkColor(COLORREF crColor)
{
ASSERT(m_hDC != NULL);
COLORREF crRetVal = CLR_INVALID;
if (m_hDC != m_hAttribDC)
crRetVal = ::SetBkColor(m_hDC, crColor);
if (m_hAttribDC != NULL)
crRetVal = ::SetBkColor(m_hAttribDC, crColor);
return crRetVal;
}
int CDC::SetBkMode(int nBkMode)
{
ASSERT(m_hDC != NULL);
int nRetVal = 0;
if (m_hDC != m_hAttribDC)
nRetVal = ::SetBkMode(m_hDC, nBkMode);
if (m_hAttribDC != NULL)
nRetVal = ::SetBkMode(m_hAttribDC, nBkMode);
return nRetVal;
}
int CDC::SetPolyFillMode(int nPolyFillMode)
{
ASSERT(m_hDC != NULL);
int nRetVal = 0;
if (m_hDC != m_hAttribDC)
nRetVal = ::SetPolyFillMode(m_hDC, nPolyFillMode);
if (m_hAttribDC != NULL)
nRetVal = ::SetPolyFillMode(m_hAttribDC, nPolyFillMode);
return nRetVal;
}
int CDC::SetROP2(int nDrawMode)
{
ASSERT(m_hDC != NULL);
int nRetVal = 0;
if (m_hDC != m_hAttribDC)
nRetVal = ::SetROP2(m_hDC, nDrawMode);
if (m_hAttribDC != NULL)
nRetVal = ::SetROP2(m_hAttribDC, nDrawMode);
return nRetVal;
}
int CDC::SetStretchBltMode(int nStretchMode)
{
ASSERT(m_hDC != NULL);
int nRetVal = 0;
if (m_hDC != m_hAttribDC)
nRetVal = ::SetStretchBltMode(m_hDC, nStretchMode);
if (m_hAttribDC != NULL)
nRetVal = ::SetStretchBltMode(m_hAttribDC, nStretchMode);
return nRetVal;
}
COLORREF CDC::SetTextColor(COLORREF crColor)
{
ASSERT(m_hDC != NULL);
COLORREF crRetVal = CLR_INVALID;
if (m_hDC != m_hAttribDC)
crRetVal = ::SetTextColor(m_hDC, crColor);
if (m_hAttribDC != NULL)
crRetVal = ::SetTextColor(m_hAttribDC, crColor);
return crRetVal;
}
int CDC::SetMapMode(int nMapMode)
{
ASSERT(m_hDC != NULL);
int nRetVal = 0;
if (m_hDC != m_hAttribDC)
nRetVal = ::SetMapMode(m_hDC, nMapMode);
if (m_hAttribDC != NULL)
nRetVal = ::SetMapMode(m_hAttribDC, nMapMode);
return nRetVal;
}
CPoint CDC::SetViewportOrg(int x, int y)
{
ASSERT(m_hDC != NULL);
CPoint point;
if (m_hDC != m_hAttribDC)
VERIFY(::SetViewportOrgEx(m_hDC, x, y, &point));
if (m_hAttribDC != NULL)
VERIFY(::SetViewportOrgEx(m_hAttribDC, x, y, &point));
return point;
}
CPoint CDC::OffsetViewportOrg(int nWidth, int nHeight)
{
ASSERT(m_hDC != NULL);
CPoint point;
if (m_hDC != m_hAttribDC)
VERIFY(::OffsetViewportOrgEx(m_hDC, nWidth, nHeight, &point));
if (m_hAttribDC != NULL)
VERIFY(::OffsetViewportOrgEx(m_hAttribDC, nWidth, nHeight, &point));
return point;
}
CSize CDC::SetViewportExt(int x, int y)
{
ASSERT(m_hDC != NULL);
CSize size;
if (m_hDC != m_hAttribDC)
VERIFY(::SetViewportExtEx(m_hDC, x, y, &size));
if (m_hAttribDC != NULL)
VERIFY(::SetViewportExtEx(m_hAttribDC, x, y, &size));
return size;
}
CSize CDC::ScaleViewportExt(int xNum, int xDenom, int yNum, int yDenom)
{
ASSERT(m_hDC != NULL);
CSize size;
if (m_hDC != m_hAttribDC)
VERIFY(::ScaleViewportExtEx(m_hDC, xNum, xDenom, yNum, yDenom, &size));
if (m_hAttribDC != NULL)
VERIFY(::ScaleViewportExtEx(m_hAttribDC, xNum, xDenom, yNum, yDenom, &size));
return size;
}
CPoint CDC::SetWindowOrg(int x, int y)
{
ASSERT(m_hDC != NULL);
CPoint point;
if (m_hDC != m_hAttribDC)
VERIFY(::SetWindowOrgEx(m_hDC, x, y, &point));
if (m_hAttribDC != NULL)
VERIFY(::SetWindowOrgEx(m_hAttribDC, x, y, &point));
return point;
}
CPoint CDC::OffsetWindowOrg(int nWidth, int nHeight)
{
ASSERT(m_hDC != NULL);
CPoint point;
if (m_hDC != m_hAttribDC)
VERIFY(::OffsetWindowOrgEx(m_hDC, nWidth, nHeight, &point));
if (m_hAttribDC != NULL)
VERIFY(::OffsetWindowOrgEx(m_hAttribDC, nWidth, nHeight, &point));
return point;
}
CSize CDC::SetWindowExt(int x, int y)
{
ASSERT(m_hDC != NULL);
CSize size;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -