WndSubclass plug-in: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(New page: {{PageAuthor|Anders}} Category:Plugins ==Download== [http://stashbox.org/358546/WndSubclass%20v0.0.3.zip v0.0.3] ==Info== This plug-in will let you subclass any window in the NSIS pr...)
 
(added an example)
Line 7: Line 7:
==Info==
==Info==
This plug-in will let you subclass any window in the NSIS process (Not thread safe ATM)
This plug-in will let you subclass any window in the NSIS process (Not thread safe ATM)
==Examples==
The following example mimics the behavior of EM_SETCUEBANNER, but can be used on older windows versions, multi-line textboxes and Windows XP with asian language support without problems.
The code essentially displays an Edit (text) field with custom colors (grey on white) and the string "Password".  When the user sets focus to this field (via mouse or keyboard), and its content is "Password", its content is cleared and the control colors set to another set of custom colors (black on white).
When the user removes focus from this field (via mouse or keyboard), and its content is empty, the 'Password' text and grey on white colors are restored.  If the field is not empty, the string the user has entered remains.
<highlight-nsis>
!addplugindir "."
!addplugindir ".\release\"
!addincludedir "."
!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
!include "MUI2.nsh"
!include "WndSubclass.nsh"
OutFile "test.exe"
var dialog
Page Custom CustomPage
var NSD_TextField
var NSD_Label
var TextFieldSubProc
Function CustomPage
nsDialogs::Create 1018
Pop $dialog
${NSD_CreateText} 0 0 100% 8% "Password"
    Pop $NSD_TextField
SetCtlColors $NSD_TextField 0x888888 0xffffff
    ${WndSubclass_Subclass} $NSD_TextField TextFieldSubProc $TextFieldSubProc $TextFieldSubProc
${NSD_CreateLabel} 0 10% 100% 8% ""
    Pop $NSD_Label
nsDialogs::Show
FunctionEnd
Function TextFieldSubProc
${If} $2 = ${WM_SETFOCUS}
${NSD_GetText} $NSD_TextField $R0
${If} $R0 == "Password"
${NSD_SetText} $NSD_TextField ""
SetCtlColors $NSD_TextField 0x000000 0xffffff
${EndIf}
${ElseIf} $2 = ${WM_KILLFOCUS}
${NSD_GetText} $NSD_TextField $R0
${If} $R0 == ""
${NSD_SetText} $NSD_TextField "Password"
SetCtlColors $NSD_TextField 0x888888 0xffffff
${EndIf}
${EndIf}
FunctionEnd
Section
SectionEnd
!insertmacro MUI_LANGUAGE "English"
</highlight-nsis>

Revision as of 07:27, 10 April 2009

Author: Anders (talk, contrib)


Download

v0.0.3

Info

This plug-in will let you subclass any window in the NSIS process (Not thread safe ATM)

Examples

The following example mimics the behavior of EM_SETCUEBANNER, but can be used on older windows versions, multi-line textboxes and Windows XP with asian language support without problems.

The code essentially displays an Edit (text) field with custom colors (grey on white) and the string "Password". When the user sets focus to this field (via mouse or keyboard), and its content is "Password", its content is cleared and the control colors set to another set of custom colors (black on white). When the user removes focus from this field (via mouse or keyboard), and its content is empty, the 'Password' text and grey on white colors are restored. If the field is not empty, the string the user has entered remains.

!addplugindir "."
!addplugindir ".\release\"
!addincludedir "."
 
!include "nsDialogs.nsh"
!include "winmessages.nsh"
!include "logiclib.nsh"
 
!include "MUI2.nsh"
 
!include "WndSubclass.nsh"
 
OutFile "test.exe"
 
var dialog
 
Page Custom CustomPage
 
var NSD_TextField
var NSD_Label
var TextFieldSubProc
 
Function CustomPage
	nsDialogs::Create 1018
	Pop $dialog
 
	${NSD_CreateText} 0 0 100% 8% "Password"
	    Pop $NSD_TextField
		SetCtlColors $NSD_TextField 0x888888 0xffffff
	    ${WndSubclass_Subclass} $NSD_TextField TextFieldSubProc $TextFieldSubProc $TextFieldSubProc
 
	${NSD_CreateLabel} 0 10% 100% 8% ""
	    Pop $NSD_Label
 
	nsDialogs::Show
FunctionEnd
 
Function TextFieldSubProc
	${If} $2 = ${WM_SETFOCUS}
		${NSD_GetText} $NSD_TextField $R0
		${If} $R0 == "Password"
			${NSD_SetText} $NSD_TextField ""
			SetCtlColors $NSD_TextField 0x000000 0xffffff
		${EndIf}
	${ElseIf} $2 = ${WM_KILLFOCUS}
		${NSD_GetText} $NSD_TextField $R0
		${If} $R0 == ""
			${NSD_SetText} $NSD_TextField "Password"
			SetCtlColors $NSD_TextField 0x888888 0xffffff
		${EndIf}
	${EndIf}
FunctionEnd
 
Section
SectionEnd
 
!insertmacro MUI_LANGUAGE "English"