看来高手不仅是水平高而且还这么负责:)值得我学习呀!
不过我用的是DrawCurve和DrawClosedCurve,不如你给的函数灵活
代码如下:
program GDITESTDrawCurve;
uses
Windows,
Messages,
SysUtils,
GDIPAPI,
GDIPOBJ;
Procedure OnPaint(DC: HDC);
var
graphics : TGPGraphics;
Pen: TGPPen;
const
points: array[0..4] of TPoint =
((x: 0 ; y: 100),
(x: 50 ; y: 80 ),
(x: 100; y: 20 ),
(x: 150; y: 80 ),
(x: 200; y: 100));
begin
graphics := TGPGraphics.Create(DC);
Pen:= TGPPen.Create(MakeColor(255, 0, 0, 255));
graphics.DrawCurve(pen, PPoint(@points), 5);
Pen.Free;
graphics.Free;
end;
function WndProc(Wnd : HWND; message : UINT; wParam : Cardinal; lParam: Integer) : Integer; stdcall;
var
Handle: HDC;
ps: PAINTSTRUCT;
begin
case message of
WM_PAINT:
begin
Handle := BeginPaint(Wnd, ps);
OnPaint(Handle);
EndPaint(Wnd, ps);
result := 0;
end;
WM_DESTROY:
begin
PostQuitMessage(0);
result := 0;
end;
else
result := DefWindowProc(Wnd, message, wParam, lParam);
end;
end;
var
hWnd : THandle;
Msg : TMsg;
wndClass : TWndClass;
begin
wndClass.style := CS_HREDRAW or CS_VREDRAW;
wndClass.lpfnWndProc := @WndProc;
wndClass.cbClsExtra := 0;
wndClass.cbWndExtra := 0;
wndClass.hInstance := hInstance;
wndClass.hIcon := LoadIcon(0, IDI_APPLICATION);
wndClass.hCursor := LoadCursor(0, IDC_ARROW);
wndClass.hbrBackground := HBRUSH(GetStockObject(WHITE_BRUSH));
wndClass.lpszMenuName := nil;
wndClass.lpszClassName := 'GettingStarted';
RegisterClass(wndClass);
hWnd := CreateWindow(
'GettingStarted', // window class name
'Drawing Cardinal Splines', // window caption
WS_OVERLAPPEDWINDOW, // window style
Integer(CW_USEDEFAULT), // initial x position
Integer(CW_USEDEFAULT), // initial y position
Integer(CW_USEDEFAULT), // initial x size
Integer(CW_USEDEFAULT), // initial y size
0, // parent window handle
0, // window menu handle
hInstance, // program instance handle
nil); // creation parameters
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
while(GetMessage(msg, 0, 0, 0)) do
begin
TranslateMessage(msg);
DispatchMessage(msg);
end;
end.
program GDITESTDrawClosedCurve;
uses
Windows,
Messages,
SysUtils,
GDIPAPI,
GDIPOBJ;
Procedure OnPaint(DC: HDC);
var
graphics : TGPGraphics;
pen: TGPpen;
const
points: array[0..5] of TPoint =
((x: 60 ; y: 60),
(x: 150; y: 80),
(x: 200; y: 40),
(x: 180; y: 120),
(x: 120; y: 100),
(x: 80 ; y: 160));
begin
graphics := TGPGraphics.Create(DC);
pen:= TGPPen.Create(MakeColor(255, 0, 0, 255));
graphics.DrawClosedCurve(pen, PPoint(@points), 6);
Pen.Free;
graphics.Free;
end;
function WndProc(Wnd : HWND; message : UINT; wParam : Cardinal; lParam: Integer) : Integer; stdcall;
var
Handle: HDC;
ps: PAINTSTRUCT;
begin
case message of
WM_PAINT:
begin
Handle := BeginPaint(Wnd, ps);
OnPaint(Handle);
EndPaint(Wnd, ps);
result := 0;
end;
WM_DESTROY:
begin
PostQuitMessage(0);
result := 0;
end;
else
result := DefWindowProc(Wnd, message, wParam, lParam);
end;
end;
var
hWnd : THandle;
Msg : TMsg;
wndClass : TWndClass;
begin
wndClass.style := CS_HREDRAW or CS_VREDRAW;
wndClass.lpfnWndProc := @WndProc;
wndClass.cbClsExtra := 0;
wndClass.cbWndExtra := 0;
wndClass.hInstance := hInstance;
wndClass.hIcon := LoadIcon(0, IDI_APPLICATION);
wndClass.hCursor := LoadCursor(0, IDC_ARROW);
wndClass.hbrBackground := HBRUSH(GetStockObject(WHITE_BRUSH));
wndClass.lpszMenuName := nil;
wndClass.lpszClassName := 'GettingStarted';
RegisterClass(wndClass);
hWnd := CreateWindow(
'GettingStarted', // window class name
'Drawing Cardinal Splines', // window caption
WS_OVERLAPPEDWINDOW, // window style
Integer(CW_USEDEFAULT), // initial x position
Integer(CW_USEDEFAULT), // initial y position
Integer(CW_USEDEFAULT), // initial x size
Integer(CW_USEDEFAULT), // initial y size
0, // parent window handle
0, // window menu handle
hInstance, // program instance handle
nil); // creation parameters
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
while(GetMessage(msg, 0, 0, 0)) do
begin
TranslateMessage(msg);
DispatchMessage(msg);
end;
end.
用起来是不爽,关键是没有每一段的函数表达式!还是回到老路上吧,是不是闭合曲线不能用自然样条呀?你给的方法用节点处的一阶导数法表示那又如何表示自然样条呢?