支持 OpenGL 打印的 Win32 类






4.14/5 (8投票s)
1999年11月23日

98148

3109
- 下载源文件 - 3 Kb
- 下载演示项目 - 36 Kb <!-- 文章开始 -->
示例
打印预览示例
本文提供了一个类 CGLMemoryDC,它可以在 Win95/NT 下支持 OpenGL 打印。
CGLMemoryDC 存储打印机上下文设备的 DIB 数据。
//data of DIB private: HBITMAP m_hBitmap; //handle of bitmap BITMAPINFO m_DIBInfo; //infomation about the DIB BYTE* m_hImage; //DIB color data DWORD m_dwDataSize; //DIB data size
CGLMemoryDC 还提供了从选定上下文设备获取 DIB 数据或将 DIB 数据设置到目标上下文设备的函数。
/********************************************************************/ /* Scan the image from the device context */ /********************************************************************/ void CGLMemoryDC::CopyDataFromDC(CDC* pDC, CRect& rect) { CDC dcBuffer; //the compatible DC CBitmap bmBitmap; //bitmap in memory for retreive data from DC CBitmap* pbmBitmapOld; //if the orignal bitmap did not set up if(!m_hBitmap) return; //create compatible DC to copy image dcBuffer.CreateCompatibleDC(pDC); //create memory bitmap bmBitmap.CreateCompatibleBitmap(pDC, m_DIBInfo.bmiHeader.biWidth, m_DIBInfo.bmiHeader.biHeight); //set memory bitmap to memory DC pbmBitmapOld = dcBuffer.SelectObject(&bmBitmap); //copy source DC image to memory bitmap dcBuffer.StretchBlt(0, 0, m_DIBInfo.bmiHeader.biWidth, m_DIBInfo.bmiHeader.biHeight, pDC, rect.left, rect.top, rect.Width(), rect.Height(), SRCCOPY); //restore the orginal object in memory DC dcBuffer.SelectObject(pbmBitmapOld); //copy image data from memory bitmap GetDIBits(pDC->m_hDC, HBITMAP)bmBitmap, 0, m_DIBInfo.bmiHeader.biHeight, m_hImage, &m_DIBInfo, DIB_RGB_COLORS); } /********************************************************************/ /* Copy image data to target DC */ /* */ /* This function just support the color printer setting in Text */ /* mode */ /********************************************************************/ void CGLMemoryDC::CopyDataToDC(CDC* pDC, CRect& rect) { ::StretchDIBits(pDC->m_hDC, rect.left, rect.top, rect.Width(), rect.Height(), 0, 0, m_DIBInfo.bmiHeader.biWidth, m_DIBInfo.bmiHeader.biHeight, m_hImage, &m_DIBInfo, DIB_RGB_COLORS, SRCCOPY); } /********************************************************************/ /* Write image data directly to target DC */ /* */ /* This function just support the color printer setting in Photo */ /* quality mode */ /********************************************************************/ void CGLMemoryDC::WriteDataToDC(CDC* pDC, int startx, int starty) { ::SetDIBitsToDevice(pDC->m_hDC, startx, starty, m_DIBInfo.bmiHeader.biWidth, m_DIBInfo.bmiHeader.biHeight, 0, 0, 0, m_DIBInfo.bmiHeader.biHeight, m_hImage, &m_DIBInfo, DIB_RGB_COLORS); }
CGLMemoryDC 中的函数 "WriteDataToDIBfile" 允许将 DIB 数据存储到磁盘文件。
在应用程序中使用 CGLMemoryDC 以支持打印的方式如下:
void CGlprintView::OnDraw(CDC* pDC) { CGlprintDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here //if print if(pDC->IsPrinting()) { //draw the image in memory DC to print DC CRect drawRect; int cx, cy; COLORREF clrOld = pDC->SetTextColor(RGB(250, 10, 10)); pDC->TextOut(450,10, "This is a demo of OpenGL print provided by Zhaohui Xing"); pDC->SetTextColor(RGB(128, 128, 255)); pDC->TextOut(40,80, "Large Size"); drawRect.SetRect(40, 160, 2440, 1960); pDC->DPtoLP(&drawRect); m_MemImageDC.CopyDataToDC(pDC, drawRect); pDC->TextOut(40,1960, "Medium Size"); drawRect.SetRect(500, 2040, 2100, 3240); pDC->DPtoLP(&drawRect); m_MemImageDC.CopyDataToDC(pDC, drawRect); pDC->TextOut(40,3260, "Orignal Size"); m_MemImageDC.GetMemorySize(&cx, &cy); drawRect.SetRect(1000, 3400, 1000 + cx , 3400 + cy); pDC->DPtoLP(&drawRect); m_MemImageDC.CopyDataToDC(pDC, drawRect); pDC->SetTextColor(clrOld); } else //draw opnGL in current DC { CPalette* oldPalette; //Set logic palette oldPalette = m_pDC->SelectPalette(&m_Palette, FALSE); m_pDC->RealizePalette(); //draw openGL object wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC); DrawObject(); SwapBuffers(m_pDC->GetSafeHdc()); wglMakeCurrent(m_pDC->GetSafeHdc(), NULL); //Prepare the memory DC CRect rect; GetClientRect(&rect); m_MemImageDC.SetMemorySize(rect.Width(), rect.Height()); //copy the image data in current DC to memory m_MemImageDC.CopyDataFromDC(m_pDC, rect); m_pDC->SelectPalette(oldPalette, FALSE); } }
许可证
本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。
作者可能使用的许可证列表可以在此处找到。