C案例:打印钻石图形

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/howard2005/article/details/79409408
C案例:打印钻石图形

打印如下钻石图形:


1、源程序


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
	
    int row = 15, half, i, j;
	
	//打印钻石上半部分
	half = row / 2 + 1;	
	
	for(i = 1; i <= half; i++)
	{
		//打印前导空格
		for(j = 1; j <= half - i; j++)
		{
			printf(" ");	
		}
		//打印星号
		for(j = 1; j <= 2 * i - 1; j++)
		{
			printf("*");	
		}
		//换行
		printf("\n");
	}
	
	//打印钻石下半部分	
	for(i = 1; i < half; i++)
	{
		//打印前导空格
		for(j = 1; j<= i; j++)
		{
			printf(" ");	
		}
		//打印星号
		for(j = 1; j <= 2 * (half - i - 1) + 1; j++)
		{
			printf("*");	
		}
		//换行
		printf("\n");
	}		  		
	return 0;
}

2、运行结果


展开阅读全文

急!!打印不出图形

11-17

我用opengl画图,然后用虚拟打印机打印成pdf显示,但是pdf出来了,却没有图形,让人很困惑。rnrn[code=c]rnCString strDefualtDev = TEXT("");rn PRINTDLG pd;rn LPDEVMODE lpDevMode;rn if(AfxGetApp()->GetPrinterDeviceDefaults(&pd))rn rn lpDevMode=(LPDEVMODE)GlobalLock(pd.hDevMode);rn if(lpDevMode)rn rn strDefualtDev = lpDevMode->dmDeviceName;rn rn GlobalUnlock(pd.hDevMode);rn rn if(AfxGetApp()->GetPrinterDeviceDefaults(&pd)) rn rn lpDevMode=(LPDEVMODE)GlobalLock(pd.hDevMode); rn if(lpDevMode)rn rn lpDevMode->dmPaperSize=DMPAPER_A4; // 设定打印纸张幅面rn lpDevMode->dmOrientation=DMORIENT_LANDSCAPE; // 设定横向打印rn lpDevMode->dmPrintQuality=600; // 设定打印机分辨率rn rn GlobalUnlock(pd.hDevMode);rn ResetDC(pd.hDC, lpDevMode);rn rn DOCINFO info;rn memset( &info, 0, sizeof(DOCINFO) );rn info.cbSize = sizeof(DOCINFO); rn info.lpszDocName = _T("打印图形"); // 设定打印文档名称rn info.lpszOutput = (LPTSTR) NULL; rn info.fwType = 0; rn HDC hdc = ::CreateDC(NULL, _T("pdfFactory Pro"), NULL, lpDevMode); // 得到打印机设备句柄rn int a = StartDoc(hdc,&info);rn StartPage(hdc);rnrn HDC hdc2 = GetDC(hWnd);rn PIXELFORMATDESCRIPTOR *pfd=new PIXELFORMATDESCRIPTOR;rn pfd->nSize=sizeof(PIXELFORMATDESCRIPTOR);rn pfd->nVersion=1;rn pfd->dwFlags=PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER|PFD_STEREO_DONTCARE;rn pfd->iPixelType=PFD_TYPE_RGBA;rn pfd->cColorBits=32;rn pfd->cRedBits=8;rn pfd->cRedShift=16;rn pfd->cGreenBits=8;rn pfd->cGreenShift=8;rn pfd->cBlueBits=8;rn pfd->cBlueShift=0;rn pfd->cAlphaBits=0;rn pfd->cAlphaShift=0;rn pfd->cAccumBits=64;rn pfd->cAccumRedBits=16;rn pfd->cAccumGreenBits=16;rn pfd->cAccumBlueBits=16;rn pfd->cAccumAlphaBits=0;rn pfd->cDepthBits=32;rn pfd->cStencilBits=8;rn pfd->cAuxBuffers=0;rn pfd->iLayerType=PFD_MAIN_PLANE;rn pfd->bReserved=0;rn pfd->dwLayerMask=0;rn pfd->dwVisibleMask=0;rn pfd->dwDamageMask=0;rn rn int informat = ChoosePixelFormat(hdc2,pfd);rn if (informat == 0)rn rn if (DescribePixelFormat(hdc2,informat,sizeof(PIXELFORMATDESCRIPTOR), pfd)==0)rn rn return;rn rn informat = 1;rn rn if (::SetPixelFormat(hdc2, informat, pfd) == FALSE)rn rn return;rn rnrn HGLRC hrc = wglCreateContext(hdc2);rn if(hrc!=NULL)rn rn BOOL flag = wglMakeCurrent(hdc2,hrc);rn int a= 0;rn rnrn glMatrixMode(GL_MODELVIEW); // To operate on Model-View matrixrn glLoadIdentity();rnrn glPushMatrix();rn glColor3f(1.0,0.0,1.0); rn glBegin(GL_POLYGON);rn glVertex2f(100,2);rn glVertex2f(4,130);rn glVertex2f(4,170);rn glEnd();rn glPopMatrix();rnrn wglMakeCurrent(NULL,NULL);//断开与着色环境的联系rnrn EndPage(hdc);rn EndDoc(hdc);rn[/code] 论坛

没有更多推荐了,返回首页