Skip to content
⌘ NSIS Forum Archive

Question: Center Align DropList

3 posts

nashtor#

Question: Center Align DropList

Hello there,

I am trying to align the strings listed inside a droplist to the horizontal center of said droplist.
Having quite a hard time trying to do so.
${AddStyle} or ${AddExStyle} in combination with ${ES_CENTER} or ${SS_CENTER} does not seem to work for me.
Google brings up html, c#, etc. but nothing what would directly help me or maybe I am just blind.

Does anyone here know of a workaround, besides trying to add spaces to the strings to center them manually, which opens another can of problems for me 🙁 ?

Thank you for your time!

!include "MUI2.nsh"
!include "nsDialogs.nsh"
!define PRODUCTNAME "DropListAlignCenter"
Name "${PRODUCTNAME}"
OutFile "${PRODUCTNAME}.exe"
RequestExecutionLevel user
Var DropList
!insertmacro MUI_LANGUAGE "English"
Page custom nsDialogsMainPage
Section ""
SectionEnd
Function nsDialogsMainPage
    
    nsDialogs::Create 1044
    Pop $0
    ${If} $0 == error
        Abort
    ${EndIf}
    
    ${NSD_CreateDropList} 100u 50u 120u 200u ""
    Pop $DropList
    
    ${NSD_CB_AddString} $DropList "Alice"
    ${NSD_CB_AddString} $DropList "Franklin"
    ${NSD_CB_AddString} $DropList "set"
    ${NSD_CB_AddString} $DropList "v0.09"
    ${NSD_CB_AddString} $DropList "Apple Computer, Inc."
    ${NSD_CB_AddString} $DropList "Microsoft Corporation"
    
    nsDialogs::Show
    
FunctionEnd 
Anders#
You can't just mix and match random styles. Combobox styles are CBS_* and there is no style for alignment. The only way to make a droplist center its text would be to use the CBS_OWNERDRAWFIXED style and custom draw. This can only be done with a custom plug-in.

You can also fake it with a ComboBox by playing with its edit control:
Page Custom custpagecr
!include nsDialogs.nsh
Function custpagecr
nsDialogs::Create 1018
Pop $0
${NSD_CreateComboBox} 0 13u 100% -13u ""
Pop $0
${NSD_CB_AppendString} $0 "Hello"
${NSD_CB_AppendString} $0 "World"
${NSD_CB_SetSelectionIndex} $0 0
FindWindow $1 "EDIT" "" $0
${NSD_AddStyle} $1 ${ES_CENTER}|${ES_READONLY}
${NSD_Edit_SetReadOnly} $1 1
nsDialogs::Show
FunctionEnd