#include once "window9.bi"

Type InputBox_
	As MSG msg
	As HWND hWnd,hwnd1,hwnd2,hwnd3
	#ifdef UNICODE
		As WString*1024 mess
	#else	
		As String*1024 mess
	#EndIf 
	As BOOL flag
	as DEVMODE dm(0)
	As HFONT font,font1
	As Integer size
End Type

Function InputBox(ByRef Caption As USTRING, ByRef Message As USTRING, ByRef DefaultString As USTRING, ByVal flag As Integer, ByVal flag2 As Integer, hParentWin as Hwnd = 0) As USTRING Export
	dim as HWND hwFocus = GetFocus()
	Dim InputBox_ As InputBox_
	InputBox_.dm(0).dmSize = sizeof(DEVMODE)
	EnumDisplaySettings( 0, ENUM_CURRENT_SETTINGS, @InputBox_.dm(0))
	#ifdef UNICODE
		InputBox_.hWnd  = CreateWindowEx(WS_EX_CONTROLPARENT, "#32770", *Caption, WS_TILED Or WS_VISIBLE, InputBox_.dm(0).dmPelsWidth/2-155, InputBox_.dm(0).dmPelsHeight/2-70, 310, 130, hParentWin, 0, 0, 0 )
		InputBox_.hWnd1 = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", *DefaultString, WS_CHILD Or WS_VISIBLE Or WS_TABSTOP Or flag, 10, 33, 275, 25, InputBox_.hwnd,0,0,0)
		InputBox_.hWnd2 = CreateWindowEx(0, "Button", "OK", WS_CHILD Or WS_VISIBLE or WS_TABSTOP, 106, 65, 80, 25, InputBox_.hwnd,0,0,0)
		InputBox_.hWnd3 = CreateWindowEx(0, "Static", *Message, WS_CHILD Or WS_VISIBLE, 10, 10, 275, 20, InputBox_.hwnd,0,0,0)  	
	#else
		InputBox_.hWnd  = CreateWindowEx(WS_EX_CONTROLPARENT, "#32770", Caption, WS_TILED Or WS_VISIBLE, InputBox_.dm(0).dmPelsWidth/2-155, InputBox_.dm(0).dmPelsHeight/2-70, 310, 130, hParentWin, 0, 0, 0 )
		InputBox_.hWnd1 = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", DefaultString, WS_CHILD Or WS_VISIBLE Or WS_TABSTOP or flag, 10, 33, 275, 25, InputBox_.hwnd,0,0,0)
		InputBox_.hWnd2 = CreateWindowEx(0, "Button", "OK", WS_CHILD Or WS_VISIBLE or WS_TABSTOP , 106, 65, 80, 25, InputBox_.hwnd,0,0,0)
		InputBox_.hWnd3 = CreateWindowEx(0, "Static", Message, WS_CHILD Or WS_VISIBLE, 10, 10, 275, 20, InputBox_.hwnd,0,0,0)  	
	#EndIf
	
	InputBox_.size  = -MulDiv(10, GetDeviceCaps(CreateDC("DISPLAY",0,0,0), LOGPIXELSY), 72)
	InputBox_.font  = CreateFont(InputBox_.size,0,0,0,0,1,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH Or FF_DONTCARE,"Times New Roman")
	SendMessage(InputBox_.hWnd3,WM_SETFONT,Cast(WPARAM,InputBox_.font),0)
	InputBox_.font1 = CreateFont(InputBox_.size,0,0,0,0,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH Or FF_DONTCARE,"Times New Roman")
	SendMessage(InputBox_.hWnd2,WM_SETFONT,Cast(WPARAM,InputBox_.font1),0)
	SendMessage(InputBox_.hWnd1,WM_SETFONT,Cast(WPARAM,InputBox_.font1),0)
	
	While GetMessage(@InputBox_.msg, 0, 0, 0 )
		if IsDialogMessage(InputBox_.hWnd ,@InputBox_.msg)  = 0 then
            TranslateMessage(@InputBox_.msg )
            DispatchMessage(@InputBox_.msg )
		EndIf
		Select Case InputBox_.msg.hwnd
			Case InputBox_.hWnd2
				dim as Integer iMsg = InputBox_.msg.message
				if (iMsg = WM_LBUTTONDOWN) orelse (iMsg = WM_KEYUP andalso (InputBox_.msg.wparam = 32 orelse InputBox_.msg.wparam = 13)) then
                    SendMessage(InputBox_.hWnd1,WM_GETTEXT,1024,Cast(LPARAM ,@InputBox_.mess))
                    dim as USTRING sRet = InputBox_.mess
                    Function = sRet
                    DestroyWindow(InputBox_.hWnd)
                    InputBox_.flag=0
                    SetFocus(hwFocus)
                    Exit Function
				endif		
			Case InputBox_.hWnd1
				If InputBox_.msg.message=WM_LBUTTONDOWN Then
					If flag2<>0 Then
						If InputBox_.flag=0 Then
							InputBox_.flag=1
							SetWindowText(InputBox_.hWnd1 , "")
						EndIf
					EndIf
				EndIf
		End Select
	Wend
	SetFocus(hwFocus)
End Function
