News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Button font is bold

Started by tekhead009, June 10, 2005, 02:56:44 PM

Previous topic - Next topic

tekhead009

I've run across a silly issue I can't find any information about. When creating my buttons, the button text is stuck on bold. It's no big deal, but I can't seem to find a way of making their text look normal. Do I have to edit the ButtonClass or something?

hutch--

Use the WM_SETFONT message witrh a valid font handle and you can set whatever font you like to a button. You can use the predefined handles for ANSI or FIXEDSYS but you can also use CreateFont() with true type fonts.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Tedd

I'm not sure it's meant to be like that - I don't think any of mine were bold. The 'default' button is usually outlined darker, but the text remains the same.
Want to post a little example?
No snowflake in an avalanche feels responsible.

tekhead009

This is what it looks like:


This is how I'm doing it:

.ELSEIF uMsg==WM_CREATE
;------------------------
;-Create OpenFile Button
;------------------------
invoke CreateWindowEx,NULL,addr ButtonClass,addr cmdOpenFile,\
WS_CHILD + WS_VISIBLE + BS_PUSHBUTTON,\
(Left_Margin * 1),(Top_Margin),Button_Width,Button_Height,\
hWnd,bidOpenFile,hInstance,NULL
mov hwnd_cmdOpenFile,EAX



I'm trying to figure out how to get a a font handle right now, but setting a font by using the WM_SETFONT message seems tedious. Do I have to call it for every control I create, since my text box controls are also in bold? I was looking for a style, or a way of setting the class. I may be going about creating my objects all wrong, though.

donkey

DATA SECTION
ALIGN 16
SmallFont LOGFONT <-11,0,0,0,FW_NORMAL,FALSE,FALSE,0,0,0,0,0,0,"Arial">

CODE SECTION
invoke CreateFontIndirect,OFFSET SmallFont
mov [hSmallFont],eax


The -11 is the font size, it is calculated as follows...

lfHeight = -((PointSize*96) / 72)

More accurately just in case...

CalcFontSize FRAME FSIZE
LOCAL hDC :D
LOCAL hDesktop :D

invoke GetDesktopWindow
mov [hDesktop],eax
invoke GetDC,[hDesktop]
mov [hDC],eax
invoke GetDeviceCaps,[hDC],LOGPIXELSY
push eax
invoke ReleaseDC,[hDesktop],hDC
pop eax
mul [FSIZE]
mov ecx,72
div ecx
neg eax
ret
ENDF


The font weight (FW_NORMAL) can be any of the following values:

FW_DONTCARE 0
FW_THIN 100
FW_EXTRALIGHT 200
FW_ULTRALIGHT 200
FW_LIGHT 300
FW_NORMAL 400
FW_REGULAR 400
FW_MEDIUM 500
FW_SEMIBOLD 600
FW_DEMIBOLD 600
FW_BOLD 700
FW_EXTRABOLD 800
FW_ULTRABOLD 800
FW_HEAVY 900
FW_BLACK 900

The font structure can be created automatically using my FontBuilder addin for RadASM, it will allow you to choose a font, preview it and insert the structure in your code, it even reads your assembler type and writes the structure in the appropriate syntax.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

tekhead009

Awsome, buttons that look normal.

I just fired up your FontBuilder too, and now I can italic, bold, size-point my way through anything!

Thank-you.

Tedd

You get that effect from system font that's applied as default to the controls when they're created. So, in one way or another you have to apply your new font selection to every control :P
You do have the option of creating the dialog/window from a resource file - in which you can specify the font, and the tedious work gets done for you.
Or, you can avoid messing about with CreateFont and use GetStockObject and select one of the sytem-wide resources.

push ebx
invoke GetStockObject, DEFAULT_GUI_FONT
mov ebx,eax

invoke SendMessage, hButton1,WM_SETFONT,ebx,0
invoke SendMessage, hButton2,WM_SETFONT,ebx,0
    .
    .

;no need to delete stock objects ;)
pop ebx
No snowflake in an avalanche feels responsible.

tekhead009

Oh great! I don't have to waste memory storing fonts if all I'm just going to be using is the default GUI font. [Why must there be 8 way to accomplish every task through the Windows API?]

Your comments say that you don't need to delete stock objects, does this mean Ishould be closing the handle to my created fonts? Was not closing them causing a memory leak? If so, is there any easy rule to remembering when I should close stuff? I would assume anytime the W32 Reference guide refers to a return as a handle.

I will eventually get into using resource files, but I'd like to get a little more familiar with the Windows API first and manually creating the GUI. Am I wasting my time by thinking that may be helpful in the future?

Tedd

It's generally a good idea to clean EVERYTHING after you've finished with it  ::) Especially GUI stuff - this is the most common source of memory leaks. So if you have a handle to a font/object/file/etc, you almost always should delete/free it once you're done with it. (Deleting stock object handles isn't required, but doesn't cause harm.)

Of course learning 101 ways to do the same thing isn't a waste of time :lol Some ways are sometimes more appropriate depnding on what you're doing.
No snowflake in an avalanche feels responsible.

jojo

> It's generally a good idea to clean EVERYTHING after you've finished with it   Especially GUI stuff - this is the most common source of memory leaks. So if you have a handle to a font/object/file/etc, you almost always should delete/free it once you're done with it.

Fonts, regions, pens, brushes etc. need to be deleted "once you're done with it", but before deleting them, make sure they are no longer selected in a device context! A typical sequence looks like this:

  Grey_Pen=CreatePen(PS_SOLID,1,RGB(96,96,96)   ; create a grey pen for painting
  hp_old=SelectObject(_DC(),Grey_Pen)   ; select it into your current device context, AND REMEMBER the old pen
  ... do some painting ...
  SelectObject(_DC(),hp_old)   ; select the old pen back - this frees the Grey_Pen
  DeleteObject(Grey_Pen)   ; ok now to delete your pen