本文翻译自msdn,由于本人英文水平一般,如果有什么差错,请大家积极指出,本人一定知错就改.
原文链接:http://msdn.microsoft.com/library/en-us/winprog/winprog/a_generic_sample_application.asp 【程序编程相关:Visual C++.NET编程基础讲座】 【推荐阅读:华为进攻(宽带城域网引发的华为营销策略分】本文适合于win32 api的初学者 【扩展信息:面向对象的思维方法】 a generic sample application 一个generic实例应用程序this section introduces the source code components of a generic windows-based application, named generic. this section assumes that you have used windows-based applications and therefore are already familiar with windows, menus, and dialog boxes in microsoft? windows?.
这个部分介绍一个基于windows的应用程序的源代码的组成部分,我们这个程序叫做generic.我们假定你已经使用过基于windows的应用程序,因此你应该对windows中的窗口.菜单与对话框都比较熟悉.the generic application described here consists of the following parts:
这里讲述的generic应用程序由以下部分组成:the entry-point function
入口函数 the menu 菜单 the window procedure 窗口过程 the about dialog box about对话框the complete code is shown in source code.
完整的代码被列在“source code”这个部分.
the entry-point function
入口函数every application must have an entry-point function. the name commonly given to the entry point is the winmain function.
每个应用程序都必须有一个入口函数.通常入口点的名称是winmain.as in most windows-based applications, the winmain function for the generic application completes the following steps:
与大部分基于windows的应用程序一样,generic应用程序中的winmain函数都完成以下一些步骤:registering the window class
注册窗口类 creating the main window 创建主窗口 entering the message loop 进入消息循环
registering the window class
注册窗口类every window must have a window class. a window class defines the attributes of a window, such as its style, its icon, its cursor, the name of the menu, and the name of the window procedure.
每个窗口都必须有一个窗口类.这个窗口类定义了一个窗口的属性,例如它的风格.图标.光标指针.菜单名与窗口过程的名称.the first step is to fill in a wndclass structure with class information. next, you pass the structures to the registerclass function. the generic application registers the genericappclass window class as follows:
第一步是用类的信息来填充一个wndclass结构.下一步,将这个结构传递给registerclass函数.generic应用程序依照下列代码来注册genericappclass窗口类: hinstance hinstance; wndclass wc;wc.lpszclassname = "genericappclass";
wc.lpfnwndproc = mainwndproc; wc.style = cs_owndc | cs_vredraw | cs_hredraw; wc.hinstance = hinstance; wc.hicon = loadicon(null, idi_application); wc.hcursor = loadcursor(null, idc_arrow); wc.hbrbackground = (hbrush)(color_window + 1); wc.lpszmenuname = "genericappmenu"; wc.cbclsextra = 0; wc.cbwndextra = 0;registerclass(&wc);
for more information on the menu, see the menu. for more information on the window procedure, see the window procedure. 至于菜单方面的更多信息,请参考“the menu”这个部分.而关于窗口过程方面的更多信息,请参考“the window procedure”.
creating the main window
创建主窗口you can create the window by calling the createwindow function. the generic application creates the window as follows:
你可以通过调用createwindow函数来创建窗口.generic应用程序依照下列代码来创建窗口: hwnd = createwindow("genericappclass", "generic application", ws_overlappedwindow | ws_hscroll | ws_vscroll, 0, 0, cw_usedefault, cw_usedefault, null, null, hinstance, null ); the first parameter is the name of the class that we registered. the remaining parameters specify other window attributes. this call creates the window, but the system does not display a window until the application calls the showwindow function. the generic application displays the window as follows: 第一个参数是我们已注册的窗口类的名称.剩下的参数用来指定其它的窗口属性.这个函数调用创建一个窗口,但是系统不会显示它,除非你再调用showwindow这个函数.generic应用程序依照下列代码来在屏幕上显示该窗口: showwindow( hwnd, ncmdshow );
entering the message loop
进入消息循环once the main window is created and displayed, the winmain function can begin its primary task, which is to read messages from the application queue and dispatch them to the appropriate window.
一旦主窗口已被创建并显示,winmain函数就可以开始它的主要任务,那就是从应用程序的消息队列中读取消息并把它发送到合适的窗口中去.the system does not send input directly to an application. instead, it places all mouse and keyboard input from the user into a message queue, along with messages posted by the system and other applications. the application must read the message queue, retrieve the messages, and dispatch them so that the window procedure can process them.
系统不会将输入直接发送到一个应用程序.... 下一页