📄 printcontextview.cpp
字号:
// PrintContextView.cpp : implementation of the CPrintContextView class
//
#include "stdafx.h"
#include "PrintContext.h"
#include "PrintContextDoc.h"
#include "PrintContextView.h"
//add down
#include "gl\gl.h"
#include "gl\glu.h"
#include "gl\glaux.h"
//add up
//add down
unsigned char threeto8[8] =
{
0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377
};
unsigned char twoto8[4] =
{
0, 0x55, 0xaa, 0xff
};
unsigned char oneto8[2] =
{
0, 255
};
static int defaultOverride[13] =
{
0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91
};
static PALETTEENTRY defaultPalEntry[20] =
{
{ 0, 0, 0, 0 },
{ 0x80,0, 0, 0 },
{ 0, 0x80,0, 0 },
{ 0x80,0x80,0, 0 },
{ 0, 0, 0x80, 0 },
{ 0x80,0, 0x80, 0 },
{ 0, 0x80,0x80, 0 },
{ 0xC0,0xC0,0xC0, 0 },
{ 192, 220, 192, 0 },
{ 166, 202, 240, 0 },
{ 255, 251, 240, 0 },
{ 160, 160, 164, 0 },
{ 0x80,0x80,0x80, 0 },
{ 0xFF,0, 0, 0 },
{ 0, 0xFF,0, 0 },
{ 0xFF,0xFF,0, 0 },
{ 0, 0, 0xFF, 0 },
{ 0xFF,0, 0xFF, 0 },
{ 0, 0xFF,0xFF, 0 },
{ 0xFF,0xFF,0xFF, 0 }
};
//add up
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPrintContextView
IMPLEMENT_DYNCREATE(CPrintContextView, CView)
BEGIN_MESSAGE_MAP(CPrintContextView, CView)
//{{AFX_MSG_MAP(CPrintContextView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
ON_WM_SIZE()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
//add down
ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
//add up
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPrintContextView construction/destruction
CPrintContextView::CPrintContextView()
{
//add down
m_pDC = NULL;
m_pOldPalette = NULL;
//add up
}
CPrintContextView::~CPrintContextView()
{
}
BOOL CPrintContextView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
//add down
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
//add up
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CPrintContextView drawing
void CPrintContextView::OnDraw(CDC* pDC)
{
CPrintContextDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//add down
if (pDC->IsPrinting())
{
CRect rcDIB;
GetClientRect(&rcDIB);
rcDIB.right = rcDIB.Width();
rcDIB.bottom = rcDIB.Height();
// get size of printer page (in pixels)
int cxPage = pDC->GetDeviceCaps(HORZRES);
int cyPage = pDC->GetDeviceCaps(VERTRES);
// get printer pixels per inch
int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
CRect rcDest;
rcDest.top = rcDest.left = 0;
rcDest.bottom = (int)(((double)rcDIB.Height() * cxPage * cyInch)
/ ((double)rcDIB.Width() * cxInch));
rcDest.right = cxPage;
CapturedImage.OnDraw(pDC->m_hDC, &rcDest, &rcDIB);
}
else // not printer DC
{
DrawScene();
}
//add up
}
/////////////////////////////////////////////////////////////////////////////
// CPrintContextView printing
BOOL CPrintContextView::OnPreparePrinting(CPrintInfo* pInfo)
{
if(!pInfo->m_bPreview)
{
CRect rcDIB;
GetClientRect(&rcDIB);
OnDraw(m_pDC);
CapturedImage.Capture(m_pDC, rcDIB);
}
else
m_PrintPreview = TRUE;
BOOL rc = DoPreparePrinting(pInfo);
return rc;
}
void CPrintContextView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CPrintContextView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
CapturedImage.Release();
m_PrintPreview = FALSE;
CView::OnEndPrinting(pDC, pInfo);
}
void CPrintContextView::OnFilePrintPreview()
{
CRect rcDIB;
GetClientRect(&rcDIB);
OnDraw(m_pDC);
CapturedImage.Capture(m_pDC, rcDIB);
CView::OnFilePrintPreview();
}
/////////////////////////////////////////////////////////////////////////////
// CPrintContextView diagnostics
#ifdef _DEBUG
void CPrintContextView::AssertValid() const
{
CView::AssertValid();
}
void CPrintContextView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CPrintContextDoc* CPrintContextView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPrintContextDoc)));
return (CPrintContextDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CPrintContextView message handlers
int CPrintContextView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//add down
Init(); //初始化OpenGL
//add up
return 0;
}
void CPrintContextView::OnDestroy()
{
//add down
HGLRC hrc;
hrc = ::wglGetCurrentContext();
::wglMakeCurrent(NULL, NULL);
if (hrc)
::wglDeleteContext(hrc);
if (m_pDC)
delete m_pDC;
//add up
CView::OnDestroy();
}
BOOL CPrintContextView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CPrintContextView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
//add down
int w=cx;
int h=cy;
//避免除数为0
if(h==0) h=1;
//设置视口与窗口匹配
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
//重新设置坐标系统
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//建立透视投影下的剪切体
gluPerspective(45.0f, (GLdouble)cx/cy, 3.0f, 7.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//add up
}
//add down
void CPrintContextView::Init()
{
PIXELFORMATDESCRIPTOR pfd;
int n;
HGLRC hrc;
m_pDC = new CClientDC(this);
ASSERT(m_pDC != NULL);
if (!bSetupPixelFormat())
return;
n = ::GetPixelFormat(m_pDC->GetSafeHdc());
::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);
hrc = wglCreateContext(m_pDC->GetSafeHdc());
wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
}
BOOL CPrintContextView::bSetupPixelFormat()
{
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
int pixelformat;
if ( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 )
{
MessageBox("ChoosePixelFormat failed");
return FALSE;
}
if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
{
MessageBox("SetPixelFormat failed");
return FALSE;
}
return TRUE;
}
void CPrintContextView::DrawScene(void)
{
//将屏幕设置为白色
glClearColor(1.0, 1.0, 1.0, 1.0f);
//清除颜色缓冲区和深度缓冲区
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0, 0.0, -4.0);
glRotatef(30.0, 1.0f, 0.0f, 0.0f);
glRotatef(30.0, 0.0f, 1.0f, 0.0f);
glRotatef(30.0, 0.0f, 0.0f, 1.0f);
//设置绘制图形的颜色
glColor3f(0.0f, 0.0f, 1.0f);
//绘制四面体
glBegin(GL_LINE_STRIP);
glVertex3f(-0.5f, 0.0f, 0.5f);
glVertex3f(0.5f, 0.0f, 0.5f);
glVertex3f(0.0f, 0.5f, 0.0f);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f(0.5f, 0.0f, 0.5f);
glVertex3f(0.5f, 0.0f, -0.5f);
glVertex3f(0.0f, 0.5f, 0.0f);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f(0.5f, 0.0f, -0.5f);
glVertex3f(-0.5f, 0.0f, -0.5f);
glVertex3f(0.0f, 0.5f, 0.0f);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f(-0.5f, 0.0f, -0.5f);
glVertex3f(-0.5f, 0.0f, 0.5f);
glVertex3f(0.0f, 0.5f, 0.0f);
glEnd();
//绘制四面体
glPopMatrix();
glFinish();
SwapBuffers(wglGetCurrentDC());
}
unsigned char CPrintContextView::ComponentFromIndex(int i, UINT nbits, UINT shift)
{
unsigned char val;
val = (unsigned char) (i >> shift);
switch (nbits)
{
case 1:
val &= 0x1;
return oneto8[val];
case 2:
val &= 0x3;
return twoto8[val];
case 3:
val &= 0x7;
return threeto8[val];
default:
return 0;
}
}
void CPrintContextView::CreateRGBPalette()
{
//检查是否需要调色板
PIXELFORMATDESCRIPTOR pfd;
LOGPALETTE *pPal;
int n, i;
n = ::GetPixelFormat(m_pDC->GetSafeHdc());
::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);
if (pfd.dwFlags & PFD_NEED_PALETTE)
{
n = 1 << pfd.cColorBits;
//分配一个逻辑调色板,使用颜色表信息填充它
pPal = (PLOGPALETTE) new char[sizeof(LOGPALETTE) + n * sizeof(PALETTEENTRY)];
ASSERT(pPal != NULL);
//Win32版本
pPal->palVersion = 0x300;
//颜色表大小
pPal->palNumEntries = n;
//使用颜色索引填充所有的调色板条目
for (i=0; i<n; i++)
{
pPal->palPalEntry[i].peRed =
ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift);
pPal->palPalEntry[i].peGreen =
ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift);
pPal->palPalEntry[i].peBlue =
ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift);
pPal->palPalEntry[i].peFlags = 0;
}
/*改善调色板,使它包含缺省的GDI调色板*/
if ((pfd.cColorBits == 8) &&
(pfd.cRedBits == 3) && (pfd.cRedShift == 0) &&
(pfd.cGreenBits == 3) && (pfd.cGreenShift == 3) &&
(pfd.cBlueBits == 2) && (pfd.cBlueShift == 6)
)
{
for (i = 1 ; i <= 12 ; i++)
pPal->palPalEntry[defaultOverride[i]] = defaultPalEntry[i];
}
//删除现有的GDI调色板
m_cPalette.CreatePalette(pPal);
delete pPal;
m_pOldPalette = m_pDC->SelectPalette(&m_cPalette, FALSE);
m_pDC->RealizePalette();
}
}
//add up
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -