?? lion-tutorial32.htm
字號:
<html>
<head>
<title>Iczelion's Win32 Assembly Tutorial 32: Multiple Document Interface (MDI)</title>
<meta http-equiv="Content-Type" content="text/html; charset=">
</head>
<body bgcolor="#FFFFFF">
<h1 align="center"><font face="Tahoma">Tutorial 32: Multiple Document Interface
(MDI)</font></h1>
<p align="left"><font face="MS Sans Serif" size="-1">This tutorial shows you how
to create MDI application. It's actually not too difficult to do. Download <a href="files/tut32.zip">the
example</a>.</font></p>
<h3 align="left"><font face="MS Sans Serif" size="-1">Theory:</font></h3>
<p align="left"><font face="MS Sans Serif" size="-1">Multiple Document Interface
(MDI) is a specification for applications that handle multple documents at the
same time. You are familiar with Notepad: It's an example of Single Document
Interface (SDI). Notepad can handle only one document at a time. If you want
to open another document, you have to close the previous one first. As you can
imagine, it's rather cumbersome. Contrast it with Microsoft Word: Word can open
arbitrary documents at the same time and let the user choose which document
to use. Microsoft Word is an example of Multiple Document Interface (MDI).</font></p>
<p align="left"><font face="MS Sans Serif" size="-1">MDI application has several
characteristics that are distinctive. I'll list some of them:</font></p>
<ul>
<li><font face="MS Sans Serif" size="-1">Within the main window, there can be
multiple child windows in the client area. All child windows are clipped to
the client area.</font></li>
<li><font face="MS Sans Serif" size="-1">When you minimize a child window, it
minimizes to the lower left corner of the client area of the main window.</font></li>
<li><font face="MS Sans Serif" size="-1">When you maximize achild window, its
title merges with that of the main window.</font></li>
<li><font face="MS Sans Serif" size="-1">You can close a child window by pressing
Ctrl+F4 and switch the focus between the child windows by pressing Ctrl+Tab</font></li>
</ul>
<p><font face="MS Sans Serif" size="-1">The main window that contains the child
windows is called<b> a frame window</b>. Its client area is where the child
windows live, hence the name "frame". Its job is a little more elaborate
than a usual window because it needs to handle some coordination for MDI.</font></p>
<p><font face="MS Sans Serif" size="-1">To control an arbitrary number of child
windows in your client area, you need a special window called <b>client window</b>.
You can think of this client window as a transparent window that covers the
whole client area of the frame window. It's this client window that is the actual
parent of those MDI child windows. The client window is the real supervisor
of the MDI child windows.</font></p>
<table width="100%">
<tr>
<td> </td>
<td> </td>
<td>
<table border="1" cellpadding="3" align="center">
<tr>
<th nowrap>
<div align="center"><font face="MS Sans Serif" size="-1">Frame Window</font></div>
</th>
</tr>
</table>
</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<div align="center">| </div>
</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<table border="1" cellpadding="3" align="center">
<tr>
<th nowrap><font face="MS Sans Serif" size="-1">Client Window</font></th>
</tr>
</table>
</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<div align="center">|</div>
</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="5">
<hr noshade>
</td>
</tr>
<tr>
<td>
<div align="center">|</div>
</td>
<td>
<div align="center">|</div>
</td>
<td>
<div align="center">|</div>
</td>
<td>
<div align="center">|</div>
</td>
<td>
<div align="center">|</div>
</td>
</tr>
<tr>
<td>
<table border="1" cellpadding="3" align="center">
<tr>
<th nowrap>
<div align="center"><font face="MS Sans Serif" size="-1">MDI Child
1 </font></div>
</th>
</tr>
</table>
</td>
<td>
<table border="1" cellpadding="3" align="center">
<tr>
<th nowrap>
<div align="center"><font face="MS Sans Serif" size="-1">MDI Child
2 </font></div>
</tr>
</table>
</td>
<td>
<table border="1" cellpadding="3" align="center">
<tr>
<th nowrap>
<div align="center"><font face="MS Sans Serif" size="-1">MDI Child
3 </font></div>
</th>
</tr>
</table>
</td>
<td>
<table border="1" cellpadding="3" align="center">
<tr>
<th nowrap>
<div align="center"><font face="MS Sans Serif" size="-1">MDI Child
4 </font></div>
</th>
</tr>
</table>
</td>
<td>
<table border="1" cellpadding="3" align="center">
<tr>
<th nowrap>
<div align="center"><font face="MS Sans Serif" size="-1">MDI Child
n </font></div>
</th>
</tr>
</table>
</td>
</tr>
</table>
<p><font face="MS Sans Serif" size="-1"><b>Figure 1. The hierachy of an MDI application</b></font></p>
<h3><font face="Tahoma">Creating the Frame Window</font></h3>
<p><font face="MS Sans Serif" size="-1">Now we can turn our attention to the detail.
First of all you need to create a frame window. It's created the same way as
the normal window: by calling CreateWindowEx. There are two major differences
from a normal window.</font></p>
<p><font face="MS Sans Serif" size="-1">The first difference is that you <b>MUST</b>
call <font color="#000099"><b>DefFrameProc</b></font> instead of <font color="#990099">
<b> DefWindowProc</b></font> to process the Windows messages your window don't
want to handle. This is one way to let Windows do the dirty job of maintaining
MDI application for you. If you forget to use <font color="#000099"> <b>DefFrameProc</b></font>,
your application won't get the MDI feature. Period. <font color="#000099"><b>DefFrameProc</b></font>
has the following syntax:</font></p>
<blockquote>
<pre align="left"><font face="MS Sans Serif" size="-1"><b>DefFrameProc proc hwndFrame:DWORD,
hwndClient:DWORD,
uMsg:DWORD,
wParam:DWORD,
lParam:DWORD</b></font></pre>
</blockquote>
<p><font face="MS Sans Serif" size="-1">If you compare <font color="#003399"><b>DefFrameProc</b></font>
with <font color="#000099"><b>DefWindowProc</b></font>, you'll notice that the
only difference between them is that <font color="#000099"><b>DefFrameProc</b></font>
has 5 parameters while <font color="#003399"><b>DefWindowProc</b></font> has
only 4. The extra parameter is the handle to the client window. This handle
is necessary so Windows can send MDI-related messages to the client window.</font></p>
<p><font face="MS Sans Serif" size="-1">The second difference is that, you must
call <font color="#000099"><b>TranslateMDISysAccel</b></font> in the message
loop of your frame window. This is necessary if you want Windows to handle MDI-related
accelerator key strokes such as Ctrl+F4, Ctrl+Tab for you. It has the following
syntax: </font></p>
<blockquote>
<pre><font face="MS Sans Serif" size="-1"><b>TranslateMDISysAccel proc hwndClient:DWORD,
lpMsg:DWORD </b></font></pre>
</blockquote>
<p><font face="MS Sans Serif" size="-1">The first parameter is the handle to the
client window. This should not come as a surprise to you because it's the client
window that is the parent of all MDI child windows. The second parameter is
the address of the MSG structure you filled by calling <font color="#000099"><b>GetMessage</b></font>.
The idea is to pass the MSG structure to the client window so it could examine
if the MSG structure contains the MDI-related keypresses. If so, it processes
the message itself and returns a non-zero value, otherwise it returns FALSE.</font></p>
<p><font face="MS Sans Serif" size="-1">The steps in creating the frame window
can be summarized as follows:</font></p>
<ol>
<li><font face="MS Sans Serif" size="-1">Fill in the WNDCLASSEX structure as
usual</font></li>
<li><font face="MS Sans Serif" size="-1">Register the frame window class by
calling <font color="#000099"><b>RegisterClassEx</b></font></font></li>
<li><font face="MS Sans Serif" size="-1">Create the frame window by calling
<font color="#000099"> <b>CreateWindowEx</b></font></font></li>
<li><font face="MS Sans Serif" size="-1">Within the message loop, call <font color="#000099"><b>TranslateMDISysAccel</b></font>.</font></li>
<li><font face="MS Sans Serif" size="-1">Within the window procedure, pass the
unprocessed messages to <font color="#000099"><b>DefFrameProc</b></font> instead
of <font color="#000099"><b>DefWindowProc</b></font>.</font></li>
</ol>
<h3><font face="Tahoma">Creating the Client Window</font></h3>
<p><font face="MS Sans Serif" size="-1">Now that we have the frame window, we
can create the client window. The client window class is pre-registered by Windows.
The class name is "MDICLIENT". You also need to pass the address of
a <font color="#990099"><b>CLIENTCREATESTRUCT</b></font> structure to <font color="#000099"><b>CreateWindowEx</b></font>.
This structure has the following definition:</font></p>
<blockquote>
<pre><font face="MS Sans Serif" size="-1"><b>CLIENTCREATESTRUCT struct
</b></font><b><font face="MS Sans Serif" size="-1"> hWindowMenu dd ?
idFirstChild dd ?
CLIENTCREATESTRUCT ends</font></b></pre>
</blockquote>
<p><font face="MS Sans Serif" size="-1"><b>hWindowMenu</b></font><font face="MS Sans Serif" size="-1">
is the handle to the submenu that Windows will append the list of MDI child
window names. This feature requires a little explanation. If you ever use an
MDI application like Microsoft Word before, you'll notice that there is a submenu
named "window" which, on activation, displays various menuitems related
to window management and at the bottom, the list of the MDI child window currently
opened. That list is internally maintained by Windows itself: you don't have
to do anything special for it. Just pass the handle of the submenu you want
the list to appear in <b>hWindowMenu</b> and Windows will handle the rest. Note
that the submenu can be <b>ANY</b> submenu:it doesn't have to be the one that
is named "window". The bottom line is that, you <b>should</b> pass
the handle to the submenu you want the window list to appear. If you don't want
the list, just put NULL in <b>hWindowMenu</b>. You get the handle to the submenu
by calling <font color="#000099"><b>GetSubMenu</b></font>.</font></p>
<p><font face="MS Sans Serif" size="-1"><b>idFirstChild</b> is the ID of the <b>first</b>
MDI child window. Windows increments the ID for each new MDI child window the
application created. For example, if you pass 100 to this field, the first MDI
child window will have the ID of 100, the second one will have the ID of 101
and so on. This ID is sent to the frame window via WM_COMMAND when the MDI child
window is selected from the window list. Normally you'll pass this "unhandled"
WM_COMMAND messages to DefFrameProc. I use the word "unhandled" because
the menuitems in the window list are not created by your application thus your
application doesn't know their IDs and doesn't have the handler for them. This
is another special case for the MDI frame window: if you have the window list,
you must modify your WM_COMMAND handler a bit like this:</font></p>
<blockquote>
<pre><b><font face="MS Sans Serif" size="-1">.elseif uMsg==WM_COMMAND<br> .if lParam==0 ; this message is generated from a menu
mov eax,wParam<br> .if ax==IDM_CASCADE<br> .....<br> .elseif ax==IDM_TILEVERT<br> .....
.else<br><font color="#CC0033"> invoke DefFrameProc, hwndFrame, hwndClient, uMsg,wParam, lParam<br> ret</font><br> .endif</font></b></pre>
</blockquote>
<p><font face="MS Sans Serif" size="-1">Normally, you would just ignore the messages
from unhandled cases. But In the MDI case, if you ignore them, when the user
clicks on the name of an MDI child window in the window list, that window won't
become active. You need to pass them to <font color="#003399"><b>DefFrameProc</b></font>
so they can be handled properly.</font></p>
<p><font face="MS Sans Serif" size="-1">A caution on the value of <b>idFirstChild</b>:
you should not use 0. Your window list will not behave properly, ie. the check
mark will not appear in front of the name of the first MDI child even though
it's active. Choose a safe value such as 100 or above.</font></p>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -