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

📄 graphb.c

📁 本下载文件是《C/C++图像处理编程》一书的演示程序
💻 C
字号:
//    GraphB1 ---- GraphB6
//    练习模块——图形功能
 
#include <windows.h>
#include "image32.h"
#include "math.h"

#define		PI		3.14159265
	
void CrossLine(HDC hMemDC,int x,int y);

extern HDC	hMemDC;
extern struct IMAGE	*image;
extern struct IMAGE *Image[10];
  
void GraphB1(HWND hWnd)
{
  COLORREF clrref;
  int      i,j,x,y,n;
 
  x=50;		y=150;
  n=400;

  hMemDC=CreateMemDC();

  SetMapMode(hMemDC,MM_TEXT);
  
  clrref=RGB(0,0,255);
  for (i=-20;i<n+20;i++) {
	SetPixel(hMemDC,x+i,y,clrref);
  }

  clrref=RGB(255,0,0);
  for (i=0;i<n;i++) {
	j=(int) (50.0*(n-3*i/4)/n*sin(PI*i/(n/15)));
	SetPixel(hMemDC,x+i,y-j,clrref);
  }

  DeleteMemDC(hMemDC);
  InvalidateRect(hWnd,NULL,FALSE);
}

void GraphB2(HWND hWnd)
{
  LOGPEN lpGreen = {PS_SOLID,1,1,RGB(0,255,0)},
         lpBlack = {PS_SOLID,1,1,RGB(0,0,0)};
  HPEN   lpGreenPen,lpBlackPen;
  int    i,j,x,y,n;
 
  x=50;		y=325;
  n=400;

  hMemDC=CreateMemDC();
  lpGreenPen = CreatePenIndirect(&lpGreen);
  lpBlackPen = CreatePenIndirect(&lpBlack);

  SelectObject(hMemDC,lpGreenPen);
  MoveToEx(hMemDC,x,y,NULL);
  for (i=0;i<n;i++) {
	j=(int) (50.0*(n-3*i/4)/n*sin(PI*i/(n/15)));
	LineTo(hMemDC,x+i,y-j);
  }

  SelectObject(hMemDC,lpBlackPen);
  MoveToEx(hMemDC,x-20,y,NULL);
  LineTo(hMemDC,x+n+20,y);

  DeleteMemDC(hMemDC);
  InvalidateRect(hWnd,NULL,FALSE);
}

void GraphB3(HWND hWnd)
{
  LOGPEN lpRed   = {PS_SOLID,1,1,RGB(255,0,0)},
         lpGreen = {PS_SOLID,1,1,RGB(0,255,0)},
         lpBlue  = {PS_SOLID,1,1,RGB(0,0,255)},
         lpBlack = {PS_SOLID,1,1,RGB(0,0,0)};
  HPEN   lpRedPen,lpGreenPen,lpBluePen,lpBlackPen;
  int    x,y,x1,y1,r;
 
  x=175;		y=450;
  r=150;
  x1=x+r;		y1=y+r;

  hMemDC=CreateMemDC();
  lpRedPen   = CreatePenIndirect(&lpRed);
  lpGreenPen = CreatePenIndirect(&lpGreen);
  lpBluePen  = CreatePenIndirect(&lpBlue);
  lpBlackPen = CreatePenIndirect(&lpBlack);

  SelectObject(hMemDC,lpRedPen);
  CrossLine(hMemDC,x1,y);
  Arc(hMemDC,x,y,x1,y1,x1,y,x,y);
  
  SelectObject(hMemDC,lpGreenPen);
  CrossLine(hMemDC,x,y);
  Arc(hMemDC,x,y,x1,y1,x,y,x,y1);

  SelectObject(hMemDC,lpBluePen);
  CrossLine(hMemDC,x,y1);
  Arc(hMemDC,x,y,x1,y1,x,y1,x1,y1);
 
  SelectObject(hMemDC,lpBlackPen);
  CrossLine(hMemDC,x1,y1);
  Arc(hMemDC,x,y,x1,y1,x1,y1,x1,y);

  DeleteMemDC(hMemDC);
  InvalidateRect(hWnd,NULL,FALSE);
}

void GraphB4(HWND hWnd)
{
  HBRUSH hBrush;
  HPEN   hPen;
  int    x,y;

  x=500;		y=0;
  hMemDC=CreateMemDC();
 
  hPen  =GetStockObject(BLACK_PEN);
  hBrush=GetStockObject(HOLLOW_BRUSH);
  SelectObject(hMemDC,hPen);
  SelectObject(hMemDC,hBrush);

  Rectangle(hMemDC,x,50,x+50,100);
  RoundRect(hMemDC,x,150,x+50,200,15,15);
  Ellipse(hMemDC,x+75,50,x+125,100);
  Chord(hMemDC,x+75,150,x+125,200,x+75,150,x+125,200);
  Pie(hMemDC,x+150,50,x+200,100,x+150,50,x+200,50);

  x=750;
  hBrush=GetStockObject(DKGRAY_BRUSH);
  SelectObject(hMemDC,hBrush);

  Rectangle(hMemDC,x,50,x+50,100);
  RoundRect(hMemDC,x,150,x+50,200,15,15);
  Ellipse(hMemDC,x+75,50,x+125,100);
  Chord(hMemDC,x+75,150,x+125,200,x+75,150,x+125,200);
  Pie(hMemDC,x+150,50,x+200,100,x+150,50,x+200,50);

  DeleteMemDC(hMemDC);
  InvalidateRect(hWnd,NULL,FALSE);
}

void GraphB5(HWND hWnd)
{
  LOGPEN lpBlack = {PS_SOLID,1,1,RGB(0,0,0)};
  HBRUSH hBrush[4];
  HPEN   hBlackPen;
  int    x,y;


  x=525;		y=250;
  hBlackPen = CreatePenIndirect(&lpBlack);
  hBrush[0] = CreateHatchBrush(HS_HORIZONTAL,RGB(0,0,0));
  hBrush[1] = CreateHatchBrush(HS_VERTICAL,RGB(255,0,0));
  hBrush[2] = CreateHatchBrush(HS_FDIAGONAL,RGB(0,255,0));
  hBrush[3] = CreateHatchBrush(HS_BDIAGONAL,RGB(0,0,255));

  hMemDC=CreateMemDC();
 
  SelectObject(hMemDC,hBlackPen);
  SelectObject(hMemDC,hBrush[0]);
  Rectangle(hMemDC,x+10,y+10,x+100,y+200);
  
  SelectObject(hMemDC,hBrush[1]);
  Rectangle(hMemDC,x+110,y+10,x+200,y+200);
  
  SelectObject(hMemDC,hBrush[2]);
  Rectangle(hMemDC,x+210,y+10,x+300,y+200);

  SelectObject(hMemDC,hBrush[3]);
  Rectangle(hMemDC,x+310,y+10,x+400,y+200);

  DeleteMemDC(hMemDC);
  InvalidateRect(hWnd,NULL,FALSE);
}

void GraphB6(HWND hWnd)
{
  int        i,x,y;
  int        nXChar,nYChar;
  TEXTMETRIC tm;
  char       *textbuf[]={"Windows C 图像处理演示程序",
	                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
	                    "abcdefghijklmnopqrstuvwxyz",
						"1234567890",
						"Windows 图形编程"};
 
  x=575;		y=490;
 
  hMemDC=CreateMemDC();
  GetTextMetrics(hMemDC,&tm);
  nXChar=tm.tmAveCharWidth;
  nYChar=tm.tmHeight+tm.tmExternalLeading;

  for (i=0;i<5;i++) {
    TextOut(hMemDC,x+nXChar,y+nYChar*(1+i),textbuf[i],lstrlen(textbuf[i]));
  }
  DeleteMemDC(hMemDC);
  InvalidateRect(hWnd,NULL,FALSE);
}

 

⌨️ 快捷键说明

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