It turns out that once WM_CLOSE has been fired, it can't be stopped. :/ But you can still disable the X button when the game is activated. Just put this on top of your script list.
module DisableX
def self.get_window
game_name = "\0" * 256
read_ini = Win32API.new('kernel32', 'GetPrivateProfileStringA', 'pppplp', 'l')
read_ini.call('Game', 'Title', '', game_name, 255, './Game.ini')
game_name += "\0"
find_window = Win32API.new('user32', 'FindWindowA', 'pp', 'L')
return find_window.call('RGSS Player', game_name)
end
window = self.get_window
if window != 0
menu = Win32API.new('user32', 'GetSystemMenu', 'll', 'l').call(window, 0)
if menu != 0
Win32API.new('user32', 'EnableMenuItem', 'lll', 'l').call(menu, 0xF060, 0x1) # SC_CLOSE, MF_BYCOMMAND | MF_GRAYED
end
end
end
@Mason: This is a simple piece of C code that can hook messages. If you compile this as a DLL with Visual Studio and link to user32.lib, this will work.
#define NOMINMAX
#include <windows.h>
#define DLL_EXPORT __declspec(dllexport)
LRESULT CALLBACK CallWndProc(int code, WPARAM wParam, LPARAM lParam)
{
if (code >= 0)
{
PCWPSTRUCT msg = (PCWPSTRUCT)lParam;
if (msg->message == WM_CLOSE) // this would be processing some stuff regarding WM_CLOSE
{
// extra code would go here, maybe something like MessageBoxA(0, "TEST", "HELLO THERE", 0);
}
}
return CallNextHookEx(NULL, code, wParam, lParam);
}
int initialized = 0;
void DLL_EXPORT Initialize(HWND hwnd) // assuming you get HWND from RUBY using FindWindowA() with the above Ruby code (self.get_window)
{
if (!initialized)
{
HINSTANCE hinstance = (HINSTANCE)GetWindowLongW(hwnd, GWL_HINSTANCE);
DWORD threadId = GetCurrentThreadId(); // some hooks can't be thread specific, but this one can
if (hinstance != 0 && threadId != 0 && SetWindowsHookExW(WH_CALLWNDPROC, CallWndProc, hinstance, threadId) != 0)
{
initialized = 1;
}
}
}
You could call the DLL like this from Ruby:
module MySpecialCode
def self.get_window
game_name = "\0" * 256
read_ini = Win32API.new('kernel32', 'GetPrivateProfileStringA', ['p', 'p', 'p', 'p', 'l', 'p'], 'l')
read_ini.call('Game', 'Title', '', game_name, 255, './Game.ini')
game_name += "\0"
find_window = Win32API.new('user32', 'FindWindowA', ['p', 'p'], 'L')
return find_window.call('RGSS Player', game_name)
end
window = self.get_window
if window != 0
Win32API.new('MyDll', 'Initialize', 'l', 'v').call(window)
end
end