⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 videowin.cpp

📁 WIN32高级程序设计书附源码
💻 CPP
字号:
#include <afxwin.h>
#include <math.h>
#include <memory.h>
static CWnd VWnd;
static BOOL fCreated=FALSE;
extern CWnd *pParent;
long screenarea;
const int vid1reps=20;
const int vid2reps=2;
const int vid3reps=50;
const int vid4reps=1;
long vid1pix=0;
long vid2pix=0;
long vid3pix=0;
long vid4pix=0;
static RECT crect;
extern int bpp,rastercaps,sizepalette,numreserved;
BOOL fSmallWindow=FALSE;  
BOOL fReduceReps=FALSE;

void video0(void) //create window
{
	RECT r={0,0,640,480};
	if(fSmallWindow) {
		r.right /= 2;
		r.bottom /= 2;
		}		
	fCreated=VWnd.Create(NULL,"WINMAG Video Torture Test",WS_POPUP|WS_CAPTION|WS_VISIBLE,r,pParent,NULL);
	if(!fCreated) MessageBeep(0);
	VWnd.GetClientRect(&crect);
	screenarea=(long)crect.right*(long)crect.bottom;
	TRACE("screenarea:%ld",screenarea);
}                     

#define ScrollMethod 1
void scroll1(CClientDC& vdc)
{
	TEXTMETRIC tm;
	vdc.GetTextMetrics(&tm);
	int yChar=tm.tmHeight + tm.tmExternalLeading;
	ASSERT(yChar>0);
	ASSERT(VWnd.GetSafeHwnd());
	int yLoc= crect.bottom-yChar;
	vdc.PatBlt(0,0,crect.right,crect.bottom,WHITENESS);	//erase background
	vid1pix += screenarea;
	CString TestString("WINDOWS Magazine Character Scrolling Test -- WINDOWS Magazine Character Scrolling Test -- WINDOWS Magazine Character Scrolling Test");
	for(int i=0;i<(fReduceReps?vid1reps/2:vid1reps);i++) {
#ifdef ColorTheText	
		vdc.SetTextColor(PALETTEINDEX(i%15));
#endif		
		vdc.TextOut(0,yLoc,TestString.Mid(i%40,85)); 
#if ScrollMethod==1		
		VWnd.ScrollWindow(0,-yChar);
		VWnd.UpdateWindow();
#elif ScrollMethod==2
		vdc.ScrollDC(0,-yChar,&crect,NULL,NULL,NULL);
#elif ScrollMethod==3		
		vdc.BitBlt(0,0,crect.right,crect.bottom-yChar,&vdc,0,yChar,SRCCOPY);
#endif //scroll method				
		vid1pix += screenarea;
	} 
	TRACE("vid1pix:%ld",vid1pix);
#ifdef ColorTheText	
	vdc.SetTextColor(PALETTEINDEX(0));
#endif		
}

void video1(void) //character scrolling
{
	vid1pix=0;
	if(!fCreated) return;
	CClientDC vdc(&VWnd);					//construct device context for client area
	CFont cf;
	cf.CreateFont(20,0,0,0,0,0,0,0,1,
		OUT_TT_PRECIS,CLIP_TT_ALWAYS,
		PROOF_QUALITY,DEFAULT_PITCH,"Arial");
	CFont *poldf=vdc.SelectObject(&cf); //poldf should point to SYSTEM
	scroll1(vdc);	//scroll test with Arial 20
	vdc.SelectObject(poldf);
	cf.DeleteObject();
	cf.CreateFont(40,0,0,0,0,0,0,0,1,
		OUT_TT_PRECIS,CLIP_TT_ALWAYS,
		PROOF_QUALITY,DEFAULT_PITCH,"Arial");
	poldf=vdc.SelectObject(&cf);	//poldf should point to SYSTEM
	scroll1(vdc);	//scroll test with Arial 40
	vdc.SelectObject(poldf);
	cf.DeleteObject();
#ifdef TestTwoFonts	
	cf.CreateFont(20,0,0,0,0,0,0,0,1,
		OUT_TT_PRECIS,CLIP_TT_ALWAYS,
		PROOF_QUALITY,DEFAULT_PITCH,"Times New Roman");
	poldf=vdc.SelectObject(&cf);	//poldf should point to SYSTEM
	scroll1(vdc);	//scroll test with Times 20
	vdc.SelectObject(poldf);
	cf.DeleteObject();
	cf.CreateFont(40,0,0,0,0,0,0,0,1,
		OUT_TT_PRECIS,CLIP_TT_ALWAYS,
		PROOF_QUALITY,DEFAULT_PITCH,"Times New Roman");
	poldf=vdc.SelectObject(&cf);	//poldf should point to SYSTEM
	scroll1(vdc);	//scroll test with Times 40
	vdc.SelectObject(poldf);
	cf.DeleteObject();
#endif
}

void video2(void) //line/curve drawing
{
	int i,j;
	CPen *ppen,*ppenold;

	vid2pix=0;
	int hcenter=(crect.left+crect.right)/2;
	int vcenter=(crect.top+crect.bottom)/2;
	if(!fCreated) return;
	CClientDC vdc(&VWnd);					//construct device context for client area
	for(j=0;j<(fReduceReps?vid2reps:vid2reps*2);j++) {				//line drawing tests
		vdc.PatBlt(0,0,crect.right,crect.bottom,WHITENESS);	//erase background
		vid2pix += screenarea;
		for(i=crect.left;i<crect.right;i++) {	//vertical black lines
			vdc.MoveTo(i,crect.top);
			vdc.LineTo(i,crect.bottom);
		}
		vid2pix += screenarea;
		ppenold=(CPen *)vdc.SelectStockObject(WHITE_PEN);
		for(i=crect.top;i<crect.bottom;i++) {	//horizontal white lines
			vdc.MoveTo(crect.left,i);
			vdc.LineTo(crect.right,i);
		}
		vdc.SelectObject(ppenold);
		vid2pix += screenarea;
		for(i=0;i<3600;i+=5) {						//colored clock wipe, half-degree increments
			double theta=i/572.96;
			ppen=new CPen(0,0,PALETTEINDEX((i/5)%15));
			vdc.SelectObject(ppen);
			vdc.MoveTo(hcenter,vcenter);
			vdc.LineTo(int(hcenter+hcenter*cos(theta)),int(vcenter+vcenter*sin(theta)));
			vdc.SelectObject(ppenold);
			delete ppen;
		}
		vid2pix += screenarea;
}
	for(j=0;j<(fReduceReps?vid2reps/2:vid2reps);j++) {		//arc drawing tests
		vdc.PatBlt(0,0,crect.right,crect.bottom,WHITENESS);	//erase background
		vid2pix += screenarea;
		for(i=0;i<hcenter;i++) {	//solid-lined colored ellipses
			ppen=new CPen(0,0,PALETTEINDEX(i%15));
			vdc.SelectObject(ppen);
			vdc.Arc(crect.left+i,crect.top+i,crect.right-i,crect.bottom-i,
					crect.right,vcenter,crect.left,vcenter);
			vdc.Arc(crect.left+i,crect.top+i,crect.right-i,crect.bottom-i,
					crect.left,vcenter,crect.right,vcenter);
			vdc.SelectObject(ppenold);
			delete ppen;
		}
		vid2pix += screenarea;
	}
	TRACE("vid2pix:%ld",vid2pix);

}

void video3(void) //filled objects
{
	int j;
	int  xLeft, xRight, yTop, yBottom;
	BYTE nRed, nGreen, nBlue ;

	vid3pix=0;
	if(!fCreated) return;
	CClientDC vdc(&VWnd);					//construct device context for client area
	vdc.PatBlt(0,0,crect.right,crect.bottom,WHITENESS);	//erase background
	vid3pix += screenarea;
	for(j=0;j<(fReduceReps?vid3reps:vid3reps*2);j++) {		//random rectangles
	  xLeft	 = rand () % crect.right ;
	  xRight  = rand () % crect.right ;
	  yTop	 = rand () % crect.bottom ;
	  yBottom = rand () % crect.bottom ;
	  nRed	 = rand () & 255 ;
	  nGreen  = rand () & 255 ;
	  nBlue	 = rand () & 255 ;
	  CBrush *cb = new CBrush(RGB (nRed, nGreen, nBlue)) ;
	  CBrush *cbold = vdc.SelectObject(cb) ;
	  vdc.Rectangle (min (xLeft, xRight), min (yTop, yBottom),
							max (xLeft, xRight), max (yTop, yBottom));
	  vdc.SelectObject(cbold);
	  delete cb;
	  vid3pix += labs(long(xLeft-xRight)*long(yTop-yBottom));
	}
	vdc.PatBlt(0,0,crect.right,crect.bottom,WHITENESS);	//erase background
	vid3pix += screenarea;
	for(j=0;j<(fReduceReps?vid3reps/2:vid3reps);j++) {	//random rounded rectangles
	  xLeft	 = rand () % crect.right ;
	  xRight  = rand () % crect.right ;
	  yTop	 = rand () % crect.bottom ;
	  yBottom = rand () % crect.bottom ;
	  nRed	 = rand () & 255 ;
	  nGreen  = rand () & 255 ;
	  nBlue	 = rand () & 255 ;
	  CBrush *cb = new CBrush(RGB (nRed, nGreen, nBlue)) ;
	  CBrush *cbold = vdc.SelectObject(cb) ;
	  vdc.RoundRect (min (xLeft, xRight), min (yTop, yBottom),
							max (xLeft, xRight), max (yTop, yBottom), 20, 20);
	  vdc.SelectObject(cbold);
	  delete cb;
	  vid3pix += labs(long(xLeft-xRight)*long(yTop-yBottom));
	}
	vdc.PatBlt(0,0,crect.right,crect.bottom,WHITENESS);	//erase background
	vid3pix += screenarea;
	for(j=0;j<(fReduceReps?vid3reps/2:vid3reps);j++) {	//random ellipses
	  xLeft	 = rand () % crect.right ;
	  xRight  = rand () % crect.right ;
	  yTop	 = rand () % crect.bottom ;
	  yBottom = rand () % crect.bottom ;
	  nRed	 = rand () & 255 ;
	  nGreen  = rand () & 255 ;
	  nBlue	 = rand () & 255 ;
	  CBrush *cb = new CBrush(RGB (nRed, nGreen, nBlue)) ;
	  CBrush *cbold = vdc.SelectObject(cb) ;
	  vdc.Ellipse (min (xLeft, xRight), min (yTop, yBottom),
							max (xLeft, xRight), max (yTop, yBottom));
	  vdc.SelectObject(cbold);
	  delete cb;
	  vid3pix += labs(long(xLeft-xRight)*long(yTop-yBottom)); //NB:bad approximation, but fair enough
	}
	TRACE("vid3pix:%ld",vid3pix);
}

void video4(void) //color display
{
	int i;
	BYTE red,green,blue;
	CPalette cpal;
	vid4pix=0;
	if(!fCreated) return;
	CClientDC vdc(&VWnd);					//construct device context for client area
	vdc.PatBlt(0,0,crect.right,crect.bottom,WHITENESS);	//erase background
	vid4pix += screenarea;
	//set up palette for gradient fill
	int numcolors=sizepalette-numreserved;
	if(numcolors<=0)
		if(bpp>=8)
			numcolors=255; //limited by number of levels of 1 color we can use
		else numcolors=2^bpp;
	LOGPALETTE NEAR *plgpl = (LOGPALETTE NEAR *) LocalAlloc(LPTR,
	 sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY));
	plgpl->palVersion = 0x300;
	for(int j=0;j<vid4reps;j++) {
		plgpl->palNumEntries = numcolors;
		for (i = 0,red = 0,green = 0,blue = 0; i < numcolors;
		  i++, blue += 1) {
			  plgpl->palPalEntry[i].peRed = red;
			  plgpl->palPalEntry[i].peGreen = green;
			  plgpl->palPalEntry[i].peBlue = blue;
			  plgpl->palPalEntry[i].peFlags = PC_RESERVED;
		}
		BOOL fpal=cpal.CreatePalette(plgpl);
		CPalette *oldpal=vdc.SelectPalette(&cpal,FALSE);
		UINT numrealized=vdc.RealizePalette();
		//do gradient fill
		for(i=crect.top;i<crect.bottom;i++) {	//horizontal lines
			int ipal=i%numcolors;
			CPen *cp=new CPen(0,0,PALETTERGB(plgpl->palPalEntry[ipal].peRed,
				plgpl->palPalEntry[ipal].peGreen,plgpl->palPalEntry[ipal].peBlue));
			CPen *oldpen=vdc.SelectObject(cp);
			vdc.MoveTo(crect.left,i);
			vdc.LineTo(crect.right,i);
			vdc.SelectObject(oldpen);
			delete cp;
			}
		vid4pix += screenarea;
		//animate palette if there is one
		for(i=0;i<numcolors && sizepalette>0;i++) {
			PALETTEENTRY pe;
			pe=plgpl->palPalEntry[0];
			memmove(&plgpl->palPalEntry[0],&plgpl->palPalEntry[1],(numcolors-1)*sizeof(PALETTEENTRY));
			plgpl->palPalEntry[numcolors-1]=pe;
			cpal.AnimatePalette(0,numcolors,plgpl->palPalEntry);
			vid4pix += numcolors*sizeof(PALETTEENTRY);
			}
		//load 24-bit DIB (BIRD BITMAP resource)
		HINSTANCE hinst=AfxGetInstanceHandle();
		ASSERT(hinst);
		HRSRC hr=FindResource(hinst,"BIRD",RT_BITMAP);
		ASSERT(hr);
		if(!hr) {
			MessageBeep(0);
			LocalFree((HLOCAL) plgpl);
			return;
		}
		HGLOBAL hg=LoadResource(hinst,hr);
		ASSERT(hg);
		void FAR *pBird=LockResource(hg);
		ASSERT(pBird);
		LPBITMAPINFOHEADER lpbi=(LPBITMAPINFOHEADER)pBird;
		LPSTR pixels=(LPSTR)pBird + *(LPDWORD)pBird + lpbi->biClrUsed*sizeof(RGBQUAD);
		//if needed, set and realize a generalized palette for the 24-bit dib
		vdc.PatBlt(0,0,crect.right,crect.bottom,WHITENESS);	//erase background
		vid4pix += screenarea;
		if(sizepalette>0) {
			red = green = blue = 0;
			vdc.SelectPalette(oldpal,FALSE);
			cpal.DeleteObject(); //free our old palette explicitly
			plgpl->palNumEntries = 256;
			for (i = 0;  i < 256;  i++) {
				plgpl->palPalEntry[i].peRed = red;
				plgpl->palPalEntry[i].peGreen = green;
				plgpl->palPalEntry[i].peBlue = blue;
				plgpl->palPalEntry[i].peFlags = (BYTE) 0;
				if (!(red += 32))
					if (!(green += 32))
						blue += 64;
				}
			fpal=cpal.CreatePalette(plgpl);
			vdc.SelectPalette(&cpal,FALSE);
			numrealized=vdc.RealizePalette();
			}
		//Actually display the 24-bit dib
		ASSERT(vdc.GetSafeHdc());
		SetDIBitsToDevice(vdc.GetSafeHdc(),0,0,(int)lpbi->biWidth,(int)lpbi->biHeight,0,0,
			0,(UINT)lpbi->biHeight,pixels,(LPBITMAPINFO)lpbi,DIB_RGB_COLORS);
		vid4pix += (long)lpbi->biWidth*lpbi->biHeight;
		//clean up
		UnlockResource(hg);
		FreeResource(hg);
		vdc.SelectPalette(oldpal,FALSE); //restore original palette
		cpal.DeleteObject(); //free our palette explicitly (
		}
	LocalFree((HLOCAL) plgpl);
	TRACE("vid4pix:%ld",vid4pix);
}

void video5(void) //destroy window
{
	if(!fCreated) return;
	BOOL fDestroyed=VWnd.DestroyWindow();
	if(!fDestroyed)
		MessageBeep(0);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -