For this I have written the below code
Are you sure about that? Looks a lot like the code I wrote in a answer here.
You failed to properly read the MUI documentation and you failed to properly adapt the example code you found.
MyLicenseShowCallback
is never executed because you used the wrong define, the correct name is MUI_PAGE_CUSTOMFUNCTION_SHOW
.
The other problem is that the original code placed the new button next to the Cancel button in the classic NSIS UI but the Cancel button is in a different place in MUI and the button ends up off-screen.
This code just places the button on the left side of the UI
Var PrintBtn
!include MUI2.nsh
!include LogicLib.nsh
!define MUI_PAGE_CUSTOMFUNCTION_SHOW HidePrintButton
!insertmacro MUI_PAGE_WELCOME
!define MUI_PAGE_CUSTOMFUNCTION_SHOW MyLicenseShowCallback
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE HidePrintButton
!insertmacro MUI_PAGE_LICENSE "${__FILE__}"
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
Function HidePrintButton
${IfThen} $PrintBtn P<> 0 ${|} SendMessage $PrintBtn ${WM_CLOSE} 0 0 ${|}
FunctionEnd
Function MyLicenseShowCallback
GetDlgItem $0 $hwndparent 2 ; Find cancel button so we can copy its size and y position.
System::Call *(i,i,i,i)p.r1
System::Call 'USER32::GetWindowRect(pr0,pr1)'
System::Call *$1(i.r2,i.r3,i.r4,i.r5)
IntOp $5 $5 - $3 ;height
IntOp $4 $4 - $2 ;width
System::Call 'USER32::ScreenToClient(p$hwndparent,pr1)'
System::Call *$1(i.r2,i.r3)
System::Free $1
IntOp $2 $4 / 5 ; Calculate x padding based on the width but you can put any value you want in $2
System::Call 'USER32::CreateWindowEx(i 0,t "Button",t "Print",i ${WS_CHILD}|${WS_VISIBLE}|${WS_TABSTOP},i r2,i r3,i r4,i r5,p $hwndparent,p 0x666,p 0,p 0)p.r0'
StrCpy $PrintBtn $0
SendMessage $hwndparent ${WM_GETFONT} 0 0 $1
SendMessage $0 ${WM_SETFONT} $1 1
GetFunctionAddress $0 onmybtnclick
ButtonEvent::AddEventHandler 0x666 $0
FunctionEnd
Function onmybtnclick
MessageBox mb_ok "You clicked me!"
FunctionEnd
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…