查看: 2252|回复: 0

[原创发布] win32 RGSSBitmap的“属性应用”

[复制链接]
梦石
0
星屑
6483
在线时间
119 小时
注册时间
2020-1-8
回帖
205
发表于 2022-2-3 23:48:16 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
参考资料:RPG Maker的Bitmap类的结构
                 还有一个英文网站,我找不到了……

  1. module CWP
  2.   CallWndProc = Win32API.new('user32', 'CallWindowProc', 'PLLLL', 'L')
  3.   CodeMove = [139,68,36,4,133,192,116,23,209,224,185,2,0,0,0,139,20,140,133,
  4.     210,116,9,139,4,16,65,131,249,5,117,240,194,16,0].pack('C*')

  5.   def self.move(obj, offsetA=16, offsetB=0, offsetC=0)
  6.     CallWndProc.call(CodeMove, obj, offsetA, offsetB, offsetC)
  7.   end
  8. end

  9. class Bitmap
  10.   def to_int
  11.     self.object_id
  12.   end
  13.   
  14.   def hBitmap
  15.     @_hBitmap ||= CWP.move(self, 16, 8, 44)
  16.   end
  17.   
  18.   def pBits
  19.     @_pBits ||= CWP.move(self, 16, 8, 16)
  20.   end
  21.   
  22.   def pScan0
  23.     @_pScan0 ||= CWP.move(self, 16, 8, 12)
  24.   end
  25.   
  26.   def pIHr
  27.     @_pIHr ||= CWP.move(self, 16, 8, 8) # 或者 CWP.move(self, 16, 8, 32)
  28.   end
  29. end

  30. module WIN32API
  31.   @@default_w = 640
  32.   @@default_h = 480
  33.   
  34.   threadID = Win32API.new('kernel32', 'GetCurrentThreadId', 'V', 'L').call()
  35.   hwnd = Win32API.new('user32', 'GetForegroundWindow', 'V', 'L').call()
  36.   _GetWndTidPid = Win32API.new('user32','GetWindowThreadProcessId', 'LP', 'L')
  37.   _FindWindowEx = Win32API.new('user32', 'FindWindowEx', 'LLPP', 'L')
  38.   while _GetWndTidPid.call(hwnd, nil) != threadID
  39.     hwnd = _FindWindowEx.call(0, hwnd, "RGSS Player", nil)
  40.   end
  41.   Hwnd = hwnd
  42.   
  43.   GetDC = Win32API.new('user32', 'GetDC', 'L', 'L')
  44.   ReleaseDC = Win32API.new('user32', 'ReleaseDC', 'LL', 'L')
  45.   CreateCompatibleDC = Win32API.new('gdi32', 'CreateCompatibleDC', 'L', 'L')
  46.   DeleteDC = Win32API.new('gdi32', 'DeleteDC', 'L', 'I')
  47.   DeleteObject = Win32API.new('gdi32', 'DeleteObject', 'L', 'I')
  48.   SelectObject = Win32API.new('gdi32', 'SelectObject', 'LL', 'L')
  49.   BitBlt = Win32API.new('gdi32', 'BitBlt', 'LIIIILIIL', 'I')
  50.   StretchBlt = Win32API.new('gdi32', 'StretchBlt', 'LIIIILIIIIL', 'I')  
  51.   SetStretchBltMode = Win32API.new('gdi32', 'SetStretchBltMode', 'LI', 'I')
  52.   SetBrushOrgEx = Win32API.new('gdi32', 'SetBrushOrgEx', 'LIIP', 'I')
  53.   
  54.   SRCCOPY = 0xCC0020
  55.   SRCPAINT = 0xEE0086
  56.   STRETCH_HALFTONE = 4
  57.   
  58.   def self.snap_to_bitmap(src_x = 0, src_y = 0, src_w = @@default_w,
  59.       src_h = @@default_h, dest_w = @@default_w, dest_h = @@default_h)
  60.       
  61.     bitmap = Bitmap.new(dest_w, dest_h)
  62.     hDC = GetDC.call(Hwnd)
  63.     mCDC = CreateCompatibleDC.call(hDC)
  64.     oldBitmap = SelectObject.call(mCDC, bitmap.hBitmap)
  65.    
  66.     if src_w == dest_w && src_h == dest_h
  67.       BitBlt.call(mCDC, 0, 0, dest_w, dest_h, hDC, 0, 0, SRCCOPY)
  68.     else
  69.       SetStretchBltMode.call(mCDC, STRETCH_HALFTONE)
  70.       SetBrushOrgEx.call(mCDC, 0, 0, nil)
  71.       StretchBlt.call(mCDC, 0, 0, dest_w, dest_h, hDC, src_x, src_y, src_w,
  72.         src_h, SRCPAINT)
  73.     end
  74.    
  75.     SelectObject.call(mCDC, oldBitmap)
  76.     DeleteDC.call(mCDC)
  77.     ReleaseDC.call(Hwnd, hDC)
  78.    
  79.     bitmap
  80.   end
  81.   
  82.   MultiByteToWideChar =
  83.     Win32API.new('kernel32', 'MultiByteToWideChar', 'ILPIPI', 'I')   
  84.   CP_UTF8 = 65001
  85.   
  86.   def self.utf16(text)
  87.     len = MultiByteToWideChar.call(CP_UTF8, 0, text, -1, nil, 0)
  88.     buf = "\0" * (len * 2)
  89.     MultiByteToWideChar.call(CP_UTF8, 0, text, -1, buf, len)
  90.     buf
  91.   end
  92.    
  93.   GdiplusStartup = Win32API.new('gdiplus', 'GdiplusStartup', 'PPP', 'I')
  94.   GdiplusShutdown = Win32API.new('gdiplus', 'GdiplusShutdown', 'L', 'V')
  95.   GdipStartupInput = [1, 0, 0, 0].pack('L4')
  96.   
  97.   GdipCreateBitmapFromGdiDib =
  98.     Win32API.new('gdiplus', 'GdipCreateBitmapFromGdiDib', 'LLP', 'I')
  99.   GdipDisposeImage = Win32API.new('gdiplus', 'GdipDisposeImage', 'L', 'I')
  100.   GdipSaveImageToFile =
  101.     Win32API.new('gdiplus', 'GdipSaveImageToFile', 'LPPP', 'I')
  102.   
  103.   fixed = [6660, 4563, 154, 115, 0, 0, 248, 30, 243, 46].pack('SSC8')
  104.   ClsidEncoder = {
  105.     :bmp => [1434252288].pack('L') << fixed,
  106.     :jpg => [1434252289].pack('L') << fixed,
  107.     :png => [1434252294].pack('L') << fixed
  108.     }
  109.    
  110.   def self.save_bitmap_to_pic(rgss_bitmap, filename, type=:png)
  111.     buf = "\0" * 4
  112.     GdiplusStartup.call(buf, GdipStartupInput, nil)
  113.     token = buf.unpack('L')[0]
  114.    
  115.     GdipCreateBitmapFromGdiDib.call(rgss_bitmap.pIHr, rgss_bitmap.pBits, buf)      
  116.     pBitmap = buf.unpack('L')[0]   
  117.     GdipSaveImageToFile.call(pBitmap, self.utf16(filename),
  118.       ClsidEncoder[type] || ClsidEncoder[:png], nil)
  119.    
  120.     GdipDisposeImage.call(pBitmap)
  121.     GdiplusShutdown.call(token)
  122.   end
  123. end
复制代码


以前只会用论坛上的Bitmap#address方法,并不知道其含义,具体可以看看链接中的解释(代码需鼠标手动拉开……)。

全部为Win32API的调用,主要为基本过程,并没有对方法的参数及API返回值做判断,
具体参数类型及取值参照变量名,有需求请自行修改。

WIN32API.snap_to_bitmap         
  由gdi实现,截屏为位图的功能,可局部缩放。
  用到 Bitmap#hBitmap(完全可以把RGSSBitmap当作GDI的Bitmap对象使用)
  示例:以屏幕左上角 Rect(0, 0, 320, 240) 区域图像生成 640×480 大小的位图对象
            bitmap = WIN32API.snap_to_bitmap(0, 0, 320, 240, 640, 480)
            #bitmap.dispose

WIN32API.save_bitmap_to_pic   
  由gdi+实现,保存位图为图片(png,jpg,bmp)。
  用到 Bitmap#pIHr(GDI中的BITMAPINFOHEADER); Bitmap#pBits(RGSSBitmap中图像数据内存的起始地址)
  示例:以bitmap对象保存为名为 “截屏.jpeg” 的图片文件
            # bitmap
            WIN32API.save_bitmap_to_pic(bitmap, "截屏.jpeg", :jpg)
            # bitmap.dispose
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部