xml地图|网站地图|网站标签 [设为首页] [加入收藏]

智能硬件

当前位置:美高梅游戏网站 > 智能硬件 > 第五章 绘图基础(BEZIER)

第五章 绘图基础(BEZIER)

来源:http://www.gd-chuangmei.com 作者:美高梅游戏网站 时间:2019-11-13 15:19

图片 1图片 2

图片 3

  1 /*-----------------------------
  2 BEZIER.C -- Bezier Splines Demo
  3 (c) Charles Petzold, 1998
  4 -----------------------------*/
  5 
  6 #include <Windows.h>
  7 
  8 LRESULT CALLBACK WndPorc(HWND, UINT, WPARAM, LPARAM);
  9 
 10 int WINAPI WinMain( __in HINSTANCE hInstance
 11                     , __in_opt HINSTANCE hPrevInstance
 12                     , __in LPSTR lpCmdLine
 13                     , __in int nShowCmd )
 14 {
 15     static TCHAR szAppName[] = TEXT("Bezier");
 16     HWND hwnd;
 17     MSG msg;
 18     WNDCLASS wndclass;
 19 
 20     wndclass.style = CS_HREDRAW | CS_VREDRAW;
 21     wndclass.lpfnWndProc = WndPorc;
 22     wndclass.cbClsExtra = 0;
 23     wndclass.cbWndExtra = 0;
 24     wndclass.hInstance = hInstance;
 25     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 26     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
 27     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 28     wndclass.lpszMenuName = NULL;
 29     wndclass.lpszClassName = szAppName;
 30 
 31     if (!RegisterClass(&wndclass))
 32     {
 33         MessageBox(NULL, TEXT("Program requires Windows NT!")
 34             , szAppName, MB_ICONERROR);
 35         return 0;
 36     }
 37 
 38     hwnd = CreateWindow(szAppName, TEXT("Bezier Splines")
 39         , WS_OVERLAPPEDWINDOW
 40         , CW_USEDEFAULT, CW_USEDEFAULT
 41         , CW_USEDEFAULT, CW_USEDEFAULT
 42         , NULL, NULL, hInstance, NULL);
 43 
 44     ShowWindow(hwnd, nShowCmd);
 45     UpdateWindow(hwnd);
 46 
 47     while (GetMessage(&msg, NULL, 0, 0))
 48     {
 49         TranslateMessage(&msg);
 50         DispatchMessage(&msg);
 51     }
 52 
 53     return msg.wParam;
 54 }
 55 
 56 void DrawBezier(HDC hdc, POINT apt[])
 57 {
 58     PolyBezier(hdc, apt, 4);
 59 
 60     MoveToEx(hdc, apt[0].x, apt[0].y, NULL);
 61     LineTo(hdc, apt[1].x, apt[1].y);
 62 
 63     MoveToEx(hdc, apt[2].x, apt[2].y, NULL);
 64     LineTo(hdc, apt[3].x, apt[3].y);
 65 }
 66 
 67 LRESULT CALLBACK WndPorc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 68 {
 69     static POINT apt[4];
 70     HDC hdc;
 71     int cxClient, cyClient;
 72     PAINTSTRUCT ps;
 73 
 74     switch (message)
 75     {
 76     case WM_SIZE:
 77         cxClient = LOWORD(lParam);
 78         cyClient = HIWORD(lParam);
 79 
 80         apt[0].x = cxClient / 4;
 81         apt[0].y = cyClient / 2;
 82         
 83         apt[1].x = cxClient / 2;
 84         apt[1].y = cyClient / 4;
 85 
 86         apt[2].x = cxClient / 2;
 87         apt[2].y = 3 * cyClient / 4;
 88 
 89         apt[3].x = 3 * cxClient / 4;
 90         apt[3].y = cyClient / 2;
 91 
 92         return 0;
 93 
 94     case WM_LBUTTONDOWN:
 95     case WM_RBUTTONDOWN:
 96     case WM_MOUSEMOVE:
 97         if (wParam & MK_LBUTTON || wParam & MK_RBUTTON)
 98         {
 99             hdc = GetDC(hwnd);
100             SelectObject(hdc, GetStockObject(WHITE_PEN));
101 
102             DrawBezier(hdc, apt);
103 
104             if (wParam & MK_LBUTTON)
105             {
106                 apt[1].x = LOWORD(lParam);
107                 apt[1].y = HIWORD(lParam);
108             }
109 
110             if (wParam & MK_RBUTTON)
111             {
112                 apt[2].x = LOWORD(lParam);
113                 apt[2].y = HIWORD(lParam);
114             }
115 
116             SelectObject(hdc, GetStockObject(BLACK_PEN));
117 
118             DrawBezier(hdc, apt);
119 
120             ReleaseDC(hwnd, hdc);
121         }
122         return 0;
123 
124     case WM_PAINT:
125         InvalidateRect(hwnd, NULL, TRUE);
126         hdc = BeginPaint(hwnd, &ps);
127 
128         DrawBezier(hdc, apt);
129 
130         EndPaint(hwnd, &ps);
131         return 0;
132 
133     case WM_DESTROY:
134         PostQuitMessage(0);
135         return 0;
136     }
137 
138     return DefWindowProc(hwnd, message, wParam, lParam);
139 }

 代码如下:

BEZIER.C

图片 4图片 5交互式绘图

BEZIER程序的显示结果

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 
 9 namespace 交互绘图
10 {
11     public partial class Form1 : Form
12     {
13         public Form1()
14         {
15             InitializeComponent();
16         }
17         int x1, y1;
18         int flat = 1;
19         bool b1, b2, b3, b4, b5;
20 
21         private void pictureBox1_Click(object sender, EventArgs e)
22         {
23            
24         }
25 
26         private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
27         {
28              int x2, y2;
29             Graphics g = pictureBox1.CreateGraphics();
30             Pen p1 = new Pen(Color.Chocolate, 3);
31             if (b1 == true)
32             {
33                 if (flat == 1)
34                 {
35                     x1 = e.X; y1 = e.Y; flat = 0;
36                 }
37                 else
38                 {
39                     x2 = e.X; y2 = e.Y;
40                     g.DrawLine(p1, x1, y1, x2, y2);
41                     flat = 1;
42                 }
43             }
44             if (b2 == true)
45             {
46                 if (flat == 1)
47                 {
48                     x1 = e.X; y1 = e.Y; flat = 0;
49                 }
50                 else
51                 {
52                     x2 = e.X; y2 = e.Y;
53                     g.DrawEllipse(p1, x1, y1, (float)Math.Abs(x1 - x2), (float)Math.Abs(y1 - y2));
54                     flat = 1;
55                 }
56             }
57             if (b3 == true)
58             {
59                 if (flat == 1)
60                 {
61                     x1 = e.X; y1 = e.Y; flat = 0;
62                 }
63                 else
64                 {
65                     x2 = e.X; y2 = e.Y;
66                     g.DrawRectangle(p1, x1, y1, (float)Math.Abs(x1 - x2), (float)Math.Abs(y1 - y2));
67                     flat = 1;
68                 }
69             }
70             b5 = true;
71         }
72 
73         private void 直线ToolStripMenuItem_Click(object sender, EventArgs e)
74         {
75             b1 = true; flat = 1; b2 = false; b3 = false; b4 = false;
76         }
77 
78         private void 圆ToolStripMenuItem_Click(object sender, EventArgs e)
79         {
80             b1 = false; flat = 1; b2 = true; b3 = false; b4 = false;
81         }
82 
83         private void 矩形ToolStripMenuItem_Click(object sender, EventArgs e)
84         {
85             b1 =false; flat = 1; b2 = false; b3 =true; b4 = false;
86         }
87 
88         private void Form1_Load(object sender, EventArgs e)
89         {
90             pictureBox1.BackColor = Color.DarkGray;
91         }
92     }
93 } 

图片 6

图片 7图片 8

图片 9图片 10

图片 11图片 12调用控件打印预览

本文由美高梅游戏网站发布于智能硬件,转载请注明出处:第五章 绘图基础(BEZIER)

关键词: