设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 2238|回复: 5

[已经过期] 关于API,让一个输入条支持中文

 关闭 [复制链接]

Lv1.梦旅人

路人党员

梦石
0
星屑
51
在线时间
2276 小时
注册时间
2010-12-30
帖子
3225
发表于 2011-6-8 22:18:34 | 显示全部楼层 |阅读模式
1星屑
本帖最后由 英顺的马甲 于 2011-6-8 22:23 编辑

怎么说呢,输入中文是没问题,
但是输出的时候就是一堆乱码,
这是脚本:
  1. #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::​
  2. # Run-Time Script Caller
  3. # Author: ForeverZer0
  4. # Version: 1.0
  5. # Date: 11.27.2010
  6. #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::​
  7. #
  8. # Introduction:
  9. #
  10. #  This script will allow you to open a small box while the game is running and
  11. #  type script calls into, which will execute when the Enter button is pressed.
  12. #
  13. # Feature:
  14. #
  15. #  - Simple to use.
  16. #  - Built in rescue to keep game from crashing if the script call is written
  17. #    wrong, etc. Instead it shows the error and continues on.
  18. #  - Did I mention you can make up script calls and change things at run-time.
  19. #
  20. # Instructions:
  21. #
  22. #  - Place script anywhere.
  23. #  - Configure call button below (F7 by default)
  24. #
  25. #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::​

  26. #===============================================================================​
  27. # ** ScriptEditor
  28. #===============================================================================​

  29. class ScriptEditor
  30.   
  31.   CALL_BUTTON = Input::F7
  32.   # Set the button to call the script box.
  33.   
  34.   def initialize
  35.     # Get game window title from Game.ini
  36.     ini = Win32API.new('kernel32', 'GetPrivateProfileString','PPPPLP', 'L')
  37.     @title = "\0" * 256
  38.     ini.call('Game', 'Title', '', @title, 256, '.\\Game.ini')
  39.     @title.delete!("\0")
  40.     # Set game window to an instance variable, using the title we found.
  41.     @main = Win32API.new('user32', 'FindWindowA', 'PP', 'L').call('RGSS Player', @title)
  42.     # Set variables to call for creating showing, and destroying the window.
  43.     @create_window = Win32API.new('user32','CreateWindowEx','lpplllllllll','l')
  44.     @show_window = Win32API.new('user32','ShowWindow',%w(l l),'l')
  45.     @destroy_window = Win32API.new('user32','DestroyWindow','p','l')
  46.     # Set variables to get the window size, position, text, etc, etc.
  47.     @window_text = Win32API.new('user32','GetWindowText',%w(n p n ),'l')
  48.     @metrics = Win32API.new('user32', 'GetSystemMetrics', 'I', 'I')
  49.     @set_window_pos = Win32API.new('user32', 'SetWindowPos', 'LLIIIII', 'I')
  50.     @window_rect = Win32API.new('user32','GetWindowRect',%w(l p),'i')
  51.     # Define the coordinates to display the window.
  52.     @x = (@metrics.call(0) - 576) / 2
  53.     @y = (@metrics.call(1) - 22) / 2
  54.     # Updates the client area of the window.
  55.     @update_window = Win32API.new('user32','UpdateWindow','p','i')
  56.     # Set a button that will register when button is pressed.
  57.     @input = Win32API.new('user32','GetAsyncKeyState','i','i')
  58.   end  
  59.   
  60.   def destroy_window
  61.     # Disposes the created box for typing text, and sets variable to nil.
  62.     @destroy_window.call(@script_window)
  63.     @script_window = nil
  64.   end
  65.    
  66.   def show_window
  67.     # End method now if window is already there.
  68.     if @script_window != nil
  69.       return
  70.     end
  71.     # Create text box for adding text into, using window dimensions.
  72.     @script_window = @create_window.call(768, 'Edit', '', 0x86000000, @x, @y, 576, 22, @main, 0, 0, 0)
  73.     # Set the 'visibility' of the window.
  74.     @show_window.call(@script_window, 1)
  75.     # Begin the loop for the text box.
  76.     start_script_update
  77.   end  
  78.   
  79.   def start_script_update
  80.     # Enter update loop.
  81.     loop {
  82.       # Update the Graphics, and the window. Breaks when Enter button is pressed.
  83.       Graphics.update
  84.       @update_window.call(@script_window)
  85.       break if @input.call(0x0D) & 0x01 == 1
  86.     }
  87.     # Get the text from the window.
  88.     text = "\0" * 256
  89.     @window_text.call(@script_window, text, 0x3E80)
  90.     text.delete!("\0")
  91.     # Evaluate the text, simply displaying a message if it throws an error.
  92.     begin
  93.       eval(text)
  94.     rescue#'start_script_update'
  95.       # Simply write the type of the error.
  96.       message = $!.message.gsub("(eval):1:in `start_script_update'") { '' }
  97.       message = message.gsub("(eval):1:in"){''}
  98.       print("执行脚本时发生了 #{$!.class}\r\n\r\n#{message}")
  99.     end
  100.     destroy_window
  101.   end
  102. end

  103. if $DEBUG
  104.   
  105.   $editor = ScriptEditor.new
  106.   # Create in instance of the class so it is ready to call.
  107.   
  108.   module Input
  109.    
  110.     class << self
  111.       alias zer0_script_editor_upd update
  112.     end
  113.    
  114.     def self.update
  115.       # Alias the Input.update method to check if call button is pressed.
  116.       $editor.show_window if self.trigger?(ScriptEditor::CALL_BUTTON)
  117.       zer0_script_editor_upd
  118.     end
  119.   end
  120. end
复制代码
此脚本的原网站:http://www.rpgmakers.net/showthread.php?tid=99
已尝试过紫苏的 to_unicode 和 to_UTF8

最佳答案

查看完整内容

在第 95 行前面插入如下 2 行:然后再加上这个脚本:
本人擅长XP,如果有脚本或者Ruby方面的问题欢迎发电邮到[email protected]咨询,本人很少检查电邮所以不一定会及时回复,本人不会直接出手解决问题只会提供一个方向,所以谢绝伸手党

Lv4.逐梦者

梦石
0
星屑
6545
在线时间
1666 小时
注册时间
2008-10-29
帖子
6710

贵宾

发表于 2011-6-8 22:18:35 | 显示全部楼层
在第 95 行前面插入如下 2 行:
  1.       text = EasyConv.s2u(text)
  2.       text.delete!("\000")
复制代码
然后再加上这个脚本:
  1. #------------------------------------------------------------------------------
  2. # Moonlight INN
  3. # http://cgi.members.interq.or.jp/aquarius/rasetsu/
  4. # RaTTiE
  5. # [email protected]
  6. #------------------------------------------------------------
  7. # EasyConv::s2u(text) : S-JIS -> UTF-8
  8. # EasyConv::u2s(text) : UTF-8 -> S-JIS
  9. #==============================================
  10. module EasyConv
  11.   CP_ACP = 0
  12.   CP_UTF8 = 65001
  13.   #--------------------------------------------------------------------------
  14.   # 仠 S-JIS -> UTF-8
  15.   #--------------------------------------------------------------------------
  16.   def s2u(text)
  17.     m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
  18.     w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
  19.     # S-JIS -> Unicode
  20.     len = m2w.call(CP_ACP, 0, text, -1, nil, 0);
  21.     buf = "\0" * (len*2)
  22.     m2w.call(CP_ACP, 0, text, -1, buf, buf.size/2);
  23.     # Unicode -> UTF-8
  24.     len = w2m.call(CP_UTF8, 0, buf, -1, nil, 0, nil, nil);
  25.     ret = "\0" * len
  26.     w2m.call(CP_UTF8, 0, buf, -1, ret, ret.size, nil, nil);
  27.     return ret
  28.   end
  29.   module_function :s2u
  30.   #--------------------------------------------------------------------------
  31.   # 仠 UTF-8 -> S-JIS
  32.   #--------------------------------------------------------------------------
  33.   def u2s(text)
  34.     m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
  35.     w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
  36.     # UTF-8 -> Unicode
  37.     len = m2w.call(CP_UTF8, 0, text, -1, nil, 0);
  38.     buf = "\0" * (len*2)
  39.     m2w.call(CP_UTF8, 0, text, -1, buf, buf.size/2);
  40.     # Unicode -> S-JIS
  41.     len = w2m.call(CP_ACP, 0, buf, -1, nil, 0, nil, nil);
  42.     ret = "\0" * len
  43.     w2m.call(CP_ACP, 0, buf, -1, ret, ret.size, nil, nil);
  44.     return ret
  45.   end
  46.   module_function :u2s
  47. end

  48. #========================================
  49. # 本脚本来自www.66rpg.com
  50. #========================================
复制代码

点评

= =最近研究API较多.这东西经常用到.很快速的就找到了...  发表于 2011-6-9 00:04
本来也想这样回答来着= =但是转换字符的脚本找不到了  发表于 2011-6-9 00:01











你知道得太多了

回复

使用道具 举报

Lv2.观梦者

梦石
0
星屑
491
在线时间
339 小时
注册时间
2010-12-15
帖子
926

开拓者

发表于 2011-6-8 22:19:15 | 显示全部楼层
请参考RMXP中文输入法
回复

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
60
在线时间
43 小时
注册时间
2010-7-18
帖子
14
发表于 2011-6-9 08:53:20 | 显示全部楼层
RGSS默认使用UNICODE(具体我也不知道,反正是UNICODE的一种),WIN32API不支持这种编码,只有A(ANSI)和W(好像是WideChar)两种形式,所以必须转码后使用
我对字符串编码不熟悉,所以可能有说错的地方

点评

RGSS内部是UTF8~  发表于 2011-6-9 15:22
回复

使用道具 举报

头像被屏蔽

Lv1.梦旅人

梦石
0
星屑
49
在线时间
143 小时
注册时间
2009-3-21
帖子
496
发表于 2011-6-9 12:56:47 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
签名被屏蔽
回复

使用道具 举报

头像被屏蔽

Lv1.梦旅人

梦石
0
星屑
49
在线时间
143 小时
注册时间
2009-3-21
帖子
496
发表于 2011-6-9 13:10:15 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
签名被屏蔽
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-4-19 01:24

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表