How to set character spacing of button?

Hello, I am using ahk script which used gdip. Many of example of gdip on MSDN are for C++ hence I come to ask you if you know the function which I should use to format the button. Here is the source code which uses dllcall to call the gdip function
http://paste.ofcode.org/cbFd48spUSjiBnRzvwhBWW

I see, there are commands
1
2
DllCall("Gdiplus.dll\GdipSetStringFormatAlign", "Ptr", PFORMAT, "Int", HALIGN)
DllCall("Gdiplus.dll\GdipSetStringFormatLineAlign", "Ptr", PFORMAT, "Int", VALIGN)

which "sets format of horizontal and vertical alignment to the button". However, because the text is Arial(?) Bold it the characters are too close and seems weird. I'd like to correct this. I believe there is only necessary to set FORMAT object using pointer PFORMAT, but cannot find function for it.
It seems easier to do this entirely in C++...

Ok, on line 55 you get the font. Since you don't want to use that font you need to create your own. Use GdipCreateFontFromLogfont() for this.

See:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms533978%28v=vs.85%29.aspx
They write I need logfont pointer to LOGFONTW structure. How to create the structure?
Yes, the structure would be a problem, but there's an alternative in the gdi32.dll: CreateFont()

See:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd183499%28v=vs.85%29.aspx

I have found ahk library which creates font:
http://maul-esel.github.io/FormsFramework/files/Font/Font-ahk.html
code:
http://mm-autohotkey.googlecode.com/svn-history/r807/trunk/Form/inc/Font.ahk
But I am not sure how to use it. Missing examples. Hopefully I will find author or somebody to help me with it.
I try the second function, seems easier. I tried to add some values, but I am not sure if they right. What should I set if I want e.g. Arial Bold 10px height and have slightly spacing between characters?

my try
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Height:=10
Width:=4
Escapement:=2
Orientation:=0
fnWeight:="FW_BOLD"
fdwItalic:=0
fdwUnderline:=0
fdwStrikeOut:=0
fdwCharSet:=0
fdwOutputPrecision:=0
fdwClipPrecision:=0
fdwQuality:=100
fdwPitchAndFamily:=0
lpszFace:="Arial"

pFont := DllCall("Gdi32.dll\CreateFont", "Ptr", Height, "int", 
Width, "int",
Escapement, "int",
Orientation, "int",
fnWeight, "int",
fdwItalic, "DWORD",
fdwUnderline, "DWORD",
fdwStrikeOut, "DWORD",
fdwCharSet, "DWORD",
fdwOutputPrecision, "DWORD",
fdwClipPrecision, "DWORD",
fdwQuality, "DWORD",
fdwPitchAndFamily, "DWORD",
lpszFace, "LPCTSTR")
Last edited on
For spacing use SetTextCharacterExtra():

http://msdn.microsoft.com/en-us/library/windows/desktop/dd145092%28v=vs.85%29.aspx

Apart from that you need to experiment with the values. I'd say that you leave Width and Escapement 0 until you know what you're doing
This works so far, without spacing:
(weight no effect, can be 0)

I need to do spacing and bold font yet. Why Weight dont works?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
height:=10
weight:=10
italic:=0
strikeOut:=0
underline:=0
nCharSet:=0
Font2:="Arial"
RegRead, LogPixels, HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontDPI, LogPixels
height := -DllCall("MulDiv", "int", Height, "int", LogPixels, "int", 72)
HFONT  := DllCall("CreateFont", "int",  height, "int",  0, "int",  0, "int", 0
,"int",  weight,   "Uint", italic,   "Uint" 
,underline,"uint"
,strikeOut, "Uint"
,nCharSet, "Uint", 0, "Uint", 0, "Uint", 0, "Uint", 0, "str" 
,Font2, "Uint")
Last edited on
I have this code:

http://paste.ofcode.org/myN4TCg6ewmMacwAiipYzD

on line 22 I have

 
DllCall("Gdiplus.dll\SetTextCharacterExtra","Ptr",PGRAPHICS,3,"int")


But it does not effect the spacing. I changed the number from 0 to 50 but nothing happens. Any idea?

And how to set bold font?
Last edited on
And how to set bold font?
Take a look at the documentation: The weight value for bold is 700

on line 22 I have
Yes, but

DllCall("Gdiplus.dll\SetTextCharacterExtra","Ptr",PGRAPHICS,3,"int")

Isn't that the wrong order?
This order is OK in AHK. BUt I must ask on AHK if the command is OK. But I doubt anybody will help me there. There is hard to find people interested in C++.

Thanks for the bold text correction. Is there something wrong with font color format? I had dark red text and it changed to pink.

Here are next three lines added to the code I linked last time:
1
2
3
NumPut(4, RECT, 0, "Float"), NumPut(2, RECT, 4, "Float")
NumPut(W - 8, RECT, 8, "Float"), NumPut(H - 4, RECT, 12, "Float")
DllCall("Gdiplus.dll\GdipCreateSolidFill", "UInt", "0xFF" . TxtColor, "PtrP", PBRUSH)


The TextColor is set to dark red.

The original hFont is this:
 
HFONT := DllCall("User32.dll\SendMessage", "Ptr", HWND, "UInt", WM_GETFONT, "Ptr", 0, "Ptr", 0, "Ptr")


There must be something what overwritten the color or changes the mode of the font appearance....

Also I would like to add here function to draw rectangle as background for the button (to change the button color). What function to look for?

Thanks
Oh, you were right. I thought the order is variable, "int", but it is not... Corrected. Bold text now. The problem with pink text color stays. Possibly background color for button too.
To change the text color use SetTextColor():

http://msdn.microsoft.com/en-us/library/windows/desktop/dd145093%28v=vs.85%29.aspx


To change the background of the text use SetBkColor():

http://msdn.microsoft.com/en-us/library/windows/desktop/dd162964%28v=vs.85%29.aspx

See also SetBkMode():

http://msdn.microsoft.com/en-us/library/windows/desktop/dd162965%28v=vs.85%29.aspx

This determines whether the text is drawn OPAQUE or TRANSPARENT



The background of the button might be a problem since the button could be created with a background.
Otherwise you can use a function like FillRect() to draw the background:

http://msdn.microsoft.com/en-us/library/windows/desktop/dd162719%28v=vs.85%29.aspx
The problem is not that I can not change color. The problem is that the color is incorrect value after I used new definition of HFONT. Why? I can change the color in input parameters but the resulting color will not be that one which I entered in the input parameter.

1
2
TxtColor:="CE0333" ; dark red
DllCall("Gdiplus.dll\GdipCreateSolidFill", "UInt", "0xFF" . TxtColor, "PtrP", PBRUSH)


Result: Pink

I wanted to prefer this fnc because it works with HEX format
Last edited on
I'd guess that GdipCreateSolidFill isn't the problem, but later when you use it?
Do not mix gdi+ and gdi32 functions.

you can use CreateSolidBrush() to use it with FillRect()

http://msdn.microsoft.com/en-us/library/aa931351.aspx

No alpha channel but I guess you don't need it
I'm still not skilled to get own solution. But in AHK somebody bring script to solve the problem in AHK.
Topic archived. No new replies allowed.