程序界面
//想来好久都没有思路,十分感谢一波同学提供的的建议
// Exp1.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "Exp1.h"
#include "windows.h"
#include "math.h"
#include "shellapi.h"
#define MAX_LOADSTRING 100
# define Array_Num 11//一笔画只支持N为质数的情况,所以N必须为质数
// 全局变量:
HINSTANCE hInst;
// 当前实例
TCHAR szTitle[MAX_LOADSTRING];
// 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];
// 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM
MyRegisterClass(HINSTANCE hInstance);
BOOL
InitInstance(HINSTANCE, int);
LRESULT CALLBACK
WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK
About(HWND, UINT, WPARAM, LPARAM);
int flag=0;//当前显示图形,为0都不显示,为1仅显示E1-1,为2仅显示E1-2,为3仅显示E1-3
RECT rect;
void paint(HDC hdc,int flag);//画图调用选择
int DrawLine(HDC hdc);//画图E1-1
int DrawDiamond(HDC hdc);//画图E1-2
int DrawTriangle(HDC hdc);//画图E1-3
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_EXP1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_EXP1));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
// 注释:
//
// 仅当希望
// 此代码与添加到 Windows 95 中的“RegisterClassEx”
// 函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
// 这样应用程序就可以获得关联的
// “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style
= CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc
= WndProc;
wcex.cbClsExtra
= 0;
wcex.cbWndExtra
= 0;
wcex.hInstance
= hInstance;
wcex.hIcon
= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_EXP1));
wcex.hCursor
= LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground
= (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName
= MAKEINTRESOURCE(IDC_EXP1);
wcex.lpszClassName
= szWindowClass;
wcex.hIconSm
= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND
- 处理应用程序菜单
// WM_PAINT
- 绘制主窗口
// WM_DESTROY
- 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
GetClientRect(hWnd,&rect);
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case ID_DrawLine:
flag=1;
InvalidateRect(hWnd,&rect,true);
break;
case ID_DrawDiamond:
flag=2;
InvalidateRect(hWnd,&rect,true);
break;
case ID_DrawRriangle:
flag=3;
InvalidateRect(hWnd,&rect,true);
break;
case ID_MyBlog:
ShellExecuteW(NULL, _T("open"), _T("http://online19910326.blog.163.com/"), NULL, NULL, SW_SHOWNORMAL);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
SetTextColor(hdc,RGB(0x9A,0x32,0xCD));//设置字体颜色为紫色
TextOutW(hdc,0,0,_T("计算机图形学实验1,Win32 API,Windows 7,Visual Studio 2010"),48);
paint(hdc,flag);//调用画图程序
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
//画图调用选择
void paint(HDC hdc,int flag)
{
if(flag==0)
{
}
else if(flag==1)
{
DrawLine(hdc);
}
else if(flag==2)
{
DrawDiamond(hdc);
}
else if (flag==3)
{
DrawTriangle(hdc);
}
else
{
DrawLine(hdc);
DrawDiamond(hdc);
DrawTriangle(hdc);
}
}
//画E1-1
int DrawLine(HDC hdc)
{
HPEN hPen; //画笔
hPen = CreatePen(PS_SOLID, 1, RGB(148, 0, 211)); //紫色画笔
SelectObject(hdc,hPen);//设置画笔颜色
MoveToEx(hdc,160,240,NULL);
for(int x=160;x<=480;x+=20)//一笔画
{
LineTo(hdc,x,120);
LineTo(hdc,480,240);
LineTo(hdc,x,360);
LineTo(hdc,160,240);
}
DeleteObject(hPen); //删除新笔
return 0;
}
//画E1-2,一笔画只支持N为质数的情况
int DrawDiamond(HDC hdc)
{
HPEN hPen; //画笔声明
hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255)); //蓝色画笔
SelectObject(hdc,hPen);//设置画笔颜色
double x0=400,y0=200;//平移坐标
double x[Array_Num],y[Array_Num];//坐标数组
double t=3.14*2/Array_Num;//t为角度(弧度制)
double radius=100;//半径
for(int i=0;i<Array_Num;i++)//找出所有点的坐标
{
x[i]=radius*cos(i*t)+x0;
y[i]=radius*sin(i*t)+y0;
}
MoveToEx(hdc,x[0],y[0],NULL);
for(int i=1;i<Array_Num;i++)
{
for(int j=i;;j=j+i)
{
if(j>=Array_Num)
j=j-Array_Num;
if(j<Array_Num)
LineTo(hdc,x[j],y[j]);
if(j==0)
break;
}
}
DeleteObject(hPen);
return 0;
}
//画E1-3
int DrawTriangle(HDC hdc)
{
int X0=300;//x方向平移
int Y0=150;//y方向平移
static POINT point1[6]={100+X0,0+Y0,128+X0,0+Y0,48+X0,80+Y0,150+X0,80+Y0,170+X0,100+Y0,0+X0,100+Y0};//多边形的顶点坐标数组
static POINT point2[6]={0+X0,100+Y0,20+X0,120+Y0,220+X0,120+Y0,128+X0,28+Y0,113+X0,43+Y0,170+X0,100+Y0};
static POINT point3[6]={76+X0,80+Y0,128+X0,28+Y0,220+X0,120+Y0,228+X0,100+Y0,128+X0,0+Y0,48+X0,80+Y0};
HPEN hPen; //画笔
hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); //红色画笔
SelectObject(hdc,hPen);//设置画笔颜色
LOGBRUSH brushRed={BS_SOLID,0x000000ff,HS_VERTICAL};//红色画刷
LOGBRUSH brushYellow={BS_SOLID,0x0000ffff,HS_VERTICAL};//黄色画刷
LOGBRUSH brushBlue={BS_SOLID,0x00ff0000,HS_VERTICAL};//红色画刷
SelectObject (hdc, CreateBrushIndirect(&brushRed)) ;
SetPolyFillMode(hdc,ALTERNATE);
Polygon(hdc,point1,6);
SelectObject (hdc, CreateBrushIndirect(&brushYellow));
Polygon(hdc,point2,6);
SelectObject (hdc, CreateBrushIndirect(&brushBlue));
Polygon(hdc,point3,6);
return 0;
}