Get text dimensions: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
 
Line 8: Line 8:
Pop $R0
Pop $R0


; Get the text dimensions suitable for this dialog into $R1 and $R2.
; Use this font.
${GetTextExtent} $R0 `My string of text` $R1 $R2
CreateFont $R1 $(^Font) 9 600
 
; Get the text dimensions suitable for this font into $R1 and $R2.
${GetTextExtent} $R1 `My string of text` $R2 $R3


; Create the label with the exact dimensions required.
; Create the label with the exact dimensions required.
${NSD_CreateLabel} 10u 10u $R1u $R2u `My string of text`
${NSD_CreateLabel} 10u 10u $R2u $R3u `My string of text`
Pop $R1
Pop $R2
SetCtlColors $R1 0x000000 0xCCCCCC
SetCtlColors $R2 0x000000 0xCCCCCC
SendMessage $R2 ${WM_SETFONT} $R1 1


nsDialogs::Show</highlight-nsis>
nsDialogs::Show</highlight-nsis>

Latest revision as of 19:37, 30 March 2013

Author: Afrow UK (talk, contrib)


Description

Gets the dimensions of a string of text for a given window according to that window's font. The width and height are returned in dialog units suitable for use with nsDialogs. For example you can get the dimensions of a string before creating a label for it. This was created for inserting in-line clickable links in a line of text.

Usage

nsDialogs::Create 1018
Pop $R0
 
; Use this font.
CreateFont $R1 $(^Font) 9 600
 
; Get the text dimensions suitable for this font into $R1 and $R2.
${GetTextExtent} $R1 `My string of text` $R2 $R3
 
; Create the label with the exact dimensions required.
${NSD_CreateLabel} 10u 10u $R2u $R3u `My string of text`
Pop $R2
SetCtlColors $R2 0x000000 0xCCCCCC
SendMessage $R2 ${WM_SETFONT} $R1 1
 
nsDialogs::Show

Code

Save the code below into GetTextExtent.nsh and then !include it in your scripts.

!ifndef __GetTextExtent_NSH__
!define __GetTextExtent_NSH__
 
!include Util.nsh
 
!macro _GetTextExtent Font Text XVar YVar
  Push `${Text}`
  Push `${Font}`
  ${CallArtificialFunction} __GetTextExtent
  Pop ${YVar}
  Pop ${XVar}
!macroend
!define GetTextExtent `!insertmacro _GetTextExtent`
 
!macro __GetTextExtent
 
  Exch $R0
  Exch
  Exch $R1
  Push $R2
  Push $R3
  Push $R4
  Push $R5
  Push $R6
 
  System::Call `user32::GetDC(i $HWNDPARENT) i.R6`
  System::Call `gdi32::SelectObject(i R6, i R0) i.R0`
 
  StrLen $R3 $R1
  System::Call `*(i 0, i 0) i.R2`
  System::Call `gdi32::GetTextExtentPoint32(i R6, t R1, i R3, i R2)`
  System::Call `*$R2(i.R4, i.R5)`
  System::Free $R2
 
  System::Call `*(i 0, i 0, i 4, i 8) i.R2`
  System::Call `user32::MapDialogRect(i $HWNDPARENT, i R2)`
  System::Call `*$R2(i, i, i.R1, i.R3)`
  System::Free $R2
 
  System::Call `gdi32::SelectObject(i R6, i R0)`
  System::Call `kernel32::MulDiv(i R5, i 8, i R3) i.s`
  System::Call `kernel32::MulDiv(i R4, i 4, i R1) i.s`
 
  Exch 8
  Pop $R0
  Exch 6
  Pop $R1
  Pop $R6
  Pop $R5
  Pop $R4
  Pop $R3
  Pop $R2
 
!macroend
 
!endif