Function BeginPaint(aH:HWND):LPPAINTSTRUCT;
var
 ret   : LPPAINTSTRUCT;
 wnd,c : PWindow;
begin
  wnd := wGetWndByHWND(aH);
  if wnd = nil then
   begin
    BeginPaint := nil;
    LAstError := ERR_BADHANDLE;
    Exit;
   end;
  New(ret);
  with ret^ do begin
    Font := DefaultFont;
    FontSize := 1;
    FontJus := ((LeftText) shl 4) OR CenterText;
    Style := SolidFill;
    Color := Black;
    BgColor := White;
  end;

  GetGlobalRect(aH,@ret^.gPos);

 with ret^.gPos do
   SetViewPort(A.X,A.Y,A.X+B.X,A.Y+B.Y,ClipOn);
 HideMouse;
 BeginPaint := Ret;
 repeat until (Port[$03DA] and 8) <> 8;
 repeat until (Port[$03DA] and 8) =  8;
end;
{-----------------------------------}
Procedure EndPaint(aP:LPPAINTSTRUCT);
begin
 SetViewPort(0,0,ScrMaxX,ScrMaxY,ClipOn);
 ShowMouse;
 Dispose(aP);
end;
Procedure wSetBgColor(aP:LPPAINTSTRUCT;aNewColor:INT8);
begin
 aP^.BgColor := aNewColor;
end;
Procedure wSetColor(aP:LPPAINTSTRUCT;aNewColor:INT8);
begin
 aP^.Color := aNewColor;
end;
Procedure wSetFont(aP:LPPAINTSTRUCT;aNewFont:INT8);
begin
 aP^.Font := aNewFont;
end;
Procedure wSetStyle(aP:LPPAINTSTRUCT;aNewStyle:INT8);
begin
 aP^.Style := aNewStyle;
end;
Procedure wSetFontSize(aP:LPPAINTSTRUCT;aNewColor:INT8);
begin
 aP^.FontSize := aNewColor;
end;
Procedure wSetFontJustify(aP:LPPAINTSTRUCT;aNewH,aNewV:INT8);
begin
 aP^.FontJus := (aNewH shl 4) OR aNewV;
end;
Function wMoveToEx(aP:LPPAINTSTRUCT;X,Y:INT16;lP:LPPOINT):Boolean;
begin
  if (X>=0) AND (Y>=0) AND (X<=aP^.gPos.B.X) AND (Y<=aP^.gPos.B.Y) then
   begin
    if lp <> nil then
     begin
      lp^.X := GetX;
      lp^.Y := GetY;
     end;
    MoveTo(X,Y);
    wMoveToEx := true;
   end
  else
   wMoveToEx := False;
end;{----------}
Function wLineTo(aP:LPPAINTSTRUCT;X,Y:INT16):Boolean;
begin
  if (X>=0) AND (Y>=0) AND (X<=aP^.gPos.B.X) AND (Y<=aP^.gPos.B.Y) then
   begin
    SetColor(aP^.Color);
    LineTo(X,Y);
    wLineTo:=true;
   end
  else
   wLineTo := False;
end;
Function wTextOut(aP:LPPAINTSTRUCT;X,Y:INT16;aStr:PString):Boolean;
begin
  if (X>=0) AND (Y>=0) AND (X<=aP^.gPos.B.X) AND (Y<=aP^.gPos.B.Y) then
  with aP^ do
   begin
    SetColor(Color);
    SetTextStyle(Font,HorizDir,FontSize);
    SetTextJustify((FontJus shr 4) and $F, FontJus AND $F);
    OutTextXY(X,Y,aStr^);
    wTextOut:=true;
   end
  else
   wTextOut := False;
end;
Function wRectangle(aP:LPPAINTSTRUCT;X1,Y1,X2,Y2:INT16):Boolean;
begin
    SetColor(aP^.Color);
    Rectangle(X1,Y1,X2,Y2);
    wRectangle:=true;
end;

Function wBar(aP:LPPAINTSTRUCT;X1,Y1,X2,Y2:INT16):Boolean;
begin
    SetColor(aP^.Color);
    SetFillStyle(aP^.Style,aP^.BGColor);
    Bar(X1,Y1,X2,Y2);
    wBar:=true;
end;

Function wLine(aP:LPPAINTSTRUCT;X1,Y1,X2,Y2:INT16):Boolean;
begin
    SetColor(aP^.Color);
    Line(X1,Y1,X2,Y2);
    wLine:=true;
end;

Function wFillCircle(aP:LPPAINTSTRUCT;X1,Y1,Rad:INT16):Boolean;
begin
    SetColor(aP^.Color);
    SetFillStyle(aP^.Style,aP^.BGColor);
    FillEllipse(X1,Y1,rad,rad);
    wFillCircle:=true;
end;
