#Include Once "window9.bi"

#Define fbguiEventsCountMax 20

#Define FBIDWINDOW 1
#Define FBIDLISTBOX 2
#Define FBIDCOMBOBOX 3
#Define FBIDTABBERBOX 4
#Define FBIDSCROLLBAR 5
#Define FBIDLISTVIEW 6
#Define FBIDTRACKBAR 7
#Define FBIDPROGRESSBAR 8
#Define SYSDATETIMEPICK32 9
#Define SYSMONTHCAL32 10

#Define FBIDCOMBOLBOX 13
#Define MSCTLS_UPDOWN32 14
#Define FBTOOLBAR 15
#Define STATICCONTROL 16

#Define fbguiToolWindow 1
#Define fbguiMDIWindow 2

Type SYSTEMDATEANDTIME777_
	As Short Year_
	As Short Month_
	As Short Weekday_
	As Short Day_
	As Short Hour_
	As Short minute_
	As Short second_
	As Short msec_
End Type
Dim Shared fgh As SYSTEMDATEANDTIME777_
Declare Function fbguiWinProc(ByVal hWnd As HWND, ByVal uMsg As UINT, ByVal wParam As WPARAM, ByVal lParam As LPARAM ) As Long

Type Message
	As Long Handle
	As Long Message
	As Long wParam
	As Long lParam
End Type

Dim Shared As HBRUSH hBrush
Dim Shared As MSG    fbguiMSG
Dim Shared As Long   eventTreeViewitem_
Dim Shared As Long EventNumberTREEView_
Dim Shared As Long Address_Function___8
Dim Shared As Long Address_Function___9
Dim Shared As Long EventNumberListView_
Dim Shared As Long List_Column_FLAG__
Dim Shared As Long columnevent_
Dim Shared As Long flagKeyListView_
Dim Shared As Long eventitem_
Dim Shared As Long eventsubitem_
Dim Shared As Long EventNumberToolBar_
Extern As Long ERROR_COLOR
Dim Shared As Long ERROR_COLOR,COLBK,COLTE
Dim Shared As Long SizeY_,SizeX_
Dim Shared As Long EventX, EventY
Dim Shared As Long EventNumber_
Dim Shared As Long EventKEY_
Dim Shared As Long EventActiveGadget
Dim Shared As Long EventActiveIDGadget
Dim Shared As Long EventID, fbguiCountEvents
Dim Shared As Integer EVENT_W_PARAM,EVENT_L_PARAM,CheckAccelerator,UBound_Accel,PageLength999=10
Dim Shared As HWND hMDIClient, EventHwnd_
Dim Shared As HDC  EventHDC
'Dim Shared As Byte Ptr fbguiUserData
Dim Shared As Message  fbguiEventsBuffer(fbguiEventsCountMax)
Dim Shared OnWindowProcCallBack    As Function (ByVal As HWND,ByVal As UINT,ByVal As WPARAM,ByVal As LPARAM) As Long Ptr = 0
Dim Shared OnWindowProcCallBack_2  As Function (ByVal As HWND,ByVal As UINT,ByVal As WPARAM,ByVal As LPARAM,ByVal As Integer,ByVal As Point) As Long Ptr = 0

ReDim Shared As ACCELERATOR ACCEL_HWND_(0)

columnevent_=-1

Function GadgetClass(ByVal gadget As HWND) As Long Export
	Dim result As Long
	Select Case GetClassName_(gadget)
		Case "ComboBox"
			result=FBIDCOMBOBOX
		Case "ComboLBox"
			result=FBIDCOMBOLBOX
		Case "ListBox"
			result=FBIDLISTBOX
		Case "SysTabControl32"
			result=FBIDTABBERBOX
		Case "msctls_trackbar32"
			result=FBIDTRACKBAR
		Case "ScrollBar"
			result=FBIDSCROLLBAR
		Case "msctls_progress32"
			result=FBIDPROGRESSBAR
		Case "SysDateTimePick32"
			result=SYSDATETIMEPICK32
		Case "SysMonthCal32"
			result=SYSMONTHCAL32
		Case "msctls_updown32"
			result=MSCTLS_UPDOWN32
		Case "ToolbarWindow32"
			result=FBTOOLBAR
		Case "Static"
			result=STATICCONTROL
		Case "SysListView32"
			result=FBIDListView
		Case Else
			result = 0
	End Select
	Return result
End Function

Sub GetAcceleratorInfo(ByVal aa As ACCELERATOR Ptr)
	CheckAccelerator=1
	Dim As ACCELERATOR Ptr ACCELERATORSTRUCT=aa
	Dim As Integer c=UBound(ACCEL_HWND_)
	For a As Integer=0 To c
		If ACCEL_HWND_(a).hwnd=ACCELERATORSTRUCT->hwnd Then
			ACCEL_HWND_(a).hAccel=ACCELERATORSTRUCT->hAccel
			Exit Sub
		EndIf
	Next
	If ACCEL_HWND_(0).hwnd=0 Then
		c=0
	Else
		c+=1
		ReDim Preserve As ACCELERATOR ACCEL_HWND_(c)
	EndIf
	ACCEL_HWND_(c).hwnd=ACCELERATORSTRUCT->hwnd
	ACCEL_HWND_(c).hAccel=ACCELERATORSTRUCT->hAccel
	UBound_Accel=c
End Sub

Function GetfbguiWinProc() As Integer
	Return Cast(Integer,@fbguiWinProc)
End Function

Function GetfbguiMSG() As Integer
	Return Cast(Integer,@fbguiMSG)
End Function

Sub MDISUB(ByVal MDI As HWND)
	hMDIClient=MDI
End Sub

Sub SetPageStepScrollBar(ByVal parscroll As Integer)Export
	PageLength999=parscroll
End Sub

Function COOLOOR(ByVal bk As Integer, ByVal te As Integer) As Integer Export
	COLBK=bk
	COLTE=te
	Return 1
End Function

Function ERR_(ByVal bk As Integer) As Integer Export
	ERROR_COLOR=bk
	Return 1
End Function

Function EventNumber() As Integer Export
	Return EventNumber_
End Function

Function EventNumberToolBar() As Integer Export
	Return EventNumberToolBar_
End Function

Function EventNumberListView() As Integer Export
	Function= EventNumberListView_
	EventNumberListView_=0
End Function

Function MouseX() As Integer Export
	Return EventX
End Function

Function MouseY() As Integer Export
	Return EventY
End Function

Function SizeX() As Integer Export
	Return SizeX_
End Function

Function SizeY() As Integer Export
	Return SizeY_
End Function

Function EventHwnd() As HWND Export
	Return EventHwnd_
End Function

Function EventKEY() As Integer Export
	Return EventKEY_
End Function

Function EventWParam() As WPARAM Export
	Return Cast(WPARAM,EVENT_W_PARAM)
End Function

Function EventLParam() As LPARAM Export
	Return Cast(LPARAM,EVENT_L_PARAM)
End Function

Function GetSubItemListView() As Integer Export
	Return eventsubitem_
End Function

Function GetItemListView() As Integer Export
	Return eventitem_
End Function

Function GetColumnListView() As Integer Export
	Function= columnevent_
	columnevent_=-1
End Function

Function FlagKeyListView() As Integer Export
	Return flagKeyListView_
End Function

Function GetItemTreeView() As Integer Export
	Return eventTreeViewitem_
End Function

Function EventNumberTreeView() As Integer Export
	Function= EventNumberTreeView_
	EventNumberTreeView_=0
End Function

Function SetWindowCallback(ByVal Address_Function As Integer,ByVal flag As Integer) As Integer Export
	If flag=0 Then
		Address_Function___9=100
		OnWindowProcCallBack=Cast(Function (ByVal As HWND,ByVal As UINT,ByVal As WPARAM,ByVal As LPARAM) As Long Ptr,Address_Function)
	ElseIf flag=1 Then
		Address_Function___8=100
		OnWindowProcCallBack_2=Cast(Function (ByVal As HWND,ByVal As UINT,ByVal As WPARAM,ByVal As LPARAM,ByVal As Integer,ByVal As Point) As Long Ptr,Address_Function)
	EndIf
	Return 1
End Function
Function FreeCallback(ByVal flag As Integer) As Integer Export
	Select Case flag
		Case 0
			Address_Function___9=0
		Case 1
			Address_Function___8=0
	End Select
	Return 1
End Function

Function fbguiWinProc(ByVal hWnd As HWND, ByVal uMsg As UINT, ByVal wParam As WPARAM, ByVal lParam As LPARAM ) As Long Export
	If Address_Function___9<>0 Then
		Var ONWINPROCCALLBACK= OnWindowProcCallBack(hwnd,umsg,wParam,lParam)
		If  ONWINPROCCALLBACK<>0 Then
			Return Cast(Long,ONWINPROCCALLBACK)
		End If
	End If

	If uMsg=WM_CTLCOLORSTATIC Or Umsg=WM_CTLCOLOREDIT Or Umsg=WM_CTLCOLORLISTBOX Or Umsg=WM_CTLCOLORSCROLLBAR Then
		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<>Umsg Then
					ccooll=hh
					hwwn=hWnd
					umsggg=Umsg
					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)
	End If

	'  fbguiUserData=Cast(Byte Ptr,GetWindowLong(hWnd,GWL_USERDATA))
	If fbguiCountEvents < fbguiEventsCountMax Then
		Select Case uMsg
			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_LBUTTONDOWN,WM_LBUTTONUP,WM_LBUTTONDBLCLK, _
				WM_RBUTTONDOWN,WM_RBUTTONUP,WM_RBUTTONDBLCLK, _
				WM_MBUTTONDOWN,WM_MBUTTONUP,WM_MBUTTONDBLCLK, _
				WM_MOUSEWHEEL, _
				WM_HOTKEY,WM_USER,WM_PARENTNOTIFY,WM_CTLCOLORLISTBOX,WM_PAINT,WM_CTLCOLORSTATIC,WM_VKEYTOITEM
				fbguiCountEvents += 1
				If uMsg = 16 Then fbguiCountEvents = 1 ' WM_CLOSE=16
				fbguiEventsBuffer(fbguiCountEvents).Handle  = Cast(Long,hWnd)
				fbguiEventsBuffer(fbguiCountEvents).Message = uMsg
				fbguiEventsBuffer(fbguiCountEvents).wParam  = wParam
				fbguiEventsBuffer(fbguiCountEvents).lParam  = lParam
		End Select
	End If

	Dim sinf As SCROLLINFO
	Dim nPos As Integer
	Select Case uMsg
		Case 36'WM_GETMINMAXINFO
			If GetProp(hwnd,"MINMAXFLAG") = 1 Then
				Dim As Point ptMin = Type(_
				Cast(Integer,GetProp(hwnd,"MINIMALWIDTH")),_
				Cast(Integer,GetProp(hwnd,"MINIMALHEIGHT")))
				Dim As Point ptMax = Type(_
				Cast(Integer,GetProp(hwnd,"MAXIMALWIDTH")),_
				Cast(Integer,GetProp(hwnd,"MAXIMALHEIGHT")))
				Dim MINMAX As MINMAXINFO Ptr = Cast(MINMAXINFO Ptr,lparam)
				MINMAX->ptMinTrackSize = ptMin
				MINMAX->ptMaxTrackSize = ptMax
				Return 0
			EndIf
		Case 1029 'WM_SHELLNOTIFY
			SetForegroundWindow(hwnd)
			SendMessage(hwnd,lparam,wparam,-1)
		Case 276 'WM_HSCROLL
			EventHwnd_ =Cast(hwnd,lParam)
			sinf.cbSize=SizeOf(sinf)
			sinf.fMask=SIF_RANGE Or SIF_PAGE
			If GetScrollInfo(EventHwnd_,SB_HORZ,@sinf)=0 Then
				GetScrollInfo(Hwnd,SB_HORZ,@sinf)
			EndIf
			Select Case LoWord(wParam)
				Case SB_THUMBPOSITION,SB_THUMBTRACK
					nPos=HiWord(wParam)
					If SetScrollPos(EventHwnd_,SB_CTL,nPos,TRUE)=0 Then
						SetScrollPos(Hwnd,SB_HORZ,nPos,TRUE)
					EndIf
				Case SB_LINEDOWN
					nPos=GetScrollPos(EventHwnd_,SB_CTL)
					npos+=1
					If SetScrollPos(EventHwnd_,SB_CTL,nPos,TRUE)=0 Then
						nPos=GetScrollPos(Hwnd,SB_HORZ)
						npos+=1
						SetScrollPos(Hwnd,SB_HORZ,nPos,TRUE)
					EndIf
				Case SB_LINEUP
					nPos=GetScrollPos(EventHwnd_,SB_CTL)
					npos-=1
					If SetScrollPos(EventHwnd_,SB_CTL,nPos,TRUE)=0 Then
						nPos=GetScrollPos(Hwnd,SB_HORZ)
						npos-=1
						SetScrollPos(Hwnd,SB_HORZ,nPos,TRUE)
					EndIf
				Case SB_PAGEDOWN
					nPos=GetScrollPos(EventHwnd_,SB_CTL)
					npos+=PageLength999
					If SetScrollPos(EventHwnd_,SB_CTL,nPos,TRUE)=0 Then
						nPos=GetScrollPos(Hwnd,SB_HORZ)
						npos+=PageLength999
						SetScrollPos(Hwnd,SB_HORZ,nPos,TRUE)
					EndIf
				Case SB_PAGEUP
					nPos=GetScrollPos(EventHwnd_,SB_CTL)
					npos-=PageLength999
					If SetScrollPos(EventHwnd_,SB_CTL,nPos,TRUE)=0 Then
						nPos=GetScrollPos(Hwnd,SB_HORZ)
						npos-=PageLength999
						SetScrollPos(Hwnd,SB_HORZ,nPos,TRUE)
					EndIf
			End Select

		Case 277 'WM_VSCROLL
			EventHwnd_ =Cast(hwnd,lParam)
			sinf.cbSize=SizeOf(sinf)
			sinf.fMask=SIF_RANGE Or SIF_PAGE
			If GetScrollInfo(EventHwnd_,SB_VERT,@sinf)=0 Then
				GetScrollInfo(Hwnd,SB_VERT,@sinf)
			EndIf
			Select Case LoWord(wParam)
				Case SB_THUMBPOSITION,SB_THUMBTRACK
					nPos=HiWord(wParam)
					If SetScrollPos(EventHwnd_,SB_CTL,nPos,TRUE)=0 Then
						SetScrollPos(Hwnd,SB_VERT,nPos,TRUE)
					EndIf
				Case SB_LINEDOWN
					nPos=GetScrollPos(EventHwnd_,SB_CTL)
					npos+=1
					If SetScrollPos(EventHwnd_,SB_CTL,nPos,TRUE)=0 Then
						nPos=GetScrollPos(Hwnd,SB_VERT)
						npos+=1
						SetScrollPos(Hwnd,SB_VERT,nPos,TRUE)
					EndIf
				Case SB_LINEUP
					nPos=GetScrollPos(EventHwnd_,SB_CTL)
					npos-=1
					If SetScrollPos(EventHwnd_,SB_CTL,nPos,TRUE)=0 Then
						nPos=GetScrollPos(Hwnd,SB_VERT)
						npos-=1
						SetScrollPos(Hwnd,SB_VERT,nPos,TRUE)
					EndIf
				Case SB_PAGEDOWN
					nPos=GetScrollPos(EventHwnd_,SB_CTL)
					npos+=PageLength999
					If SetScrollPos(EventHwnd_,SB_CTL,nPos,TRUE)=0 Then
						nPos=GetScrollPos(Hwnd,SB_VERT)
						npos+=PageLength999
						SetScrollPos(Hwnd,SB_VERT,nPos,TRUE)
					EndIf
				Case SB_PAGEUP
					nPos=GetScrollPos(EventHwnd_,SB_CTL)
					npos-=PageLength999
					If SetScrollPos(EventHwnd_,SB_CTL,nPos,TRUE)=0 Then
						nPos=GetScrollPos(Hwnd,SB_VERT)
						npos-=PageLength999
						SetScrollPos(Hwnd,SB_VERT,nPos,TRUE)
					EndIf
			End Select
		Case 16'WM_CLOSE
			Return 0
		Case 5'WM_SIZE
			Dim As HWND HWNDSTATUSBAR = GetProp(hwnd,"STATUSBARWINDOW")
			If HWNDSTATUSBAR<>0 Then
				SendMessage(HWNDSTATUSBAR,WM_SIZE,0,0)
			End If
			SizeX_ = LoWord(lParam)
			SizeY_ = HiWord(lParam)
		Case WM_NOTIFY
			Dim As LPNMHDR pnm
			#define _TTN_GETDISPINFO -520
			pnm =Cast( LPNMHDR,lParam)
			If pnm->code = -2 Then
				EventNumberListView_ =pnm->idFrom
				Dim act_ As LPNMITEMACTIVATE
				act_=Cast(LPNMITEMACTIVATE,pnm)
				eventitem_       = act_->iItem
				eventsubitem_    = act_->iSubItem
				flagKeyListView_ = act_->uKeyFlags
				SetProp(hwnd,"WM_NOTIFY_Event",Cast(handle,1))
				SendMessage(hwnd,WM_COMMAND,makelparam(EventNumberListView_,0),Cast(LPARAM,hwnd))
			EndIf
			If pnm->code = _TTN_GETDISPINFO Then
				Dim As LPNMTTDISPINFO pdi
				pdi =  Cast(LPNMTTDISPINFO,pnm)
				Dim As ZString Ptr Sptr = GetProp(hwnd,Str(pdi->hdr.idFrom))
				If Sptr<>0 Then
					pdi->szText = *Sptr
				EndIf
			ElseIf pnm->code = -108 Then'LVN_COLUMNCLICK
				EventNumberListView_ =pnm->idFrom
				Dim As LPNMLISTVIEW NMLW
				NMLW = Cast(LPNMLISTVIEW,pnm)
				columnevent_=NMLW->iSubItem
				eventitem_=NMLW->iItem
				eventsubitem_= NMLW->iSubItem
				List_Column_FLAG__=1
				SetProp(hwnd,"WM_NOTIFY_Event",Cast(handle,1))
				SendMessage(hwnd,WM_COMMAND,makelparam(EventNumberListView_,0),Cast(LPARAM,hwnd))
			ElseIf pnm->code = -9 Then 'LVN_BEGINDRAG
				Dim As LPNMLISTVIEW NMLW
				NMLW = Cast(LPNMLISTVIEW,pnm)
				columnevent_=NMLW->iSubItem
				eventitem_=NMLW->iItem
			ElseIf pnm->code=  TVN_SELCHANGED Then
				List_Column_FLAG__=1
				Dim As NMTREEVIEW Ptr NMTW
				NMTW=Cast(NMTREEVIEW Ptr,LPARAM)
				EventNumberTREEView_=pnm->idFrom
				eventTreeViewitem_=Cast(Integer,NMTW->itemNew.hItem)
			ElseIf pnm->code = NM_DBLCLK Then
				SendMessage(hwnd,WM_LBUTTONDBLCLK,0,0)
				If GadgetClass(Cast(hwnd,GadgetID(Cast(Integer,pnm->IDFrom))))=FBIDListView Then
					EventNumberListView_=pnm->IDFrom
				EndIf
			ElseIf pnm->code=  TCN_SELCHANGE Then
				Dim As TC_ITEM TC_ITEM_
				TC_ITEM_.mask=TCIF_PARAM
				SendMessage(pnm->hwndFrom,TCM_GETITEM,SendMessage(pnm->hwndFrom,TCM_GETCURSEL,0,0),Cast(LPARAM,@TC_ITEM_))
				ShowWindow(Cast(HWND,TC_ITEM_.LPARAm),1)
				List_Column_FLAG__=1
				SetProp(hwnd,"WM_NOTIFY_Event",Cast(handle,1))
				SendMessage(hwnd,WM_COMMAND,makelparam(pnm->idFrom,0),Cast(LPARAM,hwnd))
			ElseIf pnm->code=  TCN_SELCHANGING Then
				Dim As TC_ITEM TC_ITEM_
				TC_ITEM_.mask=TCIF_PARAM
				SendMessage(pnm->hwndFrom,TCM_GETITEM,SendMessage(pnm->hwndFrom,TCM_GETCURSEL,0,0),Cast(LPARAM,@TC_ITEM_))
				ShowWindow(Cast(HWND,TC_ITEM_.LPARAm),0)
			End If

	End Select

	If hMDIClient<>0 Then
		If GetParent(hMDIClient)=hWnd Then
			Return DefFrameProc(hWnd,hMDIClient , uMsg, wParam, lParam)
		EndIf
	EndIf
	Return DefWindowProc(hWnd,uMsg,wParam,lParam)
End Function

Function WindowEvent() As Long Export
	Dim uMsg As UINT, WParam As WPARAM, lParam As LPARAM, hWnd As HWND
	EventID=0
	EventHwnd_=0
	EventNumber_=0
	EventX = 0
	EventY = 0
	While fbguiCountEvents = 0
		'GetMessage(@fbguiMsg,0,0,0)
		If PeekMessage(@fbguiMsg,0,0,0,PM_REMOVE) <> 0 Then
			If CheckAccelerator=1 Then
				Dim As HWND   hwndaccel_
				Dim As HACCEL Haacceell_
				For ha As Integer=0 To UBound_Accel
					If ACCEL_HWND_(ha).hwnd=fbguiMsg.hwnd Then
						hwndaccel_=fbguiMsg.hwnd
						Haacceell_=ACCEL_HWND_(ha).hAccel
					Else
						Dim As HWND parentcheck1,parentcheck=fbguiMsg.hwnd
						Do
							parentcheck1=GetParent(parentcheck)
							If parentcheck1<>0 Then parentcheck=parentcheck1
						Loop Until parentcheck1=0
						If ACCEL_HWND_(ha).hwnd= parentcheck Then
							hwndaccel_=parentcheck
							Haacceell_=ACCEL_HWND_(ha).hAccel
						End If
					End If
				Next
				If TranslateAccelerator( hwndaccel_ , Haacceell_ , @fbguiMsg) = 0 Then
					TranslateMessage(@fbguiMsg)
					DispatchMessage(@fbguiMsg)
				End If
			Else
				TranslateMessage(@fbguiMsg)
				DispatchMessage(@fbguiMsg)
			End If

			If Address_Function___8<>0 Then
				OnWindowProcCallBack_2(fbguiMSG.hwnd,fbguiMSG.message,fbguiMSG.wParam,fbguiMSG.lParam,fbguiMSG.time,fbguiMSG.pt)
			End If
		Else
			Return 0
		EndIf

	Wend

	hWnd   = Cast(HWND,fbguiEventsBuffer(fbguiCountEvents).Handle)
	uMsg   = fbguiEventsBuffer(fbguiCountEvents).Message
	wParam = fbguiEventsBuffer(fbguiCountEvents).wParam
	lParam = fbguiEventsBuffer(fbguiCountEvents).lParam
	fbguiCountEvents -= 1
	'fbguiUserData=Cast(Byte Ptr,GetWindowLong(hWnd,-21))'GWL_USERDATA=-21

	EVENT_W_PARAM=wParam
	EVENT_L_PARAM=lParam
	Select Case uMsg
		Case 16'WM_CLOSE
			EventID    = EventClose
			EventHwnd_ = hWnd
		Case 5'WM_SIZE
			EventID    = EventSize
			EventHwnd_ = hWnd
		Case 273'WM_COMMAND
			EventHwnd_ = Cast(HWND,lParam)
			If EventHwnd_ = 0 Then
				EventNumber_ = LoWord(wParam)
				EventID      = EventMenu
				EventHwnd_   = hWnd
			Else
				If GetFocus = EventHwnd_ Or GetProp(hwnd,"WM_NOTIFY_Event")=1 _
					Or GadgetClass(Cast(hwnd,lparam)) = STATICCONTROL _
					Or GadgetClass(Cast(hwnd,lparam)) = FBTOOLBAR Then
					EventID             = EventGadget
					EventNumber_        = LoWord(wParam)
					EventNumberToolBar_ = EventNumber_
					EventActiveIDGadget = lparam
					SetProp(hwnd,"WM_NOTIFY_Event",Cast(handle,0))
				EndIf
			End If
		Case 256'WM_KEYDOWN
			EventID    = EventKeyDown
			EventHwnd_ = hWnd
			EventKEY_  = LoWord(wParam)
		Case 257'WM_KEYUP
			EventID      = EventKeyUp
			EventHwnd_   = hWnd
			EventNumber_ = LoWord(wParam)
		Case 512'WM_MOUSEMOVE
			EventID    = EventMouseMove
			EventHwnd_ = hWnd
			EventKEY_  = wParam
			EventX     = LoWord(lParam)
			EventY     = HiWord(lParam)
		Case 522'WM_MOUSEWHEEL
			EventID     = EventMouseWheel
			EventHwnd_  = hWnd
			EventKEY_   = wParam
			EventX      = LoWord(lParam)
			EventY      = HiWord(lParam)
		Case 513 'WM_LBUTTONDOWN,WM_RBUTTONDOWN,WM_MBUTTONDOWN
			If GetProp(hwnd,"panelevent") <> 1 Then
				EventID    = EventLBDown'WM_LBUTTONDOWN
				EventHwnd_ = hWnd
				EventKEY_  = wParam
				EventX     = LoWord(lParam)
				EventY     = HiWord(lParam)
				If lParam=-1 Then
					EventNumber_  = wParam
				Else
					EventNumber_  = 0
				EndIf
			EndIf
		Case 516
			If GetProp(hwnd,"panelevent") <> 1  Then
				EventID    = EventRBDown'WM_RBUTTONDOWN
				EventHwnd_ = hWnd
				EventKEY_  = wParam
				EventX     = LoWord(lParam)
				EventY     = HiWord(lParam)
				If lParam=-1 Then
					EventNumber_        = wParam
				Else
					EventNumber_        = 0
				EndIf
			EndIf
		Case 519
			If GetProp(hwnd,"panelevent") <> 1  Then
				EventID    = EventMBDown'WM_MBUTTONDOWN
				EventHwnd_ = hWnd
				EventKEY_  = wParam
				EventX     = LoWord(lParam)
				EventY     = HiWord(lParam)
				If lParam=-1 Then
					EventNumber_ = wParam
				Else
					EventNumber_ = 0
				EndIf
			endif
		Case 514
			If GetProp(hwnd,"panelevent") <> 1  Then
				EventID      = EventLBUp'WM_LBUTTONUP
				EventHwnd_   = hWnd
				EventKEY_    = wParam
				EventX       = LoWord(lParam)
				EventY       = HiWord(lParam)
			EndIf
		Case 517
			If GetProp(hwnd,"panelevent") <> 1  Then
				EventID      = EventRBUp'WM_RBUTTONUP
				EventHwnd_   = hWnd
				EventKEY_    = wParam
				EventX       = LoWord(lParam)
				EventY       = HiWord(lParam)
			EndIf
		Case 520
			If GetProp(hwnd,"panelevent") <> 1  Then
				EventID      = EventMBUp'WM_MBUTTONUP
				EventHwnd_   = hWnd
				EventKEY_    = wParam
				EventX       = LoWord(lParam)
				EventY       = HiWord(lParam)
			endif
		Case 276
			EventID      = WM_HSCROLL
			EventNumber_ = LoWord(wParam)
			EventHwnd_   = Cast(hwnd,lParam)
		Case 277
			EventID      = WM_VSCROLL
			EventNumber_ = LoWord(wParam)
			EventHwnd_   = Cast(hwnd,lParam)
		Case 275 'WM_TIMER
			EventID      = EventTimer
			EventHwnd_   = hWnd
			EventNumber_ = wParam
		Case 6 'WM_ACTIVATE
			If LoWord(wParam) = WA_CLICKACTIVE Then
				EventID    = EventActivate
				EventHwnd_ = hWnd
			EndIf
		Case WM_NOTIFY
			If List_Column_FLAG__=1 Then
				List_Column_FLAG__=0
			End If
			EventNumberToolBar_=-1
			Select Case  GadgetClass(Cast(hwnd,GadgetID(LoWord(wParam))))
				Case SYSDATETIMEPICK32,SYSMONTHCAL32,STATICCONTROL,FBIDTRACKBAR
					EventHwnd_  = Cast(HWND,lParam)
					EventNumber_= LoWord(wParam)
					EventID     = EventGadget
			End Select
			If GadgetClass(Cast(HWND,GetProp(GadgetID(LoWord(wParam)),"addparam60000")))=MSCTLS_UPDOWN32 Then
				EventHwnd_   = Cast(HWND,lParam)
				EventNumber_ = LoWord(wParam)
				EventID      = EventGadget
			End If
		Case WM_PARENTNOTIFY
			If List_Column_FLAG__=0 Then
				Select Case  LoWord(wParam)
					Case 513,516,519
						If GetProp(hwnd,"eventmousebutton") <> 1 Then ' для контейнера
							If GetProp(hwnd,"panelevent")<> 1 Then
								If LoWord(wParam)=513 Then EventID=EventLBDown
								If LoWord(wParam)=516 Then EventID=EventRBDown
								If LoWord(wParam)=519 Then EventID=EventMBDown
							EndIf
							EventX = LoWord(lParam)
							EventY = HiWord(lParam)
						Else
							SetProp(hwnd,"eventmousebutton",cast(handle,0)) ' для контейнера
						EndIf
				End Select
			End If
		Case WM_VKEYTOITEM
			EventKEY_    = LoWord(wParam)
			Eventid      = EventGadget
			EventNumber_ = ID_In_Number(Cast(HWND,lparam))
		Case WM_PAINT
			EventID  = EventPaint
			EventHDC = Cast(HDC,wParam)
		Case Else
			EventID=UMSG
	End Select
	If EventID<>0 Then
		UpdateWindow(hwnd)
	EndIf
	Return EventID
End Function

Function WaitEvent() As Long Export
	WaitMessage
	Return WindowEvent()
End Function
