zwz_good  

program Sinewave;

uses
  Windows,
  Messages,
  Math;
const
  NUM = 1000;

function WndProc(hWindow: HWND; msg, wParam, lParam: LongInt): LRESULT; stdcall;
const
  TWOPI = (2 * 3.1415926);
{$J+}
  cxClient: integer = 0;
  cyClient: integer = 0;
  hPen: HPEN = 0;
{$J-}
var
  ps: TPaintStruct;
  i: Integer;
  hdc1: HDC;
  hOldPen: HPEN;
  apt: array[0..NUM-1] of TPoint;
  pt: TPoint;
begin
  Result:= 0;
  case msg of
  WM_CREATE:
  begin
    hPen:= CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
  end;
  WM_DESTROY:
  begin
    DeleteObject(hPen);
    PostQuitMessage(0);
  end;
  WM_SIZE:
  begin
    cxClient:= LoWord(lParam);
    cyClient:= HiWord(lParam);
  end;
  WM_PAINT:
  begin
    hdc1:= BeginPaint(hWindow, ps);
    //pi;
    //for i := 0 to NUM - 1 do
    //更改画笔颜色
    SelectObject(hdc1, hPen);
    //更改坐标系
    SetMapMode(hdc1, MM_LOMETRIC);
    SetViewportOrgEx(hdc1, 0, cyClient div 2, nil);
    //注意以下绘制函数使用的逻辑单位都不是像素,而是0.01mm
    //先绘制一条水平线即X轴
    pt.X:= cxClient;
    pt.Y:= cyClient;
    DPtoLP(hdc1, pt, 1);
    //此时pt.x, pt.y值表示为客户区右下角的点,用0.01单位
//    MoveToEx(hdc1, 0, 0, nil);
//    LineTo(hdc1, pt.X, pt.Y);
    MoveToEx(hdc1, 0, 0, nil);
    LineTo(hdc1, pt.X, 0); //绘制X轴
    MoveToEx(hdc1, 0, 0, nil);
    LineTo(hdc1, 0, -pt.y); //绘制Y轴,注意由于pt.y表示的是客户区最底端的值因此它为负值,
                            //为了绘制上半轴,因此要添加负号。
    //计算Sin值,并保存到数组中
    for I := 0 to NUM-1 do
    begin
      //apt[i].X:= Trunc(I * (2*pi / NUM));
      apt[i].X:= I * pt.X div NUM;
      //绘制X轴的刻度
      MoveToEx(hdc1, apt[i].X, 0, nil);
      LineTo(hdc1, apt[i].X, 11);
      //函数值计算出来后,需要放大几倍,否则取整后为零。
      apt[i].Y:= Trunc(Sin(I * (2 * Pi / NUM)) * (-pt.Y div 2));

    end;

    //绘制Sin函数
    Polyline(hdc1, apt, NUM);
    EndPaint(hWindow, ps);
  end
  else
    Result:= DefWindowProc(hWindow, msg, wParam, lParam);
  end;
end;


const
  szAppName = 'SineWave';
var
  wndclass1: TWndClass;
  hWindow: HWND;
  msg: TMsg;
begin
  wndclass1.style:= CS_VREDRAW or CS_HREDRAW;
  wndclass1.lpfnWndProc:= @WndProc;
  wndclass1.cbClsExtra:= 0;
  wndclass1.cbWndExtra:= 0;
  wndclass1.hInstance:= HInstance;
  wndclass1.hIcon:= LoadIcon(0, IDI_APPLICATION);
  wndclass1.hCursor:= LoadCursor(0, IDC_ARROW);
  wndclass1.hbrBackground:= GetStockObject(WHITE_BRUSH);
  wndclass1.lpszMenuName:= nil;
  wndclass1.lpszClassName:= szAppName;

  if RegisterClass(wndclass1) = 0 then
  begin
    MessageBox(0, 'Program requires Windows NT!', szAppName, MB_ICONERROR);
    Exit;
  end;

  hWindow:= CreateWindow(szAppName, 'Sine Wave Using Polyline', WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, HInstance, nil);

  ShowWindow(hWindow, CmdShow);
  UpdateWindow(hWindow);

  while GetMessage(msg, 0, 0, 0) do
  begin
    TranslateMessage(msg);
    DispatchMessage(msg);
  end;
end.

posted on 2009-04-25 12:40  zwz_good  阅读(148)  评论(0编辑  收藏  举报