Submit:source file : 10092130XXX.pdf (c/cpp==>pdf)
executable : 10092130XXX.exe
编写一个应用程序实现用户通过交互方式画m(m<100)个三角形。
用户操作方法:按鼠标左键确定三角形的第一个顶点,
然后移动鼠标至另一位置,放开左键确定三角形的第二个顶点,
这时显示第一条边;再按鼠标左键并移动鼠标至另一位置,
放开左键确定三角形的第三个顶点,这时显示第二条边,
并将第三个顶点与第一个顶点相连画出一个三角形。
1. 画一个三角形(50%)
2. 交互方式画若干个三角形(40%)
3. 在窗口刷新时显示已画过的所有三角形(10%)
我的代码(这个是有问题的,修改版懒得贴了):
1#include <Windows.h>
2#include <list>
3
4using namespace std;
5
6
7struct Point
8{
9 INT x, y;
10};
11
12struct Tri
13{
14 Point a, b, c;
15};
16
17list< Tri > bufTri;
18
19
20TCHAR szClassName[] = TEXT("QuizZJ");
21TCHAR szWndName[] = TEXT("QuizZJ");
22
23
24LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
25 static INT iPt;
26 static Tri tmpTri;
27
28 PAINTSTRUCT ps;
29 HDC hdc;
30 list< Tri >::iterator ite;
31
32 switch ( uMsg ) {
33 case WM_CREATE :
34 iPt = 0;
35 return 0;
36
37 case WM_LBUTTONDOWN :
38 if ( iPt == 0 ) {
39 tmpTri.a.x = LOWORD(lParam);
40 tmpTri.a.y = HIWORD(lParam);
41 ++iPt;
42 }
43 return 0;
44
45 case WM_LBUTTONUP :
46 if ( iPt == 1 ) {
47 tmpTri.b.x = LOWORD(lParam);
48 tmpTri.b.y = HIWORD(lParam);
49 ++iPt;
50 ::InvalidateRect( hWnd, NULL, FALSE );
51 }
52 else if ( iPt == 2 ) {
53 tmpTri.c.x = LOWORD(lParam);
54 tmpTri.c.y = HIWORD(lParam);
55 bufTri.push_back( tmpTri );
56 iPt = 0;
57 ::InvalidateRect( hWnd, NULL, FALSE );
58 }
59 return 0;
60
61 case WM_PAINT :
62 hdc = ::BeginPaint( hWnd, &ps );
63 for ( ite = bufTri.begin(); ite != bufTri.end(); ++ite ) {
64 ::MoveToEx( hdc, ite->a.x, ite->a.y, NULL );
65 ::LineTo( hdc, ite->b.x, ite->b.y );
66 ::LineTo( hdc, ite->c.x, ite->c.y );
67 ::LineTo( hdc, ite->a.x, ite->a.y );
68 }
69 if ( iPt == 2 ) {
70 ::MoveToEx( hdc, tmpTri.a.x, tmpTri.a.y, NULL );
71 ::LineTo( hdc, tmpTri.b.x, tmpTri.b.y );
72 }
73 ::EndPaint( hWnd, &ps );
74 return 0;
75 }
76 return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
77}
78
79
80INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, INT iCmd ) {
81 WNDCLASS wc;
82 wc.cbClsExtra = 0;
83 wc.cbWndExtra = 0;
84 wc.hbrBackground = (HBRUSH)::GetStockObject( WHITE_BRUSH );
85 wc.hCursor = ::LoadCursor( NULL, IDC_CROSS );
86 wc.hIcon = ::LoadIcon( NULL, IDI_APPLICATION );
87 wc.hInstance = hInst;
88 wc.lpfnWndProc = WndProc;
89 wc.lpszClassName = szClassName;
90 wc.lpszMenuName = NULL;
91 wc.style = CS_HREDRAW | CS_VREDRAW;
92
93 if ( ! ::RegisterClass( &wc ) ) {
94 ::MessageBox( NULL, TEXT("RegisterClass Failed!!"), TEXT("error"), MB_OK );
95 return 0;
96 }
97
98 HWND hWnd = ::CreateWindow( szClassName, szWndName,
99 WS_OVERLAPPEDWINDOW,
100 300, 300,
101 800, 600,
102 NULL,
103 NULL,
104 wc.hInstance,
105 NULL );
106 if ( ! hWnd ) {
107 ::MessageBox( NULL, TEXT("CreateWindow Failed!!"), TEXT("error"), MB_OK );
108 return 0;
109 }
110
111 ::ShowWindow( hWnd, iCmd );
112 ::UpdateWindow( hWnd );
113
114 MSG msg;
115 while ( ::GetMessage( &msg, NULL, 0, 0 ) ) {
116 ::TranslateMessage( &msg );
117 ::DispatchMessage( &msg );
118 }
119 return msg.wParam;
120}
121