⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1hello.html

📁 Visual C++ has been one of most effective tool for the large industrial applications. This book is t
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html>
<head>

<title>Windows Techniques</title>
    <meta  name="description" content="Introduction to Windows programming">
    <meta name="keywords" content="windows, programming, window, class, message loop">
<link rel="stylesheet" href="rs.css" tppabs="http://www.relisoft.com/book/rs.css">
</head>
<body background="margin.gif" tppabs="http://www.relisoft.com/book/images/margin.gif" bgcolor="#ffffe0">

<!-- Main Table -->
<table cellpadding="6">
    <tr>
    <td width="78">
    &nbsp;
    <td>

<h3>Programming Paradigm</h3>

<p>A Windows program, like any other interactive program, is for the most part input-driven. However, the input of a Windows program is conveniently pre-digested by the system. When the user hits the keyboard or moves the mouse, Windows intercepts such an event, pre-processes it and dispatches it to the appropriate user program. The program gets all its messages from Windows. It may do something about them, or not; in the latter case it lets Windows do the &quot;right&quot; thing (do the default processing).

<p>When a Windows program starts for the first time, it registers a <b><i>Windows class</i></b> (it抯 not a C++ class) with the system. Through this class data structure it gives the system a pointer to a callback function called the <b><i>Window Procedure</i></b>. Windows will call this procedure whenever it wants to pass a message to the program, to notify it of interesting events. The name "callback" means just this: we don't call Windows, Windows will call us back. 

<p>The program also gets a peek at every message in the <b><i>message loop</i></b> before it gets dispatched to the appropriate Windows Procedure. In most cases the <b><i>message loop</i></b> just forwards the message back to Windows to do the dispatching.

<p align="CENTER"><img src="Image1-1.gif" tppabs="http://www.relisoft.com/book/win/images/Image1.gif" width=474 height=258>
<p>Figure. Input driven Windows paradigm


<p>Windows is a multi-tasking operating system. There may be many programs running at the same time. So how does Windows know which program should get a particular message? Mouse messages, for instance, are usually dispatched to the application that created the window over which the mouse cursor is positioned at a given moment (unless an application "captures" the mouse). 

<p>Most Windows programs create one or more <b><i>windows</i></b> on the screen. At any given time, one of these windows has the <b><i>focus</i></b> and is considered <b><i>active</i></b> (its title bar is usually highlighted). Keyboard messages are sent to the window that has the focus.

<p>Events such as resizing, maximizing, minimizing, covering or uncovering a window are handled by Windows, although the concerned program that owns the window also gets a chance to process messages for these events. There are dozens and dozens of types of messages that can be sent to a Windows program. Each program handles the ones that it's interested in.
<p>Windows programs use Windows services to output text or graphics to the screen. Windows not only provides high level graphical interface, but it separates the program from the actual graphical hardware. In this sense Windows graphics is device independent.
<p>It is very easy for a Windows program to use standard Windows controls. <b><i>Menus</i></b> are easy to create, so are message boxes. Dialog boxes are more general--they can be designed by a programmer using a <b><i>Dialog Editor</i></b>, icons may be created using an Icon Editor. List boxes, edit controls, scroll bars, buttons, radio buttons, check boxes, etc., are all examples of built-in ready to use controls that make Windows programs so attractive and usable.

<p>All this functionality if available to the programmer through Windows API. It is a (very large) set of C functions, typedefs, structures and macros whose declarations are included in &lt;windows.h&gt; and whose code is linked to your program through a set of libraries and DLLs (dynamic-load libraries).

<h3>Hello Windows!</h3>

<p>The simplest Windows program does nothing more but to create a window with the title &quot;Hello Windows!&quot; It is definitely more complicated than the Kernighan and Ritchie抯 &quot;Hello world!&quot; and more complicated than our first &quot;Hello!&quot; C++ program. However, what we are getting here is much more than the simple old-fashioned teletype output. We are creating a window that can be moved around, resized, minimized, maximized, overlapped by other windows, etc. It also has a standard system menu in the upper left corner. So let抯 not complain too much!

<p>In Windows, the main procedure is called <var>WinMain</var>. It must use the <var>WINAPI</var> calling convention and it is called by the system with the following parameters:
<ul>
<li><var>HINSTANCE hInst</var>--the handle to the current instance of the program
<li><var>HINSTANCE hPrevInst</var>--obsolete in Win32
<li><var>LPSTR cmdParam</var>--a string with the command line arguments
<li><var>int cmdShow</var>--a flag that says whether to show the main window or not.
</ul>
<p>Notice the strange type names. You'll have to get used to them--Windows is full of typedefs. In fact, you will rarely see an <var>int</var> or a <var>char</var> in the description of Windows API. For now, it's enough to know that <var>LPSTR</var> is in fact a typedef for a <var>char *</var> (the abbreviation stands for Long Pointer to STRing, where string is a null terminated array and "long pointer" is a fossil left over from the times of 16-bit Windows).

<p>In what follows, I will consequently prefix all Windows API functions with a double colon. A double colon simply means that it's a globally defined function (not a member of any class or namespace). It is somehow redundant, but makes the code more readable.

<!-- Code --><table width=100% cellspacing=10><tr>    <td class=codetable>
<pre>int WINAPI WinMain
   (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdParam, int cmdShow)
{
    char className [] = "Winnie";
    WinClassMaker winClass (WinProcedure, className, hInst);
    winClass.Register ();
    WinMaker maker (className, hInst);
    Window win = maker.Create ("Hello Windows!");
    win.Display (cmdShow);

    // Message loop
    MSG  msg;
    int status;
    while ((status = ::GetMessage (&amp; msg, 0, 0, 0)) != 0)
    {
        if (status == -1)
            return -1;
        ::DispatchMessage (&amp; msg);
    }
    return msg.wParam;
}</pre>
</td></tr></table><!-- End Code -->


<p>First, we create a Window class and register it. Then we create a window with the caption &quot;Hello Windows!&quot; and display it (or not, depending on the flag we were given). Finally, we enter the message loop that waits for a message from Windows (the <var>::GetMessage</var> API) and then lets the system dispatch it. The message will come back to us when Windows calls our Window procedure, <var>WinProcedure</var>. For the time being we don抰 have to worry about the details of the message loop. The program normally terminates when <var>::GetMessage</var> returns zero. The <var>wParam</var> of the last message contains the return code of our program.

<!-- Sidebar -->
<table width=100% border=0 cellpadding=5><tr>
<td width=10>
<td bgcolor="#cccccc" class=sidebar>
<p>The <var>::GetMessage</var> function is an interesting example of three-state logic. It is defined to return the type <var>BOOL</var>, which is a typedef for <var>int</var>, not <var>bool</var> (in fact, there was no <var>bool</var> in C++, not to mention C, when Windows was first released). The documentation, however, specifies three types of returns, non-zero, zero and -1 (I am not making it up!). Here's the actual excerpt from the help file:
<ul>
<li>If the function retrieves a message other than <var>WM_QUIT</var>, the return value is <b>nonzero</b>. 
<li>If the function retrieves the <var>WM_QUIT</var> message, the return value is <b>zero</b>. 
<li>If there is an error, the return value is <b>-1</b>. 
</ul>
</table>
<!-- End Sidebar -->

<p>The prototypes of all Windows APIs and data structures are in the main inlcude file &lt;windows.h&gt;.

<p>The class <var>WinClassMaker</var> encapsulates the <var>WNDCLASS</var> data structure as well as the <var>::RegisterClass</var> API.

<!-- Code --><table width=100% cellspacing=10><tr>    <td class=codetable>
<pre>class WinClassMaker
{
public:
    WinClassMaker (WNDPROC WinProcedure, 
                   char const * className,
                   HINSTANCE hInst);
    void Register ()
    {
        if (::RegisterClassEx (&_class) == 0)
            throw "RegisterClass failed";
    }
private:
    WNDCLASSEX _class;
};</pre>
</td></tr></table><!-- End Code -->

<p>In the constructor of <var>WinClassMaker</var> we initialize all parameters to some sensible default values. For instance, we default the mouse cursor to an arrow, the brush (used by Windows to paint our window抯 background) to the default window color.
<p>We have to provide the pointer to the Window procedure, the name of the class and the handle to the instance that owns the class. 

<!-- Code --><table width=100% cellspacing=10><tr>    <td class=codetable>
<pre>WinClassMaker::WinClassMaker
    (WNDPROC WinProcedure, char const * className, HINSTANCE hInst)
{
    _class.lpfnWndProc = WinProcedure;// window procedure: mandatory
    _class.hInstance = hInst;         // owner of the class: mandatory
    _class.lpszClassName = className; // mandatory
    _class.cbSize = sizeof (WNDCLASSEX);
    _class.hCursor = ::LoadCursor (0, IDC_ARROW);
    _class.hbrBackground = reinterpret_cast&lt;HBRUSH&gt; (COLOR_WINDOW + 1);
    _class.style = 0;
    _class.cbClsExtra = 0;
    _class.cbWndExtra = 0;
    _class.hIcon = 0;
    _class.hIconSm = 0;
    _class.lpszMenuName = 0;
}</pre>
</td></tr></table><!-- End Code -->

<p>Class <var>WinMaker</var> initializes and stores all the parameters describing a particular window.
<!-- Code --><table width=100% cellspacing=10><tr>    <td class=codetable>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -