Get text dimensions: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Created page with "{{PageAuthor|Afrow UK}} == 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 dia...")
 
Line 20: Line 20:
== Code ==
== Code ==
Save the code below into GetTextExtent.nsh and then !include it in your scripts.
Save the code below into GetTextExtent.nsh and then !include it in your scripts.
<highlight-nsis>!ifndef __GetTextExtend_NSH__
<highlight-nsis>!ifndef __GetTextExtent_NSH__
!define __GetTextExtend_NSH__
!define __GetTextExtent_NSH__


!include Util.nsh
!include Util.nsh

Revision as of 17:29, 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 HWND Text XVar YVar
  Push `${Text}`
  Push `${HWND}`
  ${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
 
  System::Call `user32::GetDC(i R0) i.R0`
 
  StrLen $R3 $R1
  System::Call `*(i 0, i 0) i.R2`
  System::Call `gdi32::GetTextExtentPoint32(i R0, t R1, i R3, i R2)`
  System::Call `*$R2(i.R4, i.R5)`
 
  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 `gdi32::GetTextMetrics(i R0, i R3)`
  System::Call `*$R3(i.R1)`
  System::Free $R3
 
  System::Call `gdi32::GetTextExtentPoint32(i R0, t 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', i 52, i R2)`
  System::Call `*$R2(i.R3)`
  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 `kernel32::MulDiv(i R4, i 8, i R1) i.s`
 
  Exch 7
  Pop $R0
  Exch 5
  Pop $R1
  Pop $R5
  Pop $R4
  Pop $R3
  Pop $R2
 
!macroend
 
!endif