#include once "window9.bi"

Dim Shared As HWND wind___
Dim Shared As HFONT RezerveFont_
Dim Shared As Long H_BIT_FLAG,H_Alpha_FLAG,H_ALPHA_VALUE
Dim Shared As HDC MemDC,MainDC
Dim Shared As HBITMAP hbitmapMaindraw
Dim Shared As RECT Window9RectPos

Type Point_
	As Long x
	As Long y
End Type
sub win9SetDCPrint(byval pHdc As HDC) Export
	MemDC=pHdc
End sub

Function ImageStartDraw(ByVal hbitmap As hbitmap) As HDC Export
	H_BIT_FLAG=1
	MemDC = Cast(HDC,CreateCompatibleDC(0))
	hbitmapMaindraw=hbitmap
	SelectObject(Cast(HDC,MemDC),Cast(hbitmap,hbitmapMaindraw))
	Return MemDC
End Function

Function WindowStartDraw(ByVal hWin As HWND, _
						ByVal x As Long=0, ByVal y As Long=0, _
						ByVal w As Long=0, ByVal h As Long=0, _
						ByVal Alpha_FLAG As Long=0, _
						ByVal Alpha_VALUE As ULong=0) As HDC Export
	H_BIT_FLAG=0:H_Alpha_FLAG=Alpha_FLAG:H_Alpha_VALUE=Alpha_VALUE
	If w =0 Then w = WindowClientWidth(hWin)
	If h =0 Then h = WindowClientHeight(hWin)
	Window9RectPos.left   = x
	Window9RectPos.top    = y
	Window9RectPos.right  = w
	Window9RectPos.bottom = h
	wind___ = hWin
	If wind___ = GetDesktopWindow() Then
		MainDC  = GetWindowDC(hWin)
	Else
		MainDC  = GetDC(hWin)
	End If
	MemDC = CreateCompatibleDC(Maindc)
	hbitmapMaindraw = CreateCompatibleBitmap(Maindc, w, h)
	SelectObject(MemDC,hbitmapMaindraw)
	Return MemDC
End Function

Function StopDraw() As Integer Export
	If H_BIT_FLAG=0 Then
		If H_Alpha_FLAG=0 Then
			BitBlt(Maindc,Window9RectPos.left,Window9RectPos.top,Window9RectPos.right,Window9RectPos.bottom,Memdc,0,0,SRCCOPY)
		Else
			TransparentBlt(Maindc,Window9RectPos.left,Window9RectPos.top,Window9RectPos.right,Window9RectPos.bottom,Memdc,0,0,Window9RectPos.right,Window9RectPos.bottom,H_Alpha_VALUE)
		EndIf
		Function = ReleaseDC(wind___ , MainDC)
		wind___=0
		DeleteObject(hbitmapMaindraw)
	EndIf
	DeleteDC(MemDC)
End Function

Function LineDraw(ByVal x  As Long, ByVal y  As Long, _
				ByVal x1 As Long, ByVal y1 As Long, _
				ByVal width_ As Long, _
				ByVal color_ As Long, _
				ByVal style  As Long) As Integer Export
	Dim pen As integer
	Dim point_ As point_
	If color_=-1 Then
		pen=Cast(Integer,GetStockObject(NULL_PEN))
	Else
		pen=Cast(Integer,CreatePen(Style,width_,color_))
	EndIf
	SelectObject(Cast(HDC,MemDC),Cast(hgdiobj,pen))
	MoveToEx(MemDC, x,y,Cast(LPPOINT,@point_))
	Function= LineTo(MemDC,x1,y1)
	DeleteObject(Cast(HGDIOBJ,pen))
End Function

Sub PixDraw(ByVal x As Long,ByVal y As Long,ByVal Color_ As Long) Export
	SetPixel(MemDC,x,y,color_)
End Sub

Function GetPix(ByVal x As Long,ByVal y As Long) As Long export
	Return GetPixel(MemDC,x,y)
End Function

Function BoxDraw(ByVal x As Long, ByVal y As Long, _
				ByVal w As Long, ByVal h As Long, _
				ByVal ColorPen   As Long, _
				ByVal ColorBk    As Long, _
				ByVal widthPen   As Long, _
				ByVal StylePen   As Long, _
				ByVal AlPHAPARAM As Long) As Integer Export
	Dim As HBRUSH brush
	Dim As Integer pen
	If ColorBk<>-1 Then
		brush=CreateSolidBrush(Cast(COLORREF,ColorBk))
		pen=Cast(Integer,CreatePen(stylepen,widthPen,Cast(COLORREF,colorpen)))
	Else
		brush=GetStockObject(NULL_BRUSH )
		pen=Cast(Integer,CreatePen(stylepen,widthPen,Cast(COLORREF,colorpen)))
	EndIf
	If AlPHAPARAM=255 Then
		SelectObject(MemDC,brush)
		SelectObject(MemDC,Cast(HGDIOBJ,pen))
		Function =  Rectangle(MemDC,x,y,x+w,y+h)
		DeleteObject(Cast(HGDIOBJ,brush))
		DeleteObject(Cast(HGDIOBJ,pen))
	Else
		Dim As HDC hhdd=Cast(HDC,CreateCompatibleDC(0))
		Dim hnh As hBitmap=CopyImage(hbitmapMaindraw,IMAGE_BITMAP,0,0,LR_COPYRETURNORG)
		SelectObject(Cast(HDC,hhdd),Cast(hbitmap,hnh))
		SelectObject(hhdd,brush)
		SelectObject(hhdd,Cast(HGDIOBJ,pen))
		Function =  Rectangle(hhdd,x,y,x+w,y+h)
		DeleteObject(Cast(HGDIOBJ,brush))
		DeleteObject(Cast(HGDIOBJ,pen))
		Dim as BLENDFUNCTION blendFunction
		blendFunction.BlendOp = AC_SRC_OVER
		blendFunction.SourceConstantAlpha = cast(byte,AlPHAPARAM )
		AlphaBlend(MemDC,  x,y,w,h, hhdd, x,y,w,h, blendFunction)
		DeleteDC(hhdd)
		DeleteObject(Cast(HGDIOBJ,hnh))
	endif
End Function

Function RoundBoxDraw(ByVal x As Long, ByVal y As Long, _
					ByVal w As Long, ByVal h As Long, _
					ByVal ColorPen      As Long, _
					ByVal ColorBk       As Long, _
					ByVal widthPen      As Long, _
					ByVal StylePen      As Long, _
					ByVal ellipsewidth  As Long, _
					ByVal ellipseheight As Long, _
					ByVal AlPHAPARAM    As Long) As Integer Export
	Dim As HBRUSH brush
	Dim As Integer pen
	If ColorBk<>-1 Then
		brush=CreateSolidBrush(Cast(COLORREF,ColorBk))
		pen=Cast(Integer,CreatePen(stylepen,widthPen,colorpen))
	Else
		brush=GetStockObject(NULL_BRUSH )
		pen=Cast(Integer,CreatePen(stylepen,widthPen,colorpen))
	EndIf
	If AlPHAPARAM=255 Then
		SelectObject(MemDC,brush)
		SelectObject(MemDC,Cast(HGDIOBJ,pen))
		Function =  RoundRect(MemDC,x,y,x+w,y+h,ellipsewidth,ellipseheight)
		DeleteObject(Cast(HGDIOBJ,brush))
		DeleteObject(Cast(HGDIOBJ,pen))
	Else
		Dim As HDC hhdd=Cast(HDC,CreateCompatibleDC(0))
		Dim hnh As hBitmap=CopyImage(hbitmapMaindraw,IMAGE_BITMAP,0,0,LR_COPYRETURNORG)
		SelectObject(Cast(HDC,hhdd),Cast(hbitmap,hnh))
		SelectObject(hhdd,brush)
		SelectObject(hhdd,Cast(HGDIOBJ,pen))
		Function =  RoundRect(hhdd,x,y,x+w,y+h,ellipsewidth,ellipseheight)
		DeleteObject(Cast(HGDIOBJ,brush))
		DeleteObject(Cast(HGDIOBJ,pen))
		Dim as BLENDFUNCTION blendFunction
		blendFunction.BlendOp = AC_SRC_OVER
		blendFunction.SourceConstantAlpha = cast(byte,AlPHAPARAM )
		AlphaBlend(MemDC, x,y,w,h, hhdd, x,y,w,h,  blendFunction)
		DeleteDC(hhdd)
		DeleteObject(Cast(HGDIOBJ,hnh))
	EndIf
End Function

Function RoundDraw(ByVal x As Long, ByVal y As Long, _
					ByVal w As Long, ByVal h As Long, _
					ByVal ColorPen   As Long, _
					ByVal ColorBk    As Long, _
					ByVal widthPen   As Long, _
					ByVal StylePen   As Long, _
					ByVal AlPHAPARAM As Long) As Integer export
	Dim As Integer pen
	Dim As HBRUSH brush
	If ColorBk<>-1 Then
		brush=CreateSolidBrush(ColorBk)
		pen=Cast(Integer,CreatePen(stylepen,widthPen,colorpen))
	Else
		brush=GetStockObject(NULL_BRUSH )
		pen=Cast(Integer,CreatePen(stylepen,widthPen,colorpen))
	EndIf
	If AlPHAPARAM=255 Then
		SelectObject(MemDC,brush)
		SelectObject(MemDC,Cast(HGDIOBJ,pen))
		Function = Ellipse(MemDC,x,y,x+w,y+h)
		DeleteObject(Cast(HGDIOBJ,brush))
		DeleteObject(Cast(HGDIOBJ,pen))
	Else
		Dim As HDC hhdd=Cast(HDC,CreateCompatibleDC(0))
		Dim hnh As hBitmap=CopyImage(hbitmapMaindraw,IMAGE_BITMAP,0,0,LR_COPYRETURNORG)
		SelectObject(Cast(HDC,hhdd),Cast(hbitmap,hnh))
		SelectObject(hhdd,brush)
		SelectObject(hhdd,Cast(HGDIOBJ,pen))
		Function = Ellipse(hhdd,x,y,x+w,y+h)
		DeleteObject(Cast(HGDIOBJ,brush))
		DeleteObject(Cast(HGDIOBJ,pen))
		Dim as BLENDFUNCTION blendFunction
		blendFunction.BlendOp = AC_SRC_OVER
		blendFunction.SourceConstantAlpha = cast(byte,AlPHAPARAM )
		AlphaBlend(MemDC,x,y,w,h, hhdd, x,y,w,h, blendFunction)
		DeleteDC(hhdd)
		DeleteObject(Cast(HGDIOBJ,hnh))
	EndIf
End Function

Function CircleDraw(ByVal x As Long,ByVal y As Long, _
					ByVal radius As Long, _
					ByVal ColorPen As Long, _
					ByVal ColorBk  As Long, _
					ByVal widthPen As Long, _
					ByVal StylePen As Long, _
					ByVal AlPHAPARAM As Long) As Integer export
	Dim As Integer pen
	Dim As HBRUSH brush
	If ColorBk<>-1 Then
		brush=CreateSolidBrush(ColorBk)
		pen=Cast(Integer,CreatePen(stylepen,widthPen,colorpen))
	Else
		brush=GetStockObject(NULL_BRUSH )
		pen=Cast(Integer,CreatePen(stylepen,widthPen,colorpen))
	EndIf
	If AlPHAPARAM=255 Then
		SelectObject(MemDC,brush)
		SelectObject(MemDC,Cast(HGDIOBJ,pen))
		Function = Ellipse(MemDC,x-radius,y-radius,x+radius,y+radius)
		DeleteObject(Cast(HGDIOBJ,brush))
		DeleteObject(Cast(HGDIOBJ,pen))
	Else
		Dim As HDC hhdd=Cast(HDC,CreateCompatibleDC(0))
		Dim hnh As hBitmap=CopyImage(hbitmapMaindraw,IMAGE_BITMAP,0,0,LR_COPYRETURNORG)
		SelectObject(Cast(HDC,hhdd),Cast(hbitmap,hnh))
		SelectObject(hhdd,brush)
		SelectObject(hhdd,Cast(HGDIOBJ,pen))
		Function = Ellipse(hhdd,x-radius,y-radius,x+radius,y+radius)
		DeleteObject(Cast(HGDIOBJ,brush))
		DeleteObject(Cast(HGDIOBJ,pen))
		Dim as BLENDFUNCTION blendFunction
		blendFunction.BlendOp = AC_SRC_OVER
		blendFunction.SourceConstantAlpha = cast(byte,AlPHAPARAM )
		Var ll=radius*2
		AlphaBlend(MemDC, _
		x-radius, _
		y-radius, _
		ll, _
		ll, _
		hhdd, _
		x-radius, _
		y-radius, _
		ll, _
		ll, _
		blendFunction)
		DeleteDC(hhdd)
		DeleteObject(Cast(HGDIOBJ,hnh))
	EndIf
End Function

Private Sub RezerveFont(byval fontdraw_ As integer)
	RezerveFont_=Cast(Any Ptr,FontDraw_)
End sub

Sub FontDraw(ByVal FontID As integer) Export
	RezerveFont(FontID)
	SelectObject(MemDC,Cast(HGDIOBJ,FontID))
End Sub

Function TextDraw(ByVal x As Long, ByVal y As Long, _
					ByRef string_    As USTRING, _
					ByVal ColorBK    As Long, _
					ByVal ColorText  As Long, _
					ByVal AlPHAPARAM As Long) As Integer export
	If AlPHAPARAM=255 Then
		If ColorBk=-1 Then
			SetBkMode(MemDC,1)
		Else
			SetBkColor(MemDC,ColorBk)
		EndIf
		SetTextColor(MemDC,ColorText)
		#ifdef UNICODE
			Function = TextOut(MemDC,x,y,*string_, Len(string_))
		#else	
			Function = TextOut(MemDC,x,y,string_, Len(string_))
		#EndIf		
	Else
		Dim As HDC hhdd=Cast(HDC,CreateCompatibleDC(0))
		Dim hnh As hBitmap=CopyImage(hbitmapMaindraw,IMAGE_BITMAP,0,0,LR_COPYRETURNORG)
		SelectObject(Cast(HDC,hhdd),Cast(hbitmap,hnh))
		If RezerveFont_<>0 Then
			SelectObject(hhdd,Cast(HGDIOBJ,RezerveFont_))
		EndIf
		If ColorBk=-1 Then
			SetBkMode(hhdd,1)
		Else
			SetBkColor(hhdd,ColorBk)
		EndIf
		SetTextColor(hhdd,ColorText)
		Var leenn=Len(string_)
		#ifdef UNICODE
			Function = TextOut(hhdd,x,y,*string_, leenn)
		#else	
			Function = TextOut(hhdd,x,y,string_, leenn)
		#EndIf		
		Dim As SIZE size
		#ifdef UNICODE
			GetTextExtentPoint32(hhdd,*string_ , leenn, @SIZE)
		#else	
			GetTextExtentPoint32(hhdd,string_ , leenn, @SIZE)
		#EndIf	
		Dim as BLENDFUNCTION blendFunction
		blendFunction.BlendOp = AC_SRC_OVER
		blendFunction.SourceConstantAlpha = cast(byte,AlPHAPARAM )
		AlphaBlend(MemDC, _
		x, _
		y, _
		size.cx, _
		size.cy, _
		hhdd, _
		x, _
		y, _
		size.cx, _
		size.cy, _
		blendFunction)
		DeleteDC(hhdd)
		DeleteObject(Cast(HGDIOBJ,hnh))
	EndIf
End Function

Function PolylineDraw(ByVal pPoint   As POINT Ptr, _
					ByVal nCount   As Long, _
					ByVal ColorPen As Long, _
					ByVal widthPen As Long, _
					ByVal StylePen As Long) As Integer export
	Dim As Integer pen
	pen=Cast(Integer,CreatePen(stylepen,widthPen,colorpen))
	SelectObject(MemDC,Cast(HGDIOBJ,pen))
	Function = Polyline (MemDC, pPoint, nCount)
	DeleteObject(Cast(HGDIOBJ,pen))
End Function

Function PolygonDraw(ByVal pPoint      As POINT ptr, _
					ByVal nCount      As Long, _
					byval FillColor   as Long, _
					ByVal BorderColor As Long, _
					ByVal BorderWidth As Long, _
					ByVal BorderStyle As Long) As Integer export
	Dim As HGDIOBJ oldPen   = SelectObject(MemDC,CreatePen(BorderStyle,BorderWidth,BorderColor))
	Dim As HGDIOBJ oldBrush = SelectObject(MemDC,CreateSolidBrush(FillColor))
	Function = Polygon(MemDC, pPoint, nCount)
	DeleteObject(SelectObject(MemDC,oldBrush))
	DeleteObject(SelectObject(MemDC,oldPen))
End Function

Sub ImageDraw(ByVal bitmap_ As HBITMAP,ByVal x As Long, ByVal y As Long, ByVal AlPHAPARAM As Long)  Export
	Dim As HDC hdc=CreateCompatibleDC(Maindc)
	Dim size As BITMAP
	getobject(Cast(HBITMAP,bitmap_),sizeof(BITMAP),@size)
	
	If AlPHAPARAM=255 Then
		Var oldObj = SelectObject(hdc,bitmap_)
		BitBlt(MemDC, x, y,size.bmWidth , size.bmHeight,hdc, 0, 0, SRCCOPY)
		SelectObject(hdc,oldObj)
	Else
		Dim As HDC hdc2=CreateCompatibleDC(Maindc)
		Dim hbitmap As hBitmap=CopyImage(hbitmapMaindraw,IMAGE_BITMAP,0,0,LR_COPYRETURNORG)
		SelectObject(hdc,hbitmap)
		Var oldObj = SelectObject(hdc2,bitmap_)
		BitBlt(hdc, x, y,size.bmWidth , size.bmHeight,hdc2, 0, 0, SRCCOPY)
		Dim as BLENDFUNCTION blendFunction
		blendFunction.BlendOp = AC_SRC_OVER
		blendFunction.SourceConstantAlpha = cast(byte,AlPHAPARAM )
		AlphaBlend(MemDC, _
		x, _
		y, _
		size.bmWidth, _
		size.bmHeight, _
		hdc, _
		x, _
		y, _
		size.bmWidth, _
		size.bmHeight, _
		blendFunction)
		SelectObject(hdc,oldObj)
		DeleteObject(Cast(HGDIOBJ,hbitmap))
		DeleteDC(hdc2)
	EndIf
	DeleteDC(hdc)
End Sub

sub FillRectDraw(ByVal x As Long, ByVal y As Long, ByVal Color_ As Long) Export
	Var brush=CreateSolidBrush(Color_ )
	SelectObject(MemDC,brush)
	ExtFloodFill(MemDc, x, y,GetPixel(MemDC,x,y), FLOODFILLSURFACE)
	DeleteObject(brush)
End Sub

sub FocusDraw(ByVal x As Long, ByVal y As Long, ByVal w As Long, ByVal h As Long , ByVal  iColor as Long = 0) Export
	Dim As RECT r
	r.left  =x
	r.top   =y
	r.right =x+w
	r.bottom=y+h
	DrawFocusRect(MemDC,@r)
End Sub

Sub IconDraw(ByVal x As Long, ByVal y As Long, ByVal Hicon As HICON) Export
	DrawIcon(MemDC,x,y,Hicon)
End Sub

Function GradientFillDraw(ByVal x As Long, ByVal y As Long, _
						ByVal w As Long, ByVal h As Long, _
						ByVal Rbegin As Long, ByVal Gbegin As Long, ByVal Bbegin As Long, _
						ByVal REnd   As Long, ByVal GEnd   As Long, ByVal BEnd   As Long, _
						ByVal GOR_VERT As bool)As Integer Export
	Dim  As TRIVERTEX vert(0 to 1)
	Dim As GRADIENT_RECT gRect
	vert(0).x      = x
	vert(0).y      = y
	vert(0).Red    = Rbegin
	vert(0).Green  = Gbegin
	vert(0).Blue   = Bbegin
	vert(0).Alpha  = 0
	vert(1).x      = w+x
	vert(1).y      = h+y
	vert(1).Red    = Rend
	vert(1).Green  = Gend
	vert(1).Blue   = Bend
	vert(1).Alpha  = 0
	gRect.UpperLeft  = 1
	gRect.LowerRight = 0
	Return GradientFill( MemDC, @vert(0), 2, @gRect, 1, GOR_VERT )
End Function

Function PieDraw(ByVal x As Long, ByVal y As Long,_
				ByVal w As Long, ByVal h As Long,_
				ByVal x1 As Long, ByVal y1 As Long,_
				ByVal x2 As Long, ByVal y2 As Long,_
				ByVal ColorPen As Long, ByVal ColorBk As Long,_
				ByVal widthPen As Long, ByVal StylePen As Long) As Integer export
	Dim As Integer pen
	Dim As HBRUSH brush
	If ColorBk<>-1 Then
		brush=CreateSolidBrush(ColorBk)
		pen=Cast(Integer,CreatePen(stylepen,widthPen,colorpen))
	Else
		brush=GetStockObject(NULL_BRUSH )
		pen=Cast(Integer,CreatePen(stylepen,widthPen,colorpen))
	EndIf
	SelectObject(MemDC,brush)
	SelectObject(MemDC,Cast(HGDIOBJ,pen))
	Function = Pie(MemDC,x,y,x+w,y+h,x1,y1,x2,y2)
	DeleteObject(Cast(HGDIOBJ,brush))
	DeleteObject(Cast(HGDIOBJ,pen))
End Function
