朗朗空间

我知道并不是 所有鸟儿都飞翔

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  16 Posts :: 0 Stories :: 6 Comments :: 0 Trackbacks

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜

来自:http://www.relisoft.com/win32/winnie.html
译:聂元朗
1. The Simplest Windows Program
1.最简单的Windows程序

Before you can even begin thinking about programming in Windows, you have to be able to understand how this simple program works.

在你开始想如何在windows下编程之前,你必须要弄清楚下面这个简单windows程序的工作原理。

Windows API calls are highlighted in blue and Windows specific data types are shown in green. I will also usually put a double colon in front of API calls. In C++, that simply means that I'm calling a global function, in case there is some ambiguity.

我用蓝色来标识Windows的API调用,用绿色来标识Windows中的特殊的数据类型。同时我在API函数的前面放置了一对冒号::。在C++中,这对冒号就是告诉编译器我在调用一个全局函数。由于有时候有同名的函数,为了不引起混淆,在前面放置冒号是一种很好的做法

Sources (zipped file 4k) are right here. Remember to compile them as a Windows application. For instance, in Visual C++ select File.New.Projects.Win32 Application. Otherwise you'll get the error: unresolved external _main. (I provided project file Winnie.dsp for those of you who use MS VC++ 6.0 and Winnie.sln for the users of VC++ 7.0)

我提供了源代码(一个4k大小的压缩文件)。请记住要把它们编译成一个windows应用程序而不是windows控制台程序。否则,你就会得到找不到main函数的错误。在VC++6.0中,我们可以通过选择File——New Projects——Win32 Application来创建一个windows应用程序的工程。当然,你用不着这么担心,我已经提供了两个做好的工程文件给你。一个是winnie.dsp,你可以在VC++6.0下使用。一个是Winnie.sln,你可以在VC++7.0下使用。

First, in a Windows program, you have to define a Window Class, the "class" of window(s) that will be displayed by your application (not a C++ class). In our case we will display only one window, but still, we need to give Windows some minimal information about its Class. The most important part of the WinClass (now, that's a C++ class that describes the Window Class) is the address of the callback procedure, or the Window Procedure. Windows is supposed to call us--Windows sends messages to our program by calling this procedure.

好了,让我们来看看如何写这个最简单的程序吧。首先,在一个windows程序里面,你需要定义一个窗口类,注意这个类的概念不同于C++中的类,这个窗口类相当于C语言中的结构体,你把这个结构体填好了,然后你的应用程序就会根据你填的这些信息来显示窗口。在我们的例子里面,我们仅仅显示一个窗口。但是就算是这样,我们也必须要给我们的窗口最少的窗口类信息。而其中最重要的部分就是回调函数了,有时候我们也称它为窗口过程。其实这是一个函数指针,每次程序需要处理消息的时候,windows就通过这个函数指针来调用我们写好的回调函数。是的,你看到,是windows调用我们编写的函数。你还记得函数指针吗?如果不是很清楚,可以google一下,o(∩_∩)o…

Notice the declaration of WindowProcedure. Windows calls it with a handle to the window in question, the message, and two data items associated with the message, the paramters, WPARAM and LPARAM.

让我们看看WindowProcedure(窗口过程、回调函数)的定义。仔细看一下,第一个函数是一个句柄,也就是个整数。Windows用它来区别是哪个窗口。然后是消息,然后是与消息关系密切的两个参数WPARAM和LPARAM。这个我本来想讲一下,留到以后吧,毕竟原文中在这里没有讲。

In WinClass we also have to specify things like the program instance handle HINSTANCE, the mouse cursor (we just load the standard arrow cursor), the brush to paint the window's background (we chose the default window color brush), and the symbolic name of our class (you don't have to understand the meaning of all those yet).

在下面的WinClass类中我们必须给一些窗口类的字段赋值,如窗口的实例句柄HINSTACE,鼠标光标(我们仅仅加载了标准的箭头鼠标),画窗口背景的画刷(我们用的是窗口缺省的背景颜色画刷),最后还有窗口类的名字(窗口类的名字最好和别人的不同).(当然,现在你不需要弄明白这些参数的意义)

Once all the fields of WNDCLASS are filled, we register the class with the Windows system.

一旦我们把WNDCLASS结构体填完了,我们就通过调用RegisterClass函数在windows系统中注册我们的窗口类。好了,先看看我是怎样封装WNDCLASS到一个WinClass类中去的。

#include <windows.h>
LRESULT CALLBACK WindowProcedure
    (HWND hwnd, unsigned int message, WPARAM wParam, LPARAM lParam);
class WinClass
{
public:
    WinClass (WNDPROC winProc, char const * className, HINSTANCE hInst);
    void Register ()
    {
        ::RegisterClass (&_class);//now we don’t deal with the error
    }//we will process the error in the next version
private:
    WNDCLASS _class;//define a private WNDCLASS object member
};

WinClass::WinClass
    (WNDPROC winProc, char const * className, HINSTANCE hInst)
{
    _class.style = 0;
    _class.lpfnWndProc = winProc; // window procedure: mandatory
    _class.cbClsExtra = 0;
    _class.cbWndExtra = 0;
    _class.hInstance = hInst;         // owner of the class: mandatory
    _class.hIcon = 0;
    _class.hCursor = ::LoadCursor (0, IDC_ARROW); // optional
    _class.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // optional
    _class.lpszMenuName = 0;
    _class.lpszClassName = className; // mandatory
}
Once the Window Class is registered, we can proceed with the creation of a window. This is done by calling the CreateWindow API. It takes a lot of arguments: the name of the window class that we have just registered, the caption that will appear in the title bar, window style, position, size, and the application instance. The rest of the arguments, for the time being, will be left equal to zero.

一旦窗口类注册成功,我们就可以用它来创建一个窗口。我们通过调用CreateWindow这个API函数来完成它。它有很多参数,这可能让你有点烦,不过不要进,你只要写一次就好了。这些参数包括:我们刚刚注册好的窗口类的名字(这个要是搞错了,你的窗口可能就出不来了),我们窗口的标题栏上的标题,窗口的样式,位置,大小还有应用程序的实例句柄。剩下的参数暂时我们还不需要用到,我们把它们初始化为0。

This part of the program can also be encapsulated into a C++ class, WinMaker.

程序的这部分也被我封装到了一个C++的类WinMaker当中。

The window will not appear on the screen until you tell Windows to show it.

就算你创建好了窗口,但你还是看不到,你必须调用ShowWindow函数让它显示。
class WinMaker
{
public:
    WinMaker (): _hwnd (0) {}
    WinMaker (char const * caption, 
              char const * className,
              HINSTANCE hInstance);
    void Show (int cmdShow)
    {
        ::ShowWindow (_hwnd, cmdShow);
        ::UpdateWindow (_hwnd);
    }
protected:
    HWND _hwnd;
};
WinMaker::WinMaker (char const * caption, 
                    char const * className,
                    HINSTANCE hInstance)
{
    _hwnd = ::CreateWindow (
        className,            // name of a registered window class
        caption,              // window caption
        WS_OVERLAPPEDWINDOW,  // window style
        CW_USEDEFAULT,        // x position
        CW_USEDEFAULT,        // y position
        CW_USEDEFAULT,        // witdh
        CW_USEDEFAULT,        // height
        0,                    // handle to parent window
        0,                    // handle to menu
        hInstance,            // application instance
        0);                   // window creation data
}
A Windows program is event-driven. It means that you, as a programmer, are supposed to be on the defensive. The user will bombard Windows with various input actions, and Windows will bombard your program with messages corresponding to these actions. All you have to do is to respond to these messages. The picture below shows schematically how it works.

Windows程序是事件驱动的。这就是说你作为一个windows程序员,所做的应该是防御式编程。其实也没有这么严重。具体来说就是,用户会发给windows多种输入行为,而windows把这些输入用消息的方式发给你的程序。而你,所做的就是针对每一个消息在回调函数中做出处理。下面的这张图示意性的说明了这个过程。图在下一页的下一页,o(∩_∩)o…

Windows gets various events from the keyboard, the mouse, the ports, etc. Each event is quickly converted into a message. Windows (the operating system) dispatches messages to appropriate windows. For instance, all keyboard messages go to the window that currently has the input focus (the active window). Mouse messages are dispatched according to the position of the mouse cursor. They usually go to the window that is directly under the cursor (unless some program captured the mouse).

首先,windows系统从键盘、鼠标、端口等等各种设备中获得各种事件。每个事件被快速的转换为消息。Windows分派这些消息给合适的窗口。举个例子,所有的键盘消息都会传给当前具有输入焦点的窗口(也就是所谓的活动窗口)。鼠标消息则会根据鼠标光标所在的位置分派给相应的窗口。这些窗口通常是直接位于光标下的第一个窗口(除非有某些程序捕获鼠标消息)。

All these messages end up in message queues. Windows keeps a message queue for every running application (actually, for every thread). It is your duty to retrieve these messages one-by-one in what is called a message loop. Your program has to call GetMessage to retrieve a message. Then you call DispatchMessage to give it back to Windows. Couldn't Windows just go ahead and dispatch all these messages itself? In principle it could, but a message loop gives your program a chance to have a peek at them and maybe perform some additional actions before dispatching them. Or not...

所有的消息都放在消息队列里面。Windows为每一个运行的应用程序维护一个消息队列(实际上,是为每一个线程)。把这些消息一个个的取出来,这是你的责任。通常我们把这段代码叫做消息循环。在你的程序里,首先调用GetMessage函数取出一条消息,然后调用DispatchMessage把它返回给Windows。Windows还会重新分派这些消息吗?原则上不会,但是你可以在消息循环中使用PeekMessage函数来查看消息而不分派它,因为某些情况下,你需要在分派这些消息之前做一些额外的工作。

Each message is addressed to a particular window. When you tell Windows to dispatch such a message, it will figure out the class of this window, find the associated Window Procedure, and call it. Every single message sent to our window will end up in our window procedure. It is now up to us to respond to it. So, do we have to respond appropriately to every possible type of Windows message? There a hundreds of them! Fortunately, no! We only need to intercept those messages that we are interested in. Everything else we pass back to Windows for default processing using DefWindowProc.

每个消息被传递到一个特殊的窗口。当你告诉windows分派一条信息的时候,windows会先找出这个窗口的类,然后找到与之关联的窗口过程,最后调用这个窗口过程。每个发送到我们窗口的消息都会在我们的窗口过程中终止。现在轮到我们来处理这些消息了。因此,我们必须要处理所有的消息类型?不,这里有几百种消息,我们只需要处理对我们有用的消息就可以了。但其它消息怎么办呢?不用担心,其它消息我们通过调用DefWindowProc这个函数来自动处理。

Let's have a look at WinMain. The execution of a Windows program doesn't start in main--it starts in WinMain. In our WinMain, we create a WinClass and register it. Then we create an actual window (of the class we've just registered) and show it. Actually, WinMain is called with the appropriate show directive--the user might want to start the application minimized or maximized. So we just follow this directive. Next, we enter the message loop and keep retrieving and dispatching messages until GetMessage returns 0. At that point the message's wParam will contain the return code of the whole program.

让我们看看WinMain这个函数。一个windows函数的开始不是main函数,而是WinMain。在我们的winMain函数里,我们创建了一个WinClass的对象winClass,然后我们在windows中注册了它。接下来我们创建了一个窗口(我们刚刚注册的窗口类所对应的窗口)并且显示它。实际上,WinMain由一个参数是用来指明窗口显示效果的(这样,用户可以指定窗口显示的方式)——用户可能希望打开这个程序用最小化或者最大化的方式打开,因此,我们在这里只需要直接使用这个参数就可以了。然后我们进入消息循环并不断取出消息和进行分派,直到GetMessage函数返回0。这个时候消息的wParam参数将会包含整个程序的返回值。
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
                    char * cmdParam, int cmdShow)
{
    char className [] = "Winnie";
    WinClass winClass (WindowProcedure, className, hInst);
    winClass.Register ();
    WinMaker win ("Hello Windows!", className, hInst);
    win.Show (cmdShow);
    
MSG msg; int status; while ((status = ::GetMessage (& msg, 0, 0, 0)) != 0) { if (status == -1) return -1; ::DispatchMessage (& msg); }
return msg.wParam; }
The GetMessage API is an interesting example of the bizarre Microsoft Troolean (as opposed to traditional, Boolean) logic. GetMessage is defined to return a BOOL, but the documentation specifies three types of returns, non-zero, zero and -1. I am not making it up! Here's an excerpt from the help file:

GetMessage函数是一个很好笑的微软的三值布尔类型的例子。GetMessage函数定义的返回值是BOOL类型,但是文档却明确指出它含有三种返回值,非0、0和-1。我不想在这里多费口舌。下面是从MSDN中的摘录。

· If the function retrieves a message other than WM_QUIT, the return value is nonzero.
· If the function retrieves the WM_QUIT message, the return value is zero.
· If there is an error, the return value is -1.


The other important part of every Windows program is the Windows Procedure. Remember, Windows will call it with all kinds of messages. All these messages can be ignored by sending them to DefWindowProc. There is only one message that we must intercept. That's the WM_DESTROY message that is sent by Windows when the user decides to close the window (by pressing the close button in the title bar). The standard response to WM_DESTROY is to post the quit message and return zero. That's all there is to it.

让我们看看所有windows程序都很重要的回调函数吧。你需要注意的是,windows将为所有的消息调用这个函数。所以剩余的消息一定要送给DefWindowProc去处理。在这里我们只处理了一个消息。那就是WM_DESTROY。当用户要关闭一个窗口的时候,windows会发送这个消息。标准的处理WM_DESTROY消息是发送一条退出消息,并返回0。好了,这就是我们要讲的全部了。*_*

// Window Procedure called by Windows
LRESULT CALLBACK WindowProcedure
    (HWND hwnd, unsigned int message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_DESTROY:
            ::PostQuitMessage (0);
            return 0;
    }
    return ::DefWindowProc (hwnd, message, wParam, lParam );
}
posted on 2008-09-23 11:01 聂元朗 阅读(2230) 评论(4)  编辑 收藏 引用 所属分类: windows程序学习笔记

Feedback

# re: 翻译-C++最简单的windows程序 The Simplest Windows Program 2008-09-23 17:45 adcx
练习英文翻译?否则这种文章翻译没什么价值的  回复  更多评论
  

# re: 翻译-C++最简单的windows程序 The Simplest Windows Program 2008-09-23 21:45 聂元朗
@adcx
你懂了所以不需要了,但我没懂所以需要!明白?  回复  更多评论
  

# re: 翻译-C++最简单的windows程序 The Simplest Windows Program 2008-09-25 14:48 Barrys
@adcx
贵在积累嘛  回复  更多评论
  

# re: 翻译-C++最简单的windows程序 The Simplest Windows Program 2008-09-25 15:30 肥仔
翻译这个,有意思吗?  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理