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

Project1

 找回密码
 注册会员
搜索

如何判断游戏的窗口关闭按钮的按下

查看数: 2366 | 评论数: 6 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2012-10-13 13:55

正文摘要:

就是这个

回复

亿万星辰 发表于 2012-10-14 14:39:08
  1. #==============================================================================
  2. # ■ EasyConv
  3. #------------------------------------------------------------------------------
  4. #  转码模块。
  5. #==============================================================================
  6. module EasyConv
  7.   #--------------------------------------------------------------------------
  8.   # ● 常量定义
  9.   #--------------------------------------------------------------------------
  10.   CP_ACP = 0
  11.   CP_UTF8 = 65001
  12.   #--------------------------------------------------------------------------
  13.   # ● 模块函数
  14.   #--------------------------------------------------------------------------
  15.   module_function
  16.   #--------------------------------------------------------------------------
  17.   # ● 转码
  18.   #--------------------------------------------------------------------------
  19.   def s2u(text)
  20.     m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
  21.     w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
  22.     len = m2w.call(CP_ACP, 0, text, -1, nil, 0)
  23.     buf = "\0" * (len*2)
  24.     m2w.call(CP_ACP, 0, text, -1, buf, buf.size/2)
  25.     len = w2m.call(CP_UTF8, 0, buf, -1, nil, 0, nil, nil)
  26.     ret = "\0" * len
  27.     w2m.call(CP_UTF8, 0, buf, -1, ret, ret.size, nil, nil)
  28.     ret[-1] = ""
  29.     return ret
  30.   end
  31.   #--------------------------------------------------------------------------
  32.   # ● 转码
  33.   #--------------------------------------------------------------------------
  34.   def u2s(text)
  35.     m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
  36.     w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
  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.     len = w2m.call(CP_ACP, 0, buf, -1, nil, 0, nil, nil)
  41.     ret = "\0" * len
  42.     w2m.call(CP_ACP, 0, buf, -1, ret, ret.size, nil, nil)
  43.     return ret
  44.   end
  45. end

  46. HWND = Win32API.new("user32", "GetActiveWindow", nil, 'l')
  47. module Kernel
  48.   unless defined?(old_exit)
  49.     MessageBox = Win32API.new('user32', 'MessageBox', "p p p i", 'i')
  50.     alias old_exit exit
  51.     def exit(*arg)
  52.       begin
  53.         old_exit(*arg) if arg[0] == true
  54.         text1 = "确定要退出游戏么?"
  55.         text2 = "退出游戏"
  56.         msg = MessageBox.call(HWND.call, EasyConv.u2s(text1), EasyConv.u2s(text2), 1)
  57.         if msg == 1
  58.           old_exit(*arg)
  59.         end
  60.       rescue
  61.         old_exit(*arg)
  62.       end
  63.     end
  64.   end
  65. end
复制代码
以前写过这样的东西,其实还蛮好使的~
失落的乐章 发表于 2012-10-14 12:18:30
如果单纯捕捉的话需要使用Win32API,如果只是要在退出前执行某些操作的话就重定义Kernel模块的exit方法
晴兰 发表于 2012-10-13 23:01:42
提示: 作者被禁止或删除 内容自动屏蔽
伊吹西瓜 发表于 2012-10-13 19:23:38
Wind2010 发表于 2012-10-13 18:22
不过这样的话是任何退出动作都会触发

那么是不是可以做个如果按下关闭就弹出一些确定存档的信息吗?

点评

_(:3」∠)_参考BAS  发表于 2012-10-13 19:25
Wind2010 发表于 2012-10-13 18:22:37
本帖最后由 Wind2010 于 2012-10-13 18:26 编辑
  1. alias special_exit exit unless defined?("special_exit")
  2. def exit(num)
  3.   #
  4.   special_exit(num)
  5. end
复制代码
不过这样的话是任何退出动作都会触发
伊吹西瓜 发表于 2012-10-13 18:20:18
那个是软件的设定来的吧。好像与ruby那些无关的。。。
拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

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

GMT+8, 2025-9-12 00:01

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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