9 Replies - 5300 Views - Last Post: 04 July 2012 - 03:34 PM

#1 Latias  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 28-June 12

masm - changing textcolor in a dialog on initialize

Posted 03 July 2012 - 01:55 AM

hey all

actually i got 3 question, but the main question is;

1. how to change the textcolor in a dialog on initialize? I googled and found a lot of example, like SetTextColor, but nothing worked. (I used the message WM_INITIALZE and more).

the other question is not very important but i want to know;

2. how to disable dialogbox resizing? (not with a resource editor but just in asm code)

my last question is ;

3. why i need to write a RET after this code, else it wont work

.elseif uMsg ==WM_CTLCOLORDLG
 invoke CreateSolidBrush, 2200045; Set backgroundcolor of the dialog
ret; why a RET and by other WM_ xxx  not?


Project;
http://www.sendspace.com/file/uz485g


Is This A Good Question/Topic? 0
  • +

Replies To: masm - changing textcolor in a dialog on initialize

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: masm - changing textcolor in a dialog on initialize

Posted 03 July 2012 - 02:53 AM

It doesn't work, help us help you. Post the full code you have.

You change ctl colors in response to wm_ctl* messages.

look into setwindowlong to change a window style. After you use it, you will need to use setwindowpos

Why ret? Because your not only returning from the callback, but returning a brush.

Look in our tutorial section, I have a few tuts changing ct colors in response to window messages. Not at a computer to help more atm.
Was This Post Helpful? 0
  • +
  • -

#3 Latias  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 28-June 12

Re: masm - changing textcolor in a dialog on initialize

Posted 03 July 2012 - 09:34 AM

i will google about setwindowlong

i couldnt find the tutorials your talking about

Quote

Post the full code you hav

its to much to post so thats why i pasted a link to my whole project :)
Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: masm - changing textcolor in a dialog on initialize

Posted 03 July 2012 - 02:59 PM

Your asm source file is 131 lines including blank lines, trust me, that is not too much. I easily write over 10,000 line Assembly apps.

This is a standard template for a DialogProc:
DialogProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
	mov		eax,uMsg
	.if eax==WM_INITDIALOG

	.elseif eax==WM_COMMAND
		mov		edx,wParam
		movzx	eax,dx
		shr		edx,16
		.if edx==BN_CLICKED
			.if eax==IDOK

			.elseif eax==IDCANCEL
				invoke SendMessage,hWin,WM_CLOSE,NULL,NULL
			.endif
		.endif
	.elseif eax==WM_CLOSE
		invoke EndDialog,hWin,NULL
	.else
		mov		eax,FALSE
		ret
	.endif
	mov		eax,TRUE
	ret
DialogProc endp


Your missing the .else statement

When using Dialogs, you should not use SetFocus, instead use SendMessage with the notification message WM_NEXTDLGCTL

What's up with all the SetCursor calls? You just need to handle WM_SETCURSOR

You create a font, destroy it when done!
You create a brush, destroy it when done!
You load an icon and cursor, destroy them when done!

LoadIcon, LoadCursor are older functions, use LoadImage instead.
If you use LoadImage to load an icon, cursor, bitmap etc... use the LR_SHARED flag and the OS will destroy the handle when not needed.

How to prevent sizing? Don't use the WS_SIZEBOX style in your rc file for the dialog.

Change text color on the dialog? A dialog has no text, it is just a canvas. To display text you need to use either an edit control or static control. You can also display/draw text with DrawText and TextOut.

You need to read about Windows Messages and how callback procs work. Most messages you must return 0 if you handle them otherwise windows will do it's own thing. Some messages you have to return 1, some a handle to a brush. Each message will tell you what to return if you handle it.

In the case of your WM_CTLCOLORDLG, as per MSDN:

Quote

Return Value

If an application processes this message, it must return a handle to a brush. The system uses the brush to paint the background of the dialog box.


You have a resource leak BIG TIME!!!!! Every time WM_CTLCOLORDLG is sent to your proc, you create a new brush each time... Your proc does not receive this message only one time but MANY TIMES! Anytime windows needs to redraw your dialog you will get this message. Create your brush/es in WM_INITDIALOG.

That should get you going.
Was This Post Helpful? 1
  • +
  • -

#5 Latias  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 28-June 12

Re: masm - changing textcolor in a dialog on initialize

Posted 04 July 2012 - 02:53 AM

many thanks for the clearly info

i changed the code a bit, but still got some trouble with the code

the icon isn't shown and the bachground color also not shown


Quote

Change text color on the dialog? A dialog has no text, it is just a canvas.


actually i mean the text in the textbox/editbox of the dialogbox. i want this text red instead in black.

.DATA
TextColor equ	Red
Icon	  db "skull.ico",0

.DATA?
hInstance	HINSTANCE	?
hFont		dd	?
hCursor		dd	?
hIcon		dd	?


DlgProc Proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

.if uMsg == WM_COMMAND
mov eax, wParam
 
.elseif uMsg == WM_INITDIALOG
	invoke LoadImage,hInstance,Addr Icon,IMAGE_ICON,0,0,LR_SHARED ; Not working
	mov hIcon,eax
	invoke LoadIcon, hInstance,eax
	
	invoke GetDlgItem,hWnd,1005
	invoke EnableWindow,eax,FALSE
	
	invoke SetDlgItemText,hWnd,1002, Addr EditText
	invoke SetWindowText, hWnd, Addr WindowTitle

	invoke CreateFontIndirect,Addr Font
	mov hFont,eax
	invoke GetDlgItem,hWnd,1002
	invoke SendMessage,eax,WM_SETFONT,hFont,0	
	
	invoke GetDlgItem, hWnd,1002
	invoke SendMessage, hWnd, WM_NEXTDLGCTL, eax, TRUE

	invoke SendMessage, hWnd,WM_CTLCOLORDLG, CreateSolidBrush, 2200045 ; not working

	
	invoke CenterWindow,hWnd
	ret
	

.elseif uMsg==WM_SETCURSOR

	invoke LoadImage,hInstance,3000,IMAGE_CURSOR,0,0,LR_SHARED
	mov hCursor,eax
	invoke SetCursor,hCursor


.elseif uMsg == WM_CLOSE
	invoke DeleteObject, hCursor
	invoke DeleteObject, hFont
	invoke DeleteObject, hIcon
	
	invoke EndDialog, hWnd, NULL
.else
	mov eax,FALSE
	ret

.endif
	mov eax,TRUE
Ret
DlgProc endp



Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: masm - changing textcolor in a dialog on initialize

Posted 04 July 2012 - 09:10 AM

Comments inline:
include masm32rt.inc


DlgProc			Proto 	:DWORD, :DWORD, :DWORD, :DWORD 
.DATA
WindowTitle	db	"This is the title of our window",0
EditText 		db	"This is the text in the textbox",0
Font			LOGFONT <-16,0,0,0,FW_NORMAL,FALSE,FALSE,FALSE,0,3,2,1,34,"Impact">

; not needed already defined in Windows.inc
;TextColor 		equ	Red
Icon	  db "skull.ico",0

.DATA?
hInstance	HINSTANCE	?
hFont		dd	?
hCursor		dd	?
hBrush      dd  ?
; hIcon		dd	? ; not needed

.Code
Start:
    
	invoke GetModuleHandle, NULL
	mov hInstance, eax
	invoke DialogBoxParam, hInstance, 1001, NULL, Addr DlgProc, NULL
	
	; Missing this!!!!!!
	invoke  ExitProcess, 0

DlgProc Proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

.if uMsg == WM_COMMAND
mov eax, wParam
 
.elseif uMsg == WM_INITDIALOG
    ; your loading an icon from file, not a resource?  This is wrong then
	; invoke LoadImage,hInstance,Addr Icon,IMAGE_ICON,0,0,LR_SHARED ; Not working
	invoke  LoadImage, NULL, offset Icon, IMAGE_ICON, 0, 0, LR_LOADFROMFILE or LR_SHARED
	
	; not needed
	; mov     hIcon,eax
	
	; your missing this
	invoke  SendMessage, hWnd, WM_SETICON, ICON_SMALL, eax
	
	; This is not needed
	;invoke LoadIcon, hInstance,eax
	
	; this is moved from WM_SETCURSOR
	invoke LoadImage,hInstance,3000,IMAGE_CURSOR,0,0,LR_SHARED
	mov     hCursor,eax
	
	
	invoke GetDlgItem,hWnd,1005
	invoke EnableWindow,eax,FALSE
	
	invoke SetDlgItemText,hWnd,1002, Addr EditText
	invoke SetWindowText, hWnd, Addr WindowTitle

	invoke CreateFontIndirect,Addr Font
	mov hFont,eax
	invoke GetDlgItem,hWnd,1002
	invoke SendMessage,eax,WM_SETFONT,hFont,0	
	
	invoke GetDlgItem, hWnd,1002
	invoke SendMessage, hWnd, WM_NEXTDLGCTL, eax, TRUE

    ; this does not go here!!!  Also wrong format
	;invoke SendMessage, hWnd,WM_CTLCOLORDLG, CreateSolidBrush, 2200045 ; not working
    invoke  CreateSolidBrush, 2200045
    mov     hBrush, eax
    
	; use the DS_CENTER flag for the dialog in your resource file
    ;	invoke CenterWindow,hWnd
	
	; not needed
	;ret
	

.elseif uMsg==WM_SETCURSOR
    ; EVERYTIME your mouse moves, you are making an unnecessary call!
;	invoke LoadImage,hInstance,3000,IMAGE_CURSOR,0,0,LR_SHARED
;	mov hCursor,eax
	invoke SetCursor, hCursor


.elseif uMsg == WM_CLOSE
	invoke DeleteObject, hCursor
	invoke DeleteObject, hFont
;	invoke DeleteObject, hIcon
	
	; create a brush, delete brush
	invoke  DeleteObject, hBrush
	invoke EndDialog, hWnd, NULL
	
    ; ##################################
    ; ######## Color stuff here ########
    ; Color dialog
    .elseif uMsg == WM_CTLCOLORDLG
        mov     eax, hBrush
        ret
    
    ; Color edit
    .elseif uMsg == WM_CTLCOLOREDIT
        invoke  SetTextColor, wParam, Red
        invoke  SetBkMode, wParam, TRANSPARENT
        invoke  GetSysColorBrush, COLOR_WINDOW        
        ret
.else
	mov eax,FALSE
	ret

.endif
	mov eax,TRUE
Ret
DlgProc endp

end Start


Was This Post Helpful? 1
  • +
  • -

#7 Latias  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 28-June 12

Re: masm - changing textcolor in a dialog on initialize

Posted 04 July 2012 - 09:55 AM

thanks works perfectly!

but;

Quote

your loading an icon from file, not a resource?


yes i want loading from resources, but i defined the skull.ico with the id of 4000, but when calling it doesnt show the icon.

invoke  LoadImage, NULL, 4000, IMAGE_ICON, 0, 0, LR_LOADFROMFILE or LR_SHARED
invoke  SendMessage, hWnd, WM_SETICON, ICON_SMALL, eax


and another question is why i need to use this code since Dialog Proc should returning just zero

.else
	mov eax,FALSE
	ret
.endif
	mov eax,TRUE
	Ret
DlgProc endp

Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: masm - changing textcolor in a dialog on initialize

Posted 04 July 2012 - 12:09 PM

Well you define this:
Icon      db "skull.ico",0


Then used this to load the icon:
 invoke LoadImage,hInstance,Addr Icon,IMAGE_ICON,0,0,LR_SHARED 

You are trying to load the icon by string.

But you have it defined in your resource file with the ID of 4000, maybe you meant this:
ICO_SKULL       equ     4000
...
...
...
.code
invoke  LoadImage, hInstance, ICO_SKULL, IMAGE_ICON, 0, 0, LR_SHARED

You can load a resource by string, but that is beyond the scope of a beginner.

Quote

.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
Ret
DlgProc endp

You need that code, well just because. That is how Windows works.

Some messages (As you learned) must return a certain value if you process it. By returning FALSE for messages you do not handle (the .else statement), you are telling windows that you did not handle the message and to do its default thing with the message.
Was This Post Helpful? 0
  • +
  • -

#9 Latias  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 28-June 12

Re: masm - changing textcolor in a dialog on initialize

Posted 04 July 2012 - 01:19 PM

ah i see, its working. thank!

got 1 more question about the Setfocus.
i made a new editbox, with a id of 1006.

i did this, but it's still focussed on edit1. any idea why?

Edit2	equ 1006

----

invoke GetDlgItem, hWnd, Edit2
invoke SendMessage, hWnd, WM_NEXTDLGCTL, eax, TRUE



Was This Post Helpful? 0
  • +
  • -

#10 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: masm - changing textcolor in a dialog on initialize

Posted 04 July 2012 - 03:34 PM

For that message you need to use PostMessage instead of SendMessage
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1