#Include once "window9.bi"

Dim Shared As HBRUSH hBrush
Dim Shared As Long COLBK,COLTE
Extern As Long ERROR_COLOR

#Ifndef FBIDSCROLLBAR
#Define FBIDSCROLLBAR 5
#EndIf

#Ifndef FBIDCOMBOLBOX
#Define FBIDCOMBOLBOX 13
#EndIf
private Function wndproc(hwnd As HWND, msg As UInteger,_
	wparam As WPARAM, lparam As LPARAM) As Integer
	Select Case Msg
		Case WM_CTLCOLORSTATIC,WM_CTLCOLOREDIT,WM_CTLCOLORLISTBOX,WM_CTLCOLORSCROLLBAR
		Var GETGadgetClassCOMBO_and_SCROLLBAR=GadgetClass(Cast(HWND,lparam))
		If GETGadgetClassCOMBO_and_SCROLLBAR=FBIDCOMBOLBOX Then
			SetBkColor(Cast(HDC,wparam),COLBK)
			SetTextColor(Cast(HDC,wParam),COLTE)
			Return Cast(Long,hBrush)
		Else
			Static As  COLORREF ccooll
			Static As hwnd hwwn
			Static As Integer umsggg
			Dim As Integer hh
			If GETGadgetClassCOMBO_and_SCROLLBAR=FBIDSCROLLBAR And GetGadgetColor(lparam,4)=0 Then
				hh = &hf0f0f0
			Else
				hh=GetGadgetColor(lparam,3)
			EndIf
			If ERROR_COLOR=0 Then
				If hh<>ccooll Or hwwn<>hWnd Or umsggg<>msg Then
					ccooll=hh
					hwwn=hWnd
					umsggg=msg
					DeleteObject(hBrush)
					hBrush= CreateSolidBrush(ccooll)
					SetBkColor(Cast(HDC,wparam),ccooll)
				Else
					SetBkColor(Cast(HDC,wparam),ccooll)
				EndIf
				SetTextColor(Cast(HDC,wParam),Cast(COLORREF,GetGadgetColor(lparam,4)))
			Else
				ERROR_COLOR=0
			EndIf
		EndIf

		Return Cast(Long,hBrush)
		Case WM_SIZE,WM_ACTIVATE,WM_SETFOCUS,WM_KILLFOCUS,WM_CLOSE,WM_NOTIFY, _
			WM_KEYDOWN,WM_KEYUP,WM_SYSKEYDOWN,WM_SYSKEYUP, _
			WM_COMMAND,WM_SYSCOMMAND,WM_TIMER,WM_HSCROLL,WM_VSCROLL, _
			WM_MENUSELECT,WM_DROPFILES, _
			WM_MOUSEMOVE, _
			WM_LBUTTONUP,WM_LBUTTONDBLCLK, _
			WM_RBUTTONUP,WM_RBUTTONDBLCLK, _
			WM_MBUTTONUP,WM_MBUTTONDBLCLK, _
			WM_MOUSEWHEEL, _
			WM_HOTKEY,WM_USER,WM_PAINT,WM_VKEYTOITEM
			SendMessage(GetParent(Hwnd),msg,wparam,lparam)
		Case WM_LBUTTONDOWN,WM_RBUTTONDOWN,WM_MBUTTONDOWN
			SetProp(GetParent(Hwnd),"eventmousebutton",cast(handle,1))
			SendMessage(GetParent(Hwnd),msg,wparam,lparam)
	End Select
	Return DefWindowProc(hwnd,msg,wparam,lparam)
End Function

Dim Shared NameClass As String
Function RegClassContainer() As BOOL
	Dim As WNDCLASSEX wc
	Static n As Integer
	NameClass="ContainerGadget" & n
	n+=1
	With wc
		.cbSize=SizeOf(WNDCLASSEX)
		.style=CS_HREDRAW Or CS_VREDRAW
		.lpfnWndProc=@wndproc
		.hInstance=GetModuleHandle(0)
		.hbrBackground=Cast(HBRUSH,COLOR_WINDOW)
		.lpszClassName=StrPtr(NameClass)
		.hCursor = LoadCursor(0,IDC_ARROW)
	End With
	If RegisterClassEx(@wc)=0 Then
		Print "Register error, press any key"
		Return 0
	EndIf
	Return 1
End Function

Function ContainerGadget(ByVal gadget As Integer, ByVal x As Integer,ByVal y As Integer,ByVal Width_ As Integer,ByVal height_ As Integer,ByVal par As Integer=0) As HWND Export
	RegClassContainer()
	Dim gID As HWND=CreateWindowEx(par,NameClass,"",_
	WS_VISIBLE Or WS_CHILD,x,y,width_,height_,Cast(HWND,GETGURRENTAL()), Cast(HMENU,gadget),0,0)
	ADDIN9999(gadget,gID)
	ColorAdd(gID,&hf0f0f0,0)
	UseGadgetList(gID)
	Return gID
End Function
