WinAPI: Polyline - 绘制一组连续线段

本文介绍了一个使用Polyline函数绘制连续直线的例子,并展示了如何通过定义点数组来实现特定图形的绘制。此示例有助于理解Polyline与Polygon函数的区别及应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//声明:
Polyline(
  DC: HDC;       {设备环境句柄}
  var Points;    {点数组}
  Count: Integer {数组元素个数}
): BOOL;

{Polyline 与 Polygon 的参数一模一样, 但 Polygon 能自动闭合}

 
 
 
 
 

 

 

  

//绘制连续直线 Polyline
procedure TForm1.FormPaint(Sender: TObject);
var
  ps: array[0..5] of TPoint;
begin
  ps[0].X := 50;
  ps[0].Y := 50;
  ps[1].X := 100;
  ps[1].Y := 50;
  ps[2].X := 125;
  ps[2].Y := 75;
  ps[3].X := 100;
  ps[3].Y := 100;
  ps[4].X := 50;
  ps[4].Y := 100;
  ps[5].X := 25;
  ps[5].Y := 75;

  Polyline(Canvas.Handle, ps, 6);
end;

 
 
 
 
 

 

 

  

//效果图:
26153821_9A18.png

转载于:https://my.oschina.net/hermer/blog/320627

#include <windows.h> #include <cmath> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("SinWave"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("Program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, TEXT("Dynamic Sin Wave"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static double phase = 0.0; static UINT_PTR timerId = 0; switch (message) { case WM_CREATE: timerId = SetTimer(hwnd, 1, 50, NULL); // 每50毫秒触发一次WM_TIMER return 0; case WM_TIMER: phase += 0.1; // 更新相位 InvalidateRect(hwnd, NULL, TRUE); // 触发重绘 return 0; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); RECT rect; GetClientRect(hwnd, &rect); // 绘制背景 FillRect(hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH)); // 绘制Sin曲线 int yCenter = rect.bottom / 2; int amplitude = 100; HPEN hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 255)); SelectObject(hdc, hPen); MoveToEx(hdc, 0, yCenter, NULL); for (int x = 0; x < rect.right; x++) { double value = sin(phase + x * 0.05); // 调整频率 int y = yCenter - static_cast<int>(amplitude * value); LineTo(hdc, x, y); } DeleteObject(hPen); EndPaint(hwnd, &ps); return 0; } case WM_DESTROY: KillTimer(hwnd, timerId); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }
最新发布
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值