Get text dimensions: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
Line 25: Line 25:
!include Util.nsh
!include Util.nsh


!macro _GetTextExtent HWND Text XVar YVar
!macro _GetTextExtent Font Text XVar YVar
   Push `${Text}`
   Push `${Text}`
   Push `${HWND}`
   Push `${Font}`
   ${CallArtificialFunction} __GetTextExtent
   ${CallArtificialFunction} __GetTextExtent
   Pop ${YVar}
   Pop ${YVar}
Line 43: Line 43:
   Push $R4
   Push $R4
   Push $R5
   Push $R5
  Push $R6


   System::Call `user32::GetDC(i R0) i.R0`
   System::Call `user32::GetDC(i $HWNDPARENT) i.R6`
  System::Call `gdi32::SelectObject(i R6, i R0) i.R0`


   StrLen $R3 $R1
   StrLen $R3 $R1
   System::Call `*(i 0, i 0) i.R2`
   System::Call `*(i 0, i 0) i.R2`
   System::Call `gdi32::GetTextExtentPoint32(i R0, t R1, i R3, i R2)`
   System::Call `gdi32::GetTextExtentPoint32(i R6, t R1, i R3, i R2)`
   System::Call `*$R2(i.R4, i.R5)`
   System::Call `*$R2(i.R4, i.R5)`
  System::Free $R2


   System::Call `*(i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0, i 0) i.R3`
   System::Call `*(i 0, i 0, i 4, i 8) i.R2`
   System::Call `gdi32::GetTextMetrics(i R0, i R3)`
   System::Call `user32::MapDialogRect(i $HWNDPARENT, i R2)`
   System::Call `*$R3(i.R1)`
   System::Call `*$R2(i, i, i.R1, i.R3)`
  System::Free $R3
 
  System::Call `gdi32::GetTextExtentPoint32(i R0, t 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', i 52, i R2)`
  System::Call `*$R2(i.R3)`
   System::Free $R2
   System::Free $R2
  IntOp $R3 $R3 / 26
  IntOp $R3 $R3 + 1
  IntOp $R3 $R3 / 2


   System::Call `kernel32::MulDiv(i R5, i 4, i R3) i.s`
  System::Call `gdi32::SelectObject(i R6, i R0)`
   System::Call `kernel32::MulDiv(i R4, i 8, i R1) i.s`
   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 7
   Exch 8
   Pop $R0
   Pop $R0
   Exch 5
   Exch 6
   Pop $R1
   Pop $R1
  Pop $R6
   Pop $R5
   Pop $R5
   Pop $R4
   Pop $R4

Revision as of 19:35, 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
 
; Get the text dimensions suitable for this dialog into $R1 and $R2.
${GetTextExtent} $R0 `My string of text` $R1 $R2
 
; Create the label with the exact dimensions required.
${NSD_CreateLabel} 10u 10u $R1u $R2u `My string of text`
Pop $R1
SetCtlColors $R1 0x000000 0xCCCCCC
 
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