Visual C++ 6.0

Windows C 图形程序设计

2003.3.3.

// Windows C 图形程序设计 WCTCS.cpp
// 定义应用程序进入点

#include <windows.h>
#include <math.h>
#include "WCTCS.h"
#include "string.h"
#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

// 公有变量
HINSTANCE hInst; // 当前实例
HWND hWndMain;
WPARAM wParam;
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏
TCHAR szWindowClass[MAX_LOADSTRING];

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

// Windows 应用程序入口点 WinMain
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// 初始化公有字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WCTCS, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// 初始化窗口,注册窗口 nCmdShow
if (!InitInstance (hInstance, nCmdShow)) 
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WCTCS);

// 主消息循环,检索消息 GetMessage
while (GetMessage(&msg, NULL, 0, 0)) 
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}

// 注册窗口类,填写窗口属性 RegisterClass
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX); 

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WCTCS);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
// 工作区背景颜色(画刷)
// +2 +1 +0 -1 -2 -3
// 黑 白 浅灰 灰 蓝 浅蓝
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW-3);
wcex.lpszMenuName = (LPCSTR)IDC_WCTCS;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

// 保存实例句柄
hInst = hInstance; 

// 创建窗口 CreateWindow
hWnd = CreateWindow(
szWindowClass, 
// 窗口标题栏
"Visual C++ 6.0 Windows C 图形程序设计 ddxss",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 
CW_USEDEFAULT, 0, 
NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

// 显示窗口 ShowWindow
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}

// 窗口过程函数 WndProc,接收和处理消息
LRESULT CALLBACK WndProc(
HWND hWnd,
UINT message, 
WPARAM wParam,
LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

TCHAR szWCTCS[MAX_LOADSTRING];


// 装载字符串 "WCTCS"
LoadString(hInst, IDS_WCTCS, szWCTCS, MAX_LOADSTRING);

double x0=400.0,y0=270.0,x,y,x1=x0-220.0,x2=x0+220.0;
switch (message) 
{
case WM_COMMAND:
wmId = LOWORD(wParam); 
wmEvent = HIWORD(wParam); 

// 菜单选择
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;

case WM_PAINT:
// 画图准备 BeginPaint
hdc = BeginPaint(hWnd, &ps);

// 添加画图代码
hdc = GetDC(hWnd); // 检取设备描述表

// 矩形紫绿青彩色边框线

// 上边框紫线线宽2
for(x=x0-390.0;x<x0+390.0;x+=0.01)
{
for(y=y0-240.0;y<y0-238.0;y++)
{
SetPixel(hdc,int(x),int(y),RGB(255,0,255));
}
}

// 右边框紫线
for(y=y0-240.0;y<y0+240.0;y+=0.01)
{
for(x=x0+390.0;x>x0+388.0;x--)
{
SetPixel(hdc,int(x),int(y),RGB(255,0,255));
}
}

// 下边框紫线
for(x=x0+390.0;x>x0-390.0;x-=0.01)
{
for(y=y0+240.0;y>y0+238.0;y--)
{
SetPixel(hdc,int(x),int(y),RGB(255,0,255));
}
}

// 左边框紫线
for(y=y0+240.0;y>y0-240.0;y-=0.01)
{
for(x=x0-390.0;x<x0-388.0;x++)
{
SetPixel(hdc,int(x),int(y),RGB(255,0,255));
}
}

// 上边框绿线宽10
for(x=x0-380.0;x<x0+380.0;x+=0.03)
{
for(y=y0-235.0;y<y0-225.0;y++)
{
SetPixel(hdc,int(x),int(y),RGB(0,255,0));
}
}

// 右边框绿线
for(y=y0-235.0;y<y0+236.0;y+=0.03)
{
for(x=x0+385.0;x>x0+375.0;x--)
{
SetPixel(hdc,int(x),int(y),RGB(0,255,0));
}
}

// 下边框绿线
for(x=x0+380.0;x>x0-380.0;x-=0.03)
{
for(y=y0+235.0;y>y0+225.0;y--)
{
SetPixel(hdc,int(x),int(y),RGB(0,255,0));
}
}

// 左边框绿线
for(y=y0+235.0;y>y0-235.0;y-=0.03)
{
for(x=x0-385.0;x<x0-375.0;x++)
{
SetPixel(hdc,int(x),int(y),RGB(0,255,0));
}
}

// 上边框青线宽2
for(x=x0-372.0;x<x0+372.0;x+=0.01)
{
for(y=y0-222.0;y<y0-220.0;y++)
{
SetPixel(hdc,int(x),int(y),RGB(0,255,255));
}
}

//右边框青线
for(y=y0-222.0;y<y0+222.0;y+=0.01)
{
for(x=x0+372.0;x>x0+370.0;x--)
{
SetPixel(hdc,int(x),int(y),RGB(0,255,255));
}
}

//下边框青线
for(x=x0+372.0;x>x0-372.0;x-=0.01)
{
for(y=y0+222.0;y>y0+220.0;y--)
{
SetPixel(hdc,int(x),int(y),RGB(0,255,255));
}
}

//左边框青线
for(y=y0+222.0;y>y0-220.0;y-=0.01)
{
for(x=x0-372.0;x<x0-370.0;x++)
{
SetPixel(hdc,int(x),int(y),RGB(0,255,255));
}
}

// 绿色草地
for(x=x0-314.0;x<x0+314.0;x+=0.1)
{
for(y=y0+100.0;y<y0+150.0;y++)
{
SetPixel(hdc,int(x),int(y),RGB(0,255,0));
}
}

//左边竖细彩色线
for(y=y0+100.0;y>y0-50.0;y-=0.03)
{
SetPixel(hdc,int(x1),int(y),RGB(255,0,0));
SetPixel(hdc,int(x1+5),int(y),RGB(0,255,0));
SetPixel(hdc,int(x1+10),int(y),RGB(0,50,255));
SetPixel(hdc,int(x1+15),int(y),RGB(255,255, 0));
SetPixel(hdc,int(x1+20),int(y),RGB(255,0,255));

SetPixel(hdc,int(x1),int(y),RGB(0,50,255));
SetPixel(hdc,int(x1-5),int(y),RGB(0,255,0));
SetPixel(hdc,int(x1-10),int(y),RGB(255,0,0));
SetPixel(hdc,int(x1-15),int(y),RGB(0,255,255));
SetPixel(hdc,int(x1-20),int(y),RGB(255,255,0,));
}

//中间粗竖线
// 紫线宽50
for(y=y0+100.0;y>y0-100.0;y-=0.03)
{
for(x=x0-150.0;x<x0-100.0;x++)
{
SetPixel(hdc,int(x),int(y),RGB(255,0,255));
}
}

// 黄线宽100
for(y=y0+100.0;y>y0-150.0;y-=0.05)
{
for(x=x0-50.0;x<x0+50.0;x++)
{
SetPixel(hdc,int(x),int(y),RGB(255,255,0));
}
}

// 青线宽50
for(y=y0+100.0;y>y0-100.0;y-=0.03)
{
for(x=x0+100.0;x<x0+150.0;x++)
{
SetPixel(hdc,int(x),int(y),RGB(0,255,255));
}
}

//右边竖细彩色线
for(y=y0+100.0;y>y0-50.0;y-=0.03)
{
SetPixel(hdc,int(x2),int(y),RGB(255,0,0));
SetPixel(hdc,int(x2+5),int(y),RGB(0,255,0));
SetPixel(hdc,int(x2+10),int(y),RGB(0,50,255));
SetPixel(hdc,int(x2+15),int(y),RGB(255,255, 0));
SetPixel(hdc,int(x2+20),int(y),RGB(255,0,255));

SetPixel(hdc,int(x2),int(y),RGB(0,50,255));
SetPixel(hdc,int(x2-5),int(y),RGB(0,255,0));
SetPixel(hdc,int(x2-10),int(y),RGB(255,0,0));
SetPixel(hdc,int(x2-15),int(y),RGB(0,255,255));
SetPixel(hdc,int(x2-20),int(y),RGB(255,255,0,));
}

ReleaseDC(hWnd,hdc);
RECT rt;
GetClientRect(hWnd, &rt);

// 输出文本 DrawText
DrawText(hdc, szWCTCS, strlen(szWCTCS), &rt, DT_CENTER);

// 结束画图 EndPaint
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}