// Windows C 图形程序设计 窗口管理 CKGL.cpp // 定义应用程序进入点 #include #include #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_CKGL, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 初始化窗口,注册窗口 nCmdShow if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_CKGL); // 主消息循环,检索消息 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_CKGL); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); // 工作区背景颜色(画刷) // +2 +1 +0 -1 -2 -3 // 黑 白 浅灰 灰 蓝 浅蓝 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW-3); wcex.lpszMenuName = (LPCSTR)IDC_CKGL; 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 图形程序设计 fsxss", 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; static int fnPenStyle=PS_SOLID; //画笔风格:实线 static int nPenWidth=2; //画笔宽度: static COLORREF crPenColor=2; //画笔颜色: static COLORREF crBrushColor=0; //画刷颜色: HPEN hPen; TCHAR szWCTCS[MAX_LOADSTRING]; // 装载字符串 "WCTCS" LoadString(hInst, IDS_CKGL, szWCTCS, MAX_LOADSTRING); // * 定义变量 double x0=400,x01=213,x02=588,x,x1=x02-100,x2=x02+100; double y0=270,y01=158,y02=382,y,ym=90; 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); // 检取设备描述表 // 1.用点函数黄绿青色水平垂直扫描刷新全屏 // 水平黄色扫描刷新全屏 for(y=y0-300;yx0+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;xx0+380.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+230.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;xx0+375.0;x--) { SetPixel(hdc,int(x),int(y),RGB(0,255,255)); } } //下边框青线 for(x=x0+377.0;x>x0-377.0;x-=0.01) { for(y=y0+227.0;y>y0+225.0;y--) { SetPixel(hdc,int(x),int(y),RGB(0,255,255)); } } //左边框青线 for(y=y0+227.0;y>y0-225.0;y-=0.01) { for(x=x0-377.0;xy0-50.0;y-=0.03) { SetPixel(hdc,int(x01+10),int(y),RGB(255,0,255)); SetPixel(hdc,int(x01+5),int(y),RGB(255,255, 0)); SetPixel(hdc,int(x01),int(y),RGB(255,0,0)); SetPixel(hdc,int(x01-5),int(y),RGB(0,255,0)); SetPixel(hdc,int(x01-10),int(y),RGB(0,50,255)); SetPixel(hdc,int(x01-15),int(y),RGB(0,255,0)); SetPixel(hdc,int(x01-20),int(y),RGB(255,0,0)); SetPixel(hdc,int(x01-25),int(y),RGB(0,255,255)); SetPixel(hdc,int(x01-30),int(y),RGB(255,255,0,)); } //中间粗直方图 // 红线宽50 for(y=y0+100;y>y0-80;y-=0.1) { for(x=x0-160.0;xy0-118;y-=0.1) { for(x=x0-50;xy0-80;y-=0.1) { for(x=x0+110;xy0-50.0;y-=0.03) { SetPixel(hdc,int(x02-15),int(y),RGB(0,255,255)); SetPixel(hdc,int(x02-10),int(y),RGB(255,0,0)); SetPixel(hdc,int(x02-5),int(y),RGB(0,255,0)); SetPixel(hdc,int(x02),int(y),RGB(0,50,255)); SetPixel(hdc,int(x02+5),int(y),RGB(0,255,0)); SetPixel(hdc,int(x02+10),int(y),RGB(0,50,255)); SetPixel(hdc,int(x02+15),int(y),RGB(255,255, 0)); SetPixel(hdc,int(x02+20),int(y),RGB(255,0,255)); SetPixel(hdc,int(x02+30),int(y),RGB(255,0,0)); } // 6. 保留紫绿青边框线扫描刷新窗口 // 水平紫色扫描线线宽6 for(y=y0-214.0;yx01-184;x-=0.01) { for(y=y0+220;y>y0+218;y--) { SetPixel(hdc,int(x),int(y),RGB(255,0,255)); } } //左边框紫线 for(y=y0+220;y>y0-220;y-=0.01) { for(x=x01-184;xx02-184;x-=0.01) { for(y=y01+107;y>y01+105;y--) { SetPixel(hdc,int(x),int(y),RGB(0,255,0)); } } //左边框绿线 for(y=y01+107;y>y01-107;y-=0.01) { for(x=x02-184;xx02-184;x-=0.01) { for(y=y02+107;y>y02+105;y--) { SetPixel(hdc,int(x),int(y),RGB(255,200,0)); } } //左边框黄线 for(y=y02+107;y>y02-107;y-=0.01) { for(x=x02-184;xy02-30;y-=0.01) { 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=y02+50;y>y02-50;y-=0.01) { for(x=x02-60;xy02-80;y-=0.01) { for(x=x02-20;xy02-50;y-=0.01) { for(x=x02+40;xy02-30;y-=0.01) { 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); // 10. 输出左窗口文本 DrawText TextOut(hdc, 86,int(y0-180), "Windows C 图形程序设计 窗口管理",31); TextOut(hdc, 50,int(y0-150), " 1. 用点函数黄绿青色水平垂直扫描刷新全屏",40); TextOut(hdc, 50,int(y0-120), " 2. 用点函数绘制紫绿青边框线",28); TextOut(hdc, 50,int(y0-90), " 3. 用直线函数绘制五角星图形",28); TextOut(hdc, 50,int(y0-60), " 4. 绘制圆形和圆弧",18); TextOut(hdc, 50,int(y0-30), " 5. 用点函数绘制水平线和直方图",30); TextOut(hdc, 50,int(y0), " 6. 保留紫绿青边框线扫描刷新窗口",32); TextOut(hdc, 50,int(y0+30), " 7. 绘制小窗口边框和扫描刷新小窗口",34); TextOut(hdc, 50,int(y0+60), " 8. 用直线函数绘制右上窗口五角星图形",36); TextOut(hdc, 50,int(y0+90), " 9. 用点函数绘制右下窗口水平线和直方图",38); TextOut(hdc, 50,int(y0+120), "10. 输出左窗口文本",18); TextOut(hdc,150,int(y0+150), "佛山 谢山水 2003.5.5.",21); // 结束画图 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; }