Project1
标题:
怎么接受用户输入汉字
[打印本页]
作者:
myself105
时间:
2012-10-3 10:17
标题:
怎么接受用户输入汉字
在RPGmaker中怎么样跳出输入框,接受用户输入,然后再作出判断。
作者:
1105741847
时间:
2012-10-3 10:58
xp的话,用拼音脚本(我原来用过)(完美输入法),但其它的我就不知道啦
作者:
chd114
时间:
2012-10-3 12:29
#==============================================================================
# ■ Kernel
#------------------------------------------------------------------------------
# 该模块中定义了可供所有类使用的方法。Object 类中包含了该模块。
#==============================================================================
module Kernel
#--------------------------------------------------------------------------
# ● 需要的 Windows API 函数
#--------------------------------------------------------------------------
GetWindowThreadProcessId = Win32API.new("user32", "GetWindowThreadProcessId", "LP", "L")
GetWindow = Win32API.new("user32", "GetWindow", "LL", "L")
GetClassName = Win32API.new("user32", "GetClassName", "LPL", "L")
GetCurrentThreadId = Win32API.new("kernel32", "GetCurrentThreadId", "V", "L")
GetForegroundWindow = Win32API.new("user32", "GetForegroundWindow", "V", "L")
#--------------------------------------------------------------------------
# ● 获取窗口句柄
#--------------------------------------------------------------------------
def get_hWnd
# 获取调用线程(RM 的主线程)的进程标识
threadID = GetCurrentThreadId.call
# 获取 Z 次序中最靠前的窗口
hWnd = GetWindow.call(GetForegroundWindow.call, 0)
# 枚举所有窗口
while hWnd != 0
# 如果创建该窗口的线程标识匹配本线程标识
if threadID == GetWindowThreadProcessId.call(hWnd, 0)
# 分配一个 11 个字节的缓冲区
className = " " * 11
# 获取该窗口的类名
GetClassName.call(hWnd, className, 12)
# 如果匹配 RGSS Player 则跳出循环
break if className == "RGSS Player"
end
# 获取下一个窗口
hWnd = GetWindow.call(hWnd, 2)
end
return hWnd
end
end
#==============================================================================
# ■ 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 = "\0" * (l + 1)
GetWindowText.call(@window, str, l+1)
return s2u(str.delete("\0"))
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 = "\0" * (len*2)
MultiByteToWideChar.call(0, 0, text, -1, buf, buf.size/2);
len = WideCharToMultiByte.call(65001, 0, buf, -1, nil, 0, nil, nil);
ret = "\0" * len
WideCharToMultiByte.call(65001, 0, buf, -1, ret, ret.size, nil, nil);
return ret.delete("\000")
end
#--------------------------------------------------------------------------
# ● 转码:UTF-8转系统
#--------------------------------------------------------------------------
def u2s(text)
len = MultiByteToWideChar.call(65001, 0, text, -1, nil, 0);
buf = "\0" * (len*2)
MultiByteToWideChar.call(65001, 0, text, -1, buf, buf.size/2);
len = WideCharToMultiByte.call(0, 0, buf, -1, nil, 0, nil, nil);
ret = "\0" * len
WideCharToMultiByte.call(0, 0, buf, -1, ret, ret.size, nil, nil);
return ret.delete("\000")
end
private :u2s ,:s2u # 定义私有功能
end
#==============================================================================
# ■ Sprite_TextBox by EngShun
#------------------------------------------------------------------------------
# 显示输入框用的活动块。
#==============================================================================
class Sprite_TextBox < Sprite
#--------------------------------------------------------------------------
# ● 设置缓存的类
#--------------------------------------------------------------------------
Cache = Struct.new(:bc ,:fc ,:fs ,:text ,:sel_pos)
#--------------------------------------------------------------------------
# ● 定义实例变量
#--------------------------------------------------------------------------
attr_accessor :textbox
attr_accessor :back_color
#--------------------------------------------------------------------------
# ● 初始化
#--------------------------------------------------------------------------
def initialize(x = 0,y = 0,w = 128,h = 32,bc = Color.new(255,255,255),fc = Color.new(0,0,0),tb = TextBox.new)
super(nil)
self.x = x
self.y = y
self.bitmap = Bitmap.new(w,h)
self.bitmap.font.color = fc
@back_color = bc
@textbox = tb
@cache = Cache.new
refresh
end
#--------------------------------------------------------------------------
# ● 刷新
#--------------------------------------------------------------------------
def update
super
return unless @textbox.focus
@textbox.update
if (self.bitmap.font.color != @cache.fc) or
(self.bitmap.font.size != @cache.fs) or
(@textbox.sel_pos != @cache.sel_pos) or
(@textbox.text != @cache.text) or
(@back_color != @cache.bc)
refresh
end
end
#--------------------------------------------------------------------------
# ● 释放
#--------------------------------------------------------------------------
def dispose
self.bitmap.dispose
@textbox.dispose
super
end
#--------------------------------------------------------------------------
# ● 内容刷新
#--------------------------------------------------------------------------
def refresh
@cache.fc = self.bitmap.font.color
@cache.fs = self.bitmap.font.size
@cache.sel_pos = @textbox.sel_pos
@cache.text = @textbox.text
@cache.bc = @back_color
w = self.bitmap.width
h = self.bitmap.height
self.bitmap.fill_rect(0,0,w,h,@back_color)
self.bitmap.draw_text(4,0,w-4,h,@textbox.text)
t = @textbox.text.scan(/./)[0,@textbox.sel_pos].join("")
s = self.bitmap.text_size(t).width
self.bitmap.draw_text((4 + s)-16,0,32,h,"|",1)
end
private :refresh # 设置私有功能
end
#==============================================================================
# ■ Scene_Name
#------------------------------------------------------------------------------
# 处理名称输入画面的类。
#==============================================================================
class Scene_Name
#--------------------------------------------------------------------------
# ● 主处理
#--------------------------------------------------------------------------
def main
@actor = $game_actors[$game_temp.name_actor_id]
@window = Window_Base.new(0,176,640,128)
@window.contents = Bitmap.new(576,96)
@window.draw_actor_graphic(@actor, 64, 80)
@window.contents.draw_text(128,0,480,32,"请输入名称:")
@sprite_tb = Sprite_TextBox.new(144,240,464)
@sprite_tb.z = 500
@sprite_tb.textbox.limit = $game_temp.name_max_char
@sprite_tb.textbox.text = @actor.name
@sprite_tb.textbox.sel_pos = @actor.name.length
Graphics.transition
loop do
Graphics.update
Input.update
update
break if $scene != self
end
Graphics.freeze
@sprite_tb.dispose
@window.dispose
end
#--------------------------------------------------------------------------
# ● 刷新画面
#--------------------------------------------------------------------------
def update
@sprite_tb.update
if @sprite_tb.textbox.press_enter?
if @sprite_tb.textbox.text == ""
$game_system.se_play($data_system.buzzer_se)
return
end
@actor.name = @sprite_tb.textbox.text
$scene = Scene_Map.new
end
end
end
复制代码
就是这个脚本
作者:
myself105
时间:
2012-10-3 15:13
chd114 发表于 2012-10-3 12:29
就是这个脚本
该怎么使用呢,我看不懂呢,多谢回答!
作者:
hys111111
时间:
2012-10-3 15:25
myself105 发表于 2012-10-3 15:13
该怎么使用呢,我看不懂呢,多谢回答!
打开脚本编辑器,在main前面插入。
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1