输入框核心脚本:
#==============================================================================
# ■ TextBox by EngShun
#------------------------------------------------------------------------------
# 输入框的类。
#==============================================================================
class TextBox
#--------------------------------------------------------------------------
# ● 设置API
#--------------------------------------------------------------------------
CreateWindow = Win32API.new('user32','CreateWindowEx','lpplllllllll','l')
ShowWindow = Win32API.new('user32','ShowWindow','ll','l')
DestroyWindow = Win32API.new('user32','DestroyWindow','l','l')
GetWindowText = Win32API.new('user32','GetWindowText','lpl','l')
SetWindowText = Win32API.new("user32", "SetWindowText", "lp", "l")
GetWindowTextLength = Win32API.new('user32','GetWindowTextLength','l','l')
UpdateWindow = Win32API.new('user32','UpdateWindow','l','i')
SetFocus = Win32API.new('user32','SetFocus','l','l')
SendMessage = Win32API.new('user32','SendMessage','llll','l')
MultiByteToWideChar = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
WideCharToMultiByte = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
GetKeyState = Win32API.new("user32","GetAsyncKeyState",'I','I')
#--------------------------------------------------------------------------
# ● 定义实例变量
#--------------------------------------------------------------------------
attr_accessor :focus
#--------------------------------------------------------------------------
# ● 初始化
#--------------------------------------------------------------------------
def initialize(text = "",limit = 8,number=false)
c = 0x40011000
c = (c|0x00001000) if number
@window = CreateWindow.call(0, 'EDIT', u2s(text), c, 0, 480-22, 640, 22, get_hWnd, 0, 0, 0)
SendMessage.call(@window,0xC5,limit,0)
@focus = true
@disposed = false
end
#--------------------------------------------------------------------------
# ● 释放
#--------------------------------------------------------------------------
def dispose
@disposed = true
DestroyWindow.call(@window)
end
#--------------------------------------------------------------------------
# ● 刷新
#--------------------------------------------------------------------------
def update
return if !@focus or @disposed
UpdateWindow.call(@window)
SetFocus.call(@window)
end
#--------------------------------------------------------------------------
# ● 获取内容
#--------------------------------------------------------------------------
def text
return if @disposed
l = GetWindowTextLength.call(@window)
str = "" * (l + 1)
GetWindowText.call(@window, str, l+1)
return s2u(str.delete(""))
end
#--------------------------------------------------------------------------
# ● 更改内容
#--------------------------------------------------------------------------
def text=(str)
SetWindowText.call(@window, u2s(str))
return if @disposed
end
#--------------------------------------------------------------------------
# ● 获取光标位置
#--------------------------------------------------------------------------
def sel_pos
return if @disposed
return (SendMessage.call(@window,0xB0,0,0) % 65535) / 2
end
#--------------------------------------------------------------------------
# ● 设置光标位置
#--------------------------------------------------------------------------
def sel_pos=(i)
return if @disposed
SendMessage.call(@window,0xB1,i,i)
end
#--------------------------------------------------------------------------
# ● 获取光标位置
#--------------------------------------------------------------------------
def limit
return if @disposed
return SendMessage.call(@window,0xD5,0,0)
end
#--------------------------------------------------------------------------
# ● 设置光标位置
#--------------------------------------------------------------------------
def limit=(i)
return if @disposed
SendMessage.call(@window,0xC5,i,0)
end
#--------------------------------------------------------------------------
# ● 是否按下回车键
#--------------------------------------------------------------------------
def press_enter?
return if @disposed
return (@focus and (GetKeyState.call(13) != 0))
end
#--------------------------------------------------------------------------
# ● 转码:系统转UTF-8
#--------------------------------------------------------------------------
def s2u(text)
len = MultiByteToWideChar.call(0, 0, text, -1, nil, 0);
buf = "" * (len*2)
MultiByteToWideChar.call(0, 0, text, -1, buf, buf.size/2);
len = WideCharToMultiByte.call(65001, 0, buf, -1, nil, 0, nil, nil);
ret = "" * len
WideCharToMultiByte.call(65001, 0, buf, -1, ret, ret.size, nil, nil);
return ret.delete("00")
end
#--------------------------------------------------------------------------
# ● 转码:UTF-8转系统
#--------------------------------------------------------------------------
def u2s(text)
len = MultiByteToWideChar.call(65001, 0, text, -1, nil, 0);
buf = "" * (len*2)
MultiByteToWideChar.call(65001, 0, text, -1, buf, buf.size/2);
len = WideCharToMultiByte.call(0, 0, buf, -1, nil, 0, nil, nil);
ret = "" * len
WideCharToMultiByte.call(0, 0, buf, -1, ret, ret.size, nil, nil);
return ret.delete("00")
end
private :u2s ,:s2u # 定义私有功能
end