Tutorial 22: Superclassing
In this Tutorial, We Well Learn About Superclassing, What It is and what it is for. You will Also Learn How to Provide Tab Key Navigation To The Controls in Your Own Window.
Download the Example
Here
Theory:. In your programming career, you will surely encounter a situation where you need several controls with * slightly * different behavior For example, you may need 10 edit controls which accept only number There are several ways to achieve that goal.:
Create your own class and instantiate the controls Create those edit control and then subclass all of them Superclass the edit control The first method is too tedious. You have to implement every functionality of the edit control yourself. Hardly a task to be taken lightly. The second method is better than the first one but still too much work. It is ok if you subclass only a few controls but it's going to be a nightmare to subclass a dozen or so controls. Superclassing is the technique you should use for this occasion.
Subclassing is the method you use to * take control * of a particular window class. By * taking control *, I mean you can modify the properties of the window class to suit your purpose then then create the bunch of controls.
The steps in superclassing is outlined Below:
call GetClassInfoEx to obtain the information about the window class you want to superclass. GetClassInfoEx requires a pointer to a WNDCLASSEX structure which will be filled with the information if the call returns successfully. Modify the WNDCLASSEX members that you want. However, there are two members Which you must modify:
hInstance You must put the instance handle of your program into this member. lpszClassName You must provide it with a pointer to a new class name. You need not modify lpfnWndProc member but most of the time, you need to do it. Just remember to save the original lpfnWndProc member if you want to call it with CallWindowProc. Register the modifed WNDCLASSEX structure. you'll have a new window class which has several characteristics of the old window class. Create windows from the new class Superclassing is better than subclassing if you Want to Create Many Controls with The Same Characteristics.example:
.386
.Model flat, stdcall
Option CaseMAP: NONE
INCLUDE /MASM32/INCLUDE/Windows.inc
INCLUDE /MASM32/INCLUDE/USER32.INC
INCLUDE /MASM32/INCLUDE / WANEL32.INC
INCLUDELIB /MASM32/LIB/USER32.LIB
INCLUDELIB /MASM32/LIB/kernel32.lib
WM_SUPERCLASS EQU WM_USER 5 WINMAIN Proto: DWORD,: DWORD,: DWORD,: DWORD EditWord,: DWORD,: DWORD,: DWORD,: DWORD
.DATA CLASSNAME DB "Superclasswinclass", 0 AppName DB "Superclassing Demo", 0 Editclass DB "Edit", 0 Ourclass DB "SupeReditclass", 0 Message DB "You Pressed The Enter Key in The Text Box!", 0
.DATA? HINSTANCE DD? HWNDIT DD 6 DUP (?) OldWndProc DD?
. Code Start: Invoke GetModuleHandle, Null Mov Hinstance, Eax Invoke Winmain, Hinstance, Null, Null, 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 hInst pop wc.hInstance mov wc.hbrBackground, COLOR_APPWORKSPACE 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, WS_EX_CLIENTEDGE WS_EX_CONTROLPARENT , ADDR ClassName, ADDR AppName, / WS_OVERLAPPED WS_CAPTION WS_SYSMENU WS_MINIMIZEBOX WS_MAXIMIZEBOX WS_VISIBLE, CW_USEDEFAULT, / CW_USEDEFAULT, 350,220, NULL, NULL, / hInst, NULL mov hwnd, eax.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 uses ebx edi hWnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM LOCAL wc: WNDCLASSEX .if uMsg == WM_CREATE mov wc.cbSize, sizeof WNDCLASSEX invoke GetClassInfoEx, NULL, addr EditClass, addr wc push wc. lpfnWndProc pop OldWndProc mov wc.lpfnWndProc, OFFSET EditWndProc push hInstance pop wc.hInstance mov wc.lpszClassName, OFFSET OurClass invoke RegisterClassEx, addr wc xor ebx, ebx mov edi, 20 .while ebx <6 invoke CreateWindowEx, WS_EX_CLIENTEDGE, aDDR OurClass, NULL , / WS_CHILD WS_VISIBLE WS_BORDER, 20, / EBX, 300, 25, HWND, EBX, / HINSTANCE, NULL MOV DWORD PTR [HWndedit 4 * EBX], EAX Add Edi, 25 Inc EBX .Endw Invoke SetFocus, HWndIt. Elseif UMSG == WM_DESTROY INVOKE POSTQUITMESSAGE, NULL .ELSE INVOKE DEFWINDOWPROC, HWND, UMSG, WPARAM, LPARAM RET .Endif XOR EAX, EAX RET WNDPROC ENDP
EditWordProc Proc Hedit: DWORD, UMSG: DWORD, WPARAM: DWORD, LPARAM: DWORD .IF UMSG == WM_CHAR MOV EAX, WPARAM .IF (Al> = "0" && al <= "9") || (al> = "A" && al <= "f") || (al> = "a" && al <= "f") || Al == vk_back .if al> = "a" && al <= "f" SUB al, 20h .endif invoke CallWindowProc, OldWndProc, hEdit, uMsg, eax, lParam ret .endif .elseif uMsg == WM_KEYDOWN mov eax, wParam .if al == VK_RETURN invoke MessageBox, hEdit, addr Message, addr AppName, MB_OK MB_ICONINFORMATION invoke SetFocus, hEdit .elseif al == VK_TAB invoke GetKeyState, VK_SHIFT test eax, 80000000 .if ZERO? invoke GetWindow, hEdit, GW_HWNDNEXT .if eax == NULL invoke GetWindow, hEdit, GW_HWNDFIRST .endif .else invoke GetWindow, hEdit, GW_HWNDPREV .IF EAX == Null Invoke GetWindow, H Edit, GW_HWNDLAST .endif .endif invoke SetFocus, eax xor eax, eax ret .else invoke CallWindowProc, OldWndProc, hEdit, uMsg, wParam, lParam ret .endif .else invoke CallWindowProc, OldWndProc, hEdit, uMsg, wParam, lParam ret .endif XOR EAX, EAX RET EditWndProc Endp End Start
Analysis: The program will create a simple window with 6 "modified" edit controls in its client area The edit controls will accept only hex digits Actually, I modified the subclassing example to do superclassing The program starts normally and the interesting part is... When the main window is created: .if umsg == wm_create mov wc.cbsize, sizeof wndclassex invoke getclassinfoex, null, addr editclass, addr wc
We must first fill the WNDCLASSEX structure with the data from the class which we want to superclass, in this case, it's EDIT class. Remember that you must set the cbSize member of the WNDCLASSEX structure before you call GetClassInfoEx else the WNDCLASSEX structure will not be Filled Properly. After getClassInfoEx Returns, WC is Filled With all information we need to create a new window class.
Push wc.lpfnwndproc Pop OldWndProc Wc.lpfnWndProc, Offset EditWndProc Push Hinstance Pop Wc.hinstance Mov wc.lpszclassname, Offset Ourclass
Now we must modify some members of wc. The first one is the pointer to the window procedure. Since we need to chain our own window procedure with the original one, we have to save it into a variable so we can call it with CallWindowProc. This technique is identical to subclassing except that you modify the WNDCLASSEX structure directly without having to call SetWindowLong. The next two members must be changed else you will not be able to register your new window class, hInstance and lpsClassName. you must replace original hInstance value .
Invoke RegisterClassex, Addr WC
When all is ready, register the new class. You will get a new class with some characteristics of the old class.xor ebx, ebx mov edi, 20 .while ebx <6 invoke CreateWindowEx, WS_EX_CLIENTEDGE, ADDR OurClass, NULL, / WS_CHILD WS_Visible WS_Border, 20, / EDI, 300, 25, HWND, EBX, / Hinstance, Null Mov DWORD PTR [HWndET 4 * EBX], EAX Add Edi, 25 Inc EBX .Endw Invoke setfocus, HWndedit
Now that we registered the class, we can create windows based on it. In the above snippet, I use ebx as the counter of the number of windows created. Edi is used as the y coordinate of the left upper corner of the window. When a window is created, its handle is stored in the array of dwords. When all windows are created, set input focus to the first window. At this point, you got 6 edit controls which accept only hex digits. The substituted window proc handles the .
I throw in a code snippet to handle control navigation with tabs to make this example more juicy. Normally, if you put controls on a dialog box, the dialog box manager handles the navigation keys for you so you can tab to go to the next control or shift-tab to go back to the previous control. Alas, such feature is not available if you put your controls on a simple window. you have to subclass them so you can handle the Tab keys yourself. In our example, we need not Subclass The Controls One by One Because We Already Superclassed Them, So We CAN Provide a "Central Control Navigation Manager" for them.
.elseif al == VK_TAB invoke GetKeyState, VK_SHIFT test eax, 80000000 .if ZERO? invoke GetWindow, hEdit, GW_HWNDNEXT .if eax == NULL invoke GetWindow, hEdit, GW_HWNDFIRST .endif .else invoke GetWindow, hEdit, GW_HWNDPREV .if eax = = Null Invoke GetWindow, Hedit, GW_HWndlast .ndif.ndif Invoke SetFocus, EAX XOR Eax, Eax Ret
The above code snippet is from EditWndClass procedure. It checks if the user press Tab key, if so, it call GetKeyState to check if the SHIFT key is also pressed. GetKeyState returns a value in eax that determines whether the specified key is pressed or not IF NOT OF EAX IS set. If NOT OF EAX IS CLEAR. SO We Test The Return Value Against 80000000H. If The High Bit Is Set, IT Means The User Pressed Shift Tab Which WE must handle separately. If the user press Tab key alone, we call GetWindow to retrieve the handle of the next control. We use GW_HWNDNEXT flag to tell GetWindow to obtain the handle to the window that is next in line to the current hEdit. If this function returns NULL, we interpret it as no more handle to obtain so the current hEdit is the last control in the line. We will "wrap around" to the first control by calling GetWindow with GW_HWNDFIRST flag. Similar to the Tab case, shift- Tab Just Works in Reverse.