I did a test and able to make button with icon using Cheat Engine Lua script + winapi. But I fail to find how to handle the events for the buttons created using winapi. Below is the code I am used via Cheat Engine Lua scripting including winapi functions called using "executeCodeLocal":
function GetModuleHandle(lpModuleName)
return executeCodeLocal("kernel32.GetModuleHandleA", lpModuleName)
end
function CreateWindow(oControl, oClass, oName, oHwnd, oDWStyle, nleft, ntop, nwidth, nheight)
return executeCodeLocalEx("user32.CreateWindowExA", 0, oClass, oName, oDWStyle, nleft, ntop, nwidth, nheight, oHwnd, 0, 0, 0)
end
function LoadIcon(hInstance, lpIconName)
return executeCodeLocalEx("user32.LoadIconA", lpIconName)
end
function _SendMessage(hWnd, Msg, wParam, lParam)
return executeCodeLocalEx("user32.SendMessageA",hWnd, Msg, wParam, lParam)
end
function LoadImage(hInst, lpszName, uType, cxDesired, cyDesired, fuLoad)
return executeCodeLocalEx("user32.LoadImageA", hInst, lpszName, uType, cxDesired, cyDesired, fuLoad)
end
function SetWindowLong(hWnd, nIndex, dwNewLong)
return executeCodeLocalEx("user32.SetWindowLongPtrA", hWnd, nIndex, dwNewLong)
end
function GetWindowLong(prmlngWindowHandle, prmlngIndex)
return executeCodeLocalEx("user32.GetWindowLongA", prmlngWindowHandle, prmlngIndex)
end
function CallWindowProc(hWnd, Msg, wParam, lParam)
return executeCodeLocalEx("user32.CallWindowProcA", lpPrevWndFunc, hWnd, Msg, wParam, lParam)
end
function RegisterClass(lpwcx)
return executeCodeLocalEx("user32.RegisterClassW", lpwcx)
end
function SetWindowLongW(hWnd, nIndex, dwCallBack)
return executeCodeLocalEx("user32.SetWindowLongW", hWnd, nIndex, dwCallBack)
end
function CallWindowProcW(wndproc, hWnd, Msg, wParam, lParam)
return executeCodeLocalEx("user32.CallWindowProcW", wndproc, hWnd, Msg, wParam, lParam)
end
--Window Style
WS_VISIBLE = 0x10000000
WS_BORDER = 0x00800000
WS_CHILD = 0x40000000
-- Window Message
WM_LBUTTONUP = 0x0202
WM_LBUTTONDOWN = 0x020
WM_COMMAND = 0x111
-- Button Style
BS_TEXT = 0x00000000
BS_BOTTOM = 0x00000800
BS_TOP = 0x00000400
BS_LEFT = 0x00000100
BS_RIGHT = 0x00000200
BM_SETIMAGE = 0xF7
BS_BITMAP = 0x00000080
BS_DEFPUSHBUTTON = 0x00000001
GWL_WNDPROC = -4
IMAGE_BITMAP = 0
IMAGE_ICON = 1
LR_LOADFROMFILE = 16
LR_DEFAULTCOLOR = 0x00000000
LR_CREATEDIBSECTION = 8192
LR_DEFAULTSIZE = 64
--------------------------------------------------------------------------------
if f then f.destroy() end
f = createForm()
f.Height = 70
f.Width = 230
f.Position = 'poScreenCenter'
f.Caption = 'Test Winapi'
f.Show()
open = 'd:\\open.bmp'
save = 'd:\\save.bmp'
-- Create Open Button
hInstance = GetModuleHandle(nil)
hButton = CreateWindow(0, "Button", "Open", f.Handle, WS_VISIBLE + WS_BORDER + WS_CHILD | BS_TEXT | BS_BOTTOM,
10, 10, 100, 50, 10000, hInstance, 0)
hBitmap = LoadImage(hInstance, open, IMAGE_BITMAP, 30, 30, LR_LOADFROMFILE | LR_DEFAULTCOLOR)
_SendMessage(hButton, BM_SETIMAGE, IMAGE_BITMAP, hBitmap)
-- Create Save Button
hInstance2 = GetModuleHandle(nil)
hButton2 = CreateWindow(0, "Button", "Save", f.Handle, WS_VISIBLE + WS_BORDER + WS_CHILD | BS_TEXT | BS_TOP,
120, 10, 100, 50, 0, hInstance2, 0)
hBitmap2 = LoadImage(hInstance2, save, IMAGE_BITMAP, 25, 25, LR_LOADFROMFILE | LR_DEFAULTCOLOR)
_SendMessage(hButton2, BM_SETIMAGE, IMAGE_BITMAP, hBitmap2)
But, how to handle click event on the created buttons?. If I am use, example:
function hButton_onClick()
showMessage('Button open has clicked')
end
hButton.OnClick = hButton_onClick
The function return error because hButton is a nil value. How to solve the issue?