?? lion-tutorial22.htm
字號:
<b> .endif</b>
<br>
<b> invoke
CallWindowProc,OldWndProc,hEdit,uMsg,eax,lParam</b> <br>
<b> ret</b>
<br>
<b> .endif</b> <br>
<b> .elseif uMsg==WM_KEYDOWN</b> <br>
<b> mov eax,wParam</b> <br>
<b> .if al==VK_RETURN</b> <br>
<b> invoke
MessageBox,hEdit,addr Message,addr AppName,MB_OK+MB_ICONINFORMATION</b> <br>
<b> invoke
SetFocus,hEdit</b> <br>
<b> .elseif al==VK_TAB</b> <br>
<b> invoke
GetKeyState,VK_SHIFT</b> <br>
<b> test eax,80000000</b>
<br>
<b> .if ZERO?</b>
<br>
<b>
invoke GetWindow,hEdit,GW_HWNDNEXT</b> <br>
<b>
.if eax==NULL</b> <br>
<b>
invoke GetWindow,hEdit,GW_HWNDFIRST</b> <br>
<b>
.endif</b> <br>
<b> .else</b>
<br>
<b>
invoke GetWindow,hEdit,GW_HWNDPREV</b> <br>
<b>
.if eax==NULL</b> <br>
<b>
invoke GetWindow,hEdit,GW_HWNDLAST</b> <br>
<b>
.endif</b> <br>
<b> .endif</b>
<br>
<b> invoke
SetFocus,eax</b> <br>
<b> xor eax,eax</b>
<br>
<b> ret</b>
<br>
<b> .else</b> <br>
<b> invoke
CallWindowProc,OldWndProc,hEdit,uMsg,wParam,lParam</b> <br>
<b> ret</b>
<br>
<b> .endif</b> <br>
<b> .else</b> <br>
<b> invoke CallWindowProc,OldWndProc,hEdit,uMsg,wParam,lParam</b>
<br>
<b> ret</b> <br>
<b> .endif</b> <br>
<b> xor eax,eax</b> <br>
<b> ret</b> <br>
<b>EditWndProc endp</b> <br>
<b>end start</b> <br>
<h3> Analysis:</h3>
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:
<p><b> .if uMsg==WM_CREATE</b> <br>
<b> mov wc.cbSize,sizeof WNDCLASSEX</b>
<br>
<b> invoke GetClassInfoEx,NULL,addr
EditClass,addr wc</b>
<p>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.
<p><b> push wc.lpfnWndProc</b> <br>
<b> pop OldWndProc</b> <br>
<b> mov wc.lpfnWndProc, OFFSET EditWndProc</b>
<br>
<b> push hInstance</b> <br>
<b> pop wc.hInstance</b> <br>
<b> mov wc.lpszClassName,OFFSET OurClass</b>
<p>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 with hInstance
of your own program. And you must choose a new name for the new class.
<p><b> invoke RegisterClassEx, addr
wc</b>
<p>When all is ready, register the new class. You will get a new class with some
characteristics of the old class.
<p><b> xor ebx,ebx</b> <br>
<b> mov edi,20</b> <br>
<b> .while ebx<6</b> <br>
<b> invoke
CreateWindowEx,WS_EX_CLIENTEDGE,ADDR OurClass,NULL,\</b> <br>
<b>
WS_CHILD+WS_VISIBLE+WS_BORDER,20,\</b> <br>
<b>
edi,300,25,hWnd,ebx,\</b> <br>
<b>
hInstance,NULL</b> <br>
<b> mov dword
ptr [hwndEdit+4*ebx],eax</b> <br>
<b> add edi,25</b>
<br>
<b> inc ebx</b>
<br>
<b> .endw</b> <br>
<b> invoke SetFocus,hwndEdit</b>
<p>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. <br>
At this point, you got 6 edit controls which accept only hex digits. The substituted
window proc handles the filter. Actually, it's identical to the window proc
in subclassing example. As you can see, you don't have to do extra work of subclassing
them.
<p>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. <br>
<p><b> .elseif al==VK_TAB</b> <br>
<b> invoke
GetKeyState,VK_SHIFT</b> <br>
<b> test eax,80000000</b>
<br>
<b> .if ZERO?</b>
<br>
<b>
invoke GetWindow,hEdit,GW_HWNDNEXT</b> <br>
<b>
.if eax==NULL</b> <br>
<b>
invoke GetWindow,hEdit,GW_HWNDFIRST</b> <br>
<b>
.endif</b> <br>
<b> .else</b>
<br>
<b>
invoke GetWindow,hEdit,GW_HWNDPREV</b> <br>
<b>
.if eax==NULL</b> <br>
<b>
invoke GetWindow,hEdit,GW_HWNDLAST</b> <br>
<b>
.endif</b> <br>
<b> .endif</b>
<br>
<b> invoke
SetFocus,eax</b> <br>
<b> xor eax,eax</b>
<br>
<b> ret</b>
<p>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 the key is pressed, the high bit of eax
is set. If not, the high bit 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. <br>
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.
<hr size="1">
<div align="center"> This article come from Iczelion's asm page, Welcom to <a href="http://asm.yeah.net">http://asm.yeah.net</a></div>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -