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
4
using namespace std;
5
6
7
struct Point
8

{
9
INT x, y;
10
};
11
12
struct Tri
13

{
14
Point a, b, c;
15
};
16
17
list< Tri > bufTri;
18
19
20
TCHAR szClassName[] = TEXT("QuizZJ");
21
TCHAR szWndName[] = TEXT("QuizZJ");
22
23
24
LRESULT 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
80
INT 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