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...") |
(→Usage) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 8: | Line 8: | ||
Pop $R0 | Pop $R0 | ||
; Get the text dimensions suitable for this | ; Use this font. | ||
${GetTextExtent} $ | 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 $ | ${NSD_CreateLabel} 10u 10u $R2u $R3u `My string of text` | ||
Pop $ | Pop $R2 | ||
SetCtlColors $ | SetCtlColors $R2 0x000000 0xCCCCCC | ||
SendMessage $R2 ${WM_SETFONT} $R1 1 | |||
nsDialogs::Show</highlight-nsis> | nsDialogs::Show</highlight-nsis> | ||
Line 20: | Line 24: | ||
== 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 | <highlight-nsis>!ifndef __GetTextExtent_NSH__ | ||
!define | !define __GetTextExtent_NSH__ | ||
!include Util.nsh | !include Util.nsh | ||
!macro _GetTextExtent | !macro _GetTextExtent Font Text XVar YVar | ||
Push `${Text}` | Push `${Text}` | ||
Push `${ | Push `${Font}` | ||
${CallArtificialFunction} __GetTextExtent | ${CallArtificialFunction} __GetTextExtent | ||
Pop ${YVar} | Pop ${YVar} | ||
Line 43: | Line 47: | ||
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 | 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 | System::Call `*(i 0, i 0, i 4, i 8) i.R2` | ||
System::Call ` | System::Call `user32::MapDialogRect(i $HWNDPARENT, i R2)` | ||
System::Call `*$ | System::Call `*$R2(i, i, i.R1, i.R3)` | ||
System::Free $R2 | System::Free $R2 | ||
System::Call `kernel32::MulDiv(i R5, i | System::Call `gdi32::SelectObject(i R6, i R0)` | ||
System::Call `kernel32::MulDiv(i R4, i | 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 | Exch 8 | ||
Pop $R0 | Pop $R0 | ||
Exch | Exch 6 | ||
Pop $R1 | Pop $R1 | ||
Pop $R6 | |||
Pop $R5 | Pop $R5 | ||
Pop $R4 | Pop $R4 |
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