Page 1 of 1

MASM - Superclassing

#1 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon

Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Posted 01 August 2011 - 06:26 PM

Superclassing? Yup, it is like subclassing but better! Subclasing is fine for a few controls, but what if you need 10 edit controls in your app that only accept numbers and you want a custom cursor for them instead of windows defaut? Sure you could subclass them all and have a proc for each one, but that is a lot of code when we can do it in less.

Enter the Superclass where you want to add functionality to a bunch of controls..

You get the properties for a class (in our case the edit window class) modify a few properties, and register the new class.

You call GetClassInfoEx 2 things: the name of the class you want to Superclass and a pointer to a WNDCLASSEX structure. GetClassInfoEx will fill that structure.

Next, modify WNDCLASSEX, there are 2 members you must modify - hInstance and lpszClassName and in our example we modify lpfnWndProc

hInstance will be the handle to your app you get from GetModuleHandle
lpszClassName will be the name of your new class
lpfnWndProc will be the address of our callback proc

Step by step...
Get the class info for the Edit window class
	mov		wc.cbSize, sizeof WNDCLASSEX
	invoke	GetClassInfoEx, NULL, offset szWndClsEdit, addr wc

next change a few things, then register our new superclassed edit control:
	push	wc.lpfnWndProc
	pop		lpEditWndProc
	mov		wc.lpfnWndProc, offset WndProcEdit
	push    hInst
	pop		wc.hInstance
	;#####  Change the cursor for our edit windows
	invoke	LoadCursor, NULL, IDC_UPARROW
	mov		wc.hCursor, eax
	mov		wc.lpszClassName, offset szWndClsSuperEdit
	invoke	RegisterClassEx, addr wc    

As with subclassing, we need to save the original address for the edit window proc so we can pass on the messages, #1 and #2

#3 we are telling windows to use our Window proc for all controls created with our class name not needed, but makes our life easier for our task

#4 - 5 we are setting the hInstance for the control, this is a must

I decided to change the cursor for our class to an up arrow #7-8

#9 we give our class a name this is a must

Then we register our class.. That is it, now you can CreateWindowEx with our new classname! Instead of having to create multiple procs to handle each edit control, all controls created with our classname, will use the same callback proc.

in the attached sample, I create 4 edit controls that only accept numbers
        xor     ebx, ebx
        mov     edi, 5
        .while ebx < 4
            invoke  CreateWindowEx, 
                        offset szWndClsSuperEdit, 
                        WS_CHILD or WS_VISIBLE or WS_TABSTOP, 
                        5, edi, 
                        285, 20, 
                        hWin, ebx, 
                        hInst, 0
            mov     dword ptr [hEditHandles + 4 * ebx], eax
            add     edi, 50
            inc     ebx           

As you can see, superclassing a control is easier than subclassing if you want a bunch of controls with the same properties..

Attached File(s)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1