News:

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

Changing a Button's font size

Started by Crashish, June 20, 2005, 12:02:22 AM

Previous topic - Next topic

Crashish

I've created a simple window with one push button but, the text (pt-size) on the button is too big for my liking. I'd like to know if there is a way to adjust the font size to whatever I choose, say around 6 for example?

Would using CreateFont and processing WM_SETFONT be the way to achieve this?

hutch--

Crash,

If you just want the font to look a little tidier, try the various default system ones first, the variable ANSI font looks OK for most things. If you specifically want a true type font, you will have to use CreateFont() for it. Try the reference for GetStockObject() for the range of system fonts.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Crashish

Hello and thanks for the quick response.

I'm not completely sure how to do this but I'll keep trying and post back if I get stuck.

Thanks again for your help.

[edit]

donkey

"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

Crashish

I tried what Hutch and Tedd suggested which worked very easily.
So now I'm trying to apply what Donkey posted about using CreateFontIndirect.

Is this the correct way to use a newly created font handle? It works, but is it correct or rather the best way to go about it?

.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\gdi32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\gdi32.lib

WinMain proto :DWORD,:DWORD,:DWORD,:DWORD

.data
ClassName db "SimpleWinClass",0
AppName  db "Our First Window",0

ButtonClassName db "button",0
ButtonText      db "TEST",0
ButtonID equ 1030

SmallFont LOGFONT <-11,0,0,0,FW_NORMAL,FALSE,FALSE,0,0,0,0,0,0,"Arial">

.data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
hButton dd ?
hSmallFont dd ?

.code
start:
invoke GetModuleHandle, NULL
mov    hInstance,eax
invoke GetCommandLine
mov    CommandLine,eax
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
invoke ExitProcess,eax

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND
mov   wc.cbSize,SIZEOF WNDCLASSEX
mov   wc.style, CS_HREDRAW or CS_VREDRAW
mov   wc.lpfnWndProc, OFFSET WndProc
mov   wc.cbClsExtra,NULL
mov   wc.cbWndExtra,NULL
push  hInstance
pop   wc.hInstance
mov   wc.hbrBackground,COLOR_WINDOW+1
mov   wc.lpszMenuName,NULL
mov   wc.lpszClassName,OFFSET ClassName
invoke LoadIcon,NULL,IDI_APPLICATION
mov   wc.hIcon,eax
mov   wc.hIconSm,eax
invoke LoadCursor,NULL,IDC_ARROW
mov   wc.hCursor,eax
invoke RegisterClassEx, addr wc
INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
           WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\
           CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,\
           hInst,NULL
mov   hwnd,eax
invoke ShowWindow, hwnd,SW_SHOWNORMAL
invoke UpdateWindow, hwnd
.WHILE TRUE
invoke GetMessage, ADDR msg,NULL,0,0
.BREAK .IF (!eax)
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
.ENDW
mov     eax,msg.wParam
ret
WinMain endp

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM


    .IF uMsg==WM_DESTROY
        invoke PostQuitMessage,NULL
        invoke DeleteObject,hSmallFont       


    .ELSEIF uMsg==WM_CREATE
        invoke  CreateWindowEx,WS_EX_CLIENTEDGE or WS_EX_STATICEDGE, ADDR ButtonClassName,ADDR ButtonText,\
                WS_CHILD or WS_VISIBLE or WS_TABSTOP or BS_DEFPUSHBUTTON,\
                50,180,100,21,hWnd,ButtonID,hInstance,NULL
        mov   hButton,eax
       
        invoke CreateFontIndirect,OFFSET SmallFont
           mov [hSmallFont],eax

        invoke SendMessage, hButton,WM_SETFONT,hSmallFont,0       ; 0(FASLE) & TRUE both work

    .ELSE
        invoke DefWindowProc,hWnd,uMsg,wParam,lParam
        ret
       
    .ENDIF
xor eax,eax
ret
WndProc endp
end start


Also, I was trying to do this below to begin with. Can you even do it this way?

.ELSEIF uMsg==WM_SETFONT
           mov wParam, OFFSET hSmallFont
           mov lParam, TRUE

Before I saw this below from another post:

SendMessage,hButton,WM_SETFONT,hSmallFont,0



Thanks again for your help

gabor

Hi Crash!

I played around a bit with your code and I'd say it is ok. It works...

.ELSEIF uMsg==WM_SETFONT
           mov wParam, OFFSET hSmallFont
           mov lParam, TRUE


To this code my first thought would be: is there any source for the WM_SETFONT message? What would send it and when? the uMsg is a message ID which message can be processsed or simply passed to DefWindowProc. So when you are selecting a message ELSEIF uMsg==WM_SETFONT then you have to process it using the parameters wParam and lParam. It is no use to alter those parameters, except you pass them to the default proc afterwards...

So, the invoke SendMessage, hButton,WM_SETFONT,hSmallFont,0 call is just fine.


Greets, Gábor


Crashish

Hello and thanks for the quick replay Gábor

I appreciate you taking the time out to explain that to me. 

Mark Jones

Should he DeleteObject before PostQuitMessage?
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

BytePtr

Quote from: Mark Jones on July 15, 2005, 01:56:55 PM
Should he DeleteObject before PostQuitMessage?

Yes it's recommended. I do always this. Otherways u can get memory leaks.
edit: crash modify your code.

Crashish


    .IF uMsg==WM_DESTROY
        invoke DeleteObject,hSmallFont
        invoke PostQuitMessage,NULL


This is correct now right?

I figured having PostQuitMessage before DeleteObject didn't really matter because DeleteObject still gets executed before the window is destroyed.
I tried putting a system Beep after PostQuitMessage to see if it gets executed and it does.

    .IF uMsg==WM_DESTROY
        invoke DeleteObject,hSmallFont
        invoke PostQuitMessage,NULL
        invoke Beep,50,50


Thanks for your help guys


gabor

Hi!


I guess it is quite clear that code executes after invoke PostQuitMessage,NULL since the execution of the thread does not terminate by calling this procedure. The procedure simply adds the QuitMessage to the queue of the calling thread:

"The PostQuitMessage function posts a WM_QUIT message to the thread's message queue and returns immediately; the function simply indicates to the system that the thread is requesting to quit at some time in the future." from WIN32 API help.

What won't be executed is code after the ExitProcess call.

About memory leaks left behind by undestroyed objects I found again in WIN32 API help:
"1. All of the object handles opened by the process are closed."

However, I must admit that destroying objects manually is the most secure way since the creating process itself knows the best what objects were created. One more thing to this, it is not always advantageous to delete object, closing their handles may be enough as well, especially when the object may be used soon, (say when the process is executed again).

Greets, Gábor