Windows程序设计_Chap01_起步_学习笔记

Windows程序设计_Chap01_起步_学习笔记

――By: Neicole(2013.05.21)

 

01. 开篇

 

    今天是2013.05.20,开始学习《windows程序设计》这书,期间,会借助MSDN和网络资料展开学习。书本第一章,简要介绍了Windows的历史,简要提到动态链接库的概念,讲述Windows编程的准备工作,还有最重要的第一个Windows程序。这次学习笔记以翻译MSDN为主,目的是学会使用两个最基本的WindowsAPI函数。

 

02. WindowsAPI重要的头文件(动态链接库)

 

  02.01 内核WINBASE.H (KERNEL32.DLL) 负责操作系统的五个基本工作:进程管理、存储管理、设备管理、文件管理、操作系统接口。

  02.02 用户WINUSER.H (USER32.DLL) 用户界面,负责所有的窗口管理。

  02.03 图形设备接口(GDI)WINGDI.H(GDI32.DLL) 负责在屏幕或打印机显示文本与图形。

  02.04 基本数据类型定义WINDEF.H

  02.05 支持Unicode的类型定义WINNT.H

 

03. 我的第一个Windows程序

 

03.01 效果演示

 

03.02 源代码

/**
 * 文件:HahaWorld.cpp
 * 简介:第一个WindowsAPI实现的程序,显示一个MessageBox。
 * IDE:VS2010 SP1
 * 制作者:Neicole
 * 联系方式:http://blog.csdn.net/neicole
 **/

#include <Windows.h>

int WINAPI 	WinMain	(HINSTANCE hInstance,	HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	MessageBox(NULL, TEXT("HaHa World!"), TEXT("HahaMsg"), MB_DEFBUTTON1|MB_YESNO);
	return 0;
}

 

04. 程序解释

 

04.01 WinMain函数

  主要使用了WinMain函数作为windows程序入口。经过查阅MSDN得到关于WinMain的介绍(以下内容个人理解翻译,如有错误,欢迎指出希望能给出更正意见):

    Every Windowsprogram includes an entry-point function that is named either WinMain orwWinMain.

    一个空的windows程序包含了一个入口函数WinMain或者wWinMain。

 

 04.01.01 参数介绍

    hInstance is something called a "handle to an instance" or "handle to a module." The operating system uses this value to identify the executable (EXE) when it is loaded in memory. The instance handle is needed for certain Windows functions — for example, to load icons or bitmaps.

    hInstance叫“实例句柄”或者“模块句柄”。操作系统使用这个句柄的值去定义加载到内存的构件。使用像加载图标或者位图这类窗口函数需要用上实例句柄。
    hPrevInstance has no meaning. It was used in 16-bit Windows, but is now always zero.
    hPrevInstance没有含义。它以前被用于16位系统,但现在常常为零。
    pCmdLine contains the command-line arguments as a Unicode string.
    pCmdLine包含程序的(Unicode编程字符串)命令行参数。
    nCmdShow is a flag that says whether the main application window will be minimized, maximized, or shown normally.
    nCmdShow是一个说明主应用窗口如何显示的标志,如最小化,最大化或者正常显示。

04.01.02 返回值

    The function returns an int value. The return value is not used by the operating system, but you can use the return value to convey a status code to some other program that you write.
    这个函数返回一个int类型的值。这返回值不被操作系统使用,但是你可以使用返回值去传达一个状态码到一些你写的程序。
    WINAPI is the calling convention. A calling convention defines how a function receives parameters from the caller. For example, it defines the order that parameters appear on the stack. Just make sure to declare your wWinMain function as shown.
    WINAPI是一个调用约定。定义了函数接收参数的方式。例如,它定义了参数的压栈顺序(从右到左),这样只是确认可以使你的wWinMain函数正常显示。

 

04.01.03 备注

    The WinMain function is identical to wWinMain, except the command-line arguments are passed as an ANSI string. The Unicode version is preferred. You can use the ANSI WinMain function even if you compile your program as Unicode. To get a Unicode copy of the command-line arguments, call the GetCommandLine function. This function returns all of the arguments in a single string. If you want the arguments as an argv-style array, pass this string to CommandLineToArgvW.
    WinMain函数与wWinMain除了命令行pCmdLine参数是使用ANSI字符串外其它完全相同, Unicode版本优先使用。当你的程序用Unicode编译时,你也可以使用ANSI命令行参数。获得Unicode命令行的副本,调用GetCommandLine函数。这个函数将这些参数返回唯一的字符串。如果你想这些参数是命令行参数风格数组,将这字符串作为参数执行CommandLine函数。
    How does the compiler know to invoke wWinMain instead of the standard main function? What actually happens is that the Microsoft C runtime library (CRT) provides an implementation of main that calls either WinMain or wWinMain.
    编译器如何知道调用wWinMain来代替标准的main函数?这是由于微软的C的标准运行时库(CRT)提供了WinMain或者wWinMain的函数调用接口。
    Note:The CRT does some additional work inside main. For example, any static initializers are called before wWinMain. Although you can tell the linker to use a different entry-point function, use the default if you link to the CRT. Otherwise, the CRT initialization code will be skipped, with unpredictable results. (For example, global objects will not be initialized correctly.)
    注意,标准运行时库会在main里做一些额外的操作。例如,一些静态初始化对象会在wWinMain执行之前初始化。如果你连接到CRT,你会使用默认的入口函数,即使你可以告诉键接者去使用不同的入口函数。否则,CRT初始化对象代码会被跳过,可能会出现不可预知的结果。(例如,全部变量不能正确地被初始化。)

 
04.02 Message函数

  int MessageBox(HWND hWnd,LPCTSTR lpText,LPCTSTR lpCaption,UINT UType);
    这是在Windows编程里面常常用上的一个函数,正如在Win32控制台编程时常用cout一样。

 

04.02.01 参数介绍


    hWnd  [in] Handle to the owner window of the message box to be created. If this parameter is NULL, the message box has no owner window.
    消息框的父窗口句柄。如果这个参数为NULL,则消息框没有父窗口。
    lpText  [in] Pointer to a null-terminated string that contains the message to be displayed.
    指向一个以NULL结尾、含有将被显示消息的字符串的指针。
    lpCaption  [in] Pointer to a null-terminated string that contains the dialog box title. If this parameter is NULL, the default title Error is used.
    指向一个以NULL结尾的、用于对话框标题的字符串的指针。如果参数为空,则默认标题错误会被使用。
    uType  [in] Specifies the contents and behavior of the dialog box. This parameter can be a combination of flags from the following groups of flags.
    指定对话框的内容和行为的位标志集。这个参数可以为下列标志组中标志的组合。(这里不一一列出)

 

04.02.02 返回值

    If a message box has a Cancel button, the function returns the IDCANCEL value if either the ESC key is pressed or the Cancel button is selected. If the message box has no Cancel button, pressing ESC has no effect.
    如果消息框有取消按钮,如果ESC键被按下或者取消按钮被选中,函数返回IDCANCEL值。如果消息框没有消息按钮,按下ESC键将不会产生效果。
    If the function fails, the return value is zero. To get extended error information, call GetLastError.
    如果函数失败,返回值为0。调用GetLastError能获得更详细的错误信息。
    If the function succeeds, the return value is one of the following menu-item values.
    如果函数成功,返回值将会是下列列表中的其中一个。(这里不一一列出)

 

04.02.03 备注

    Adding two right-to-left marks (RLMs), represented by Unicode formatting character U+200F, in the beginning of a MessageBox display string is interpreted by the Win32 MessageBox rendering engine so as to cause the reading order of the MessageBox to be rendered as right-to-left (RTL).
    增加两个从右到左的标志(RLMs)为代表的Unicode格式字符U+200F,在开始的一个消息框显示字符串被Win32弹出窗口渲染引擎解读,使对话框的阅读顺序被显示为从右到左(RTL)。
    When you use a system-modal message box to indicate that the system is low on memory, the strings pointed to by the lpText and lpCaption parameters should not be taken from a resource file because an attempt to load the resource may fail.
    当你使用一个系统模态消息框来指示系统是低内存,这个字符串所指向的lpText和lpCaption参数不能来自资源文件,这是因为试图加载资源可能会失败。
    If you create a message box while a dialog box is present, use a handle to the dialog box as the hWnd parameter. The hWnd parameter should not identify a child window, such as a control in a dialog box.
    如果在一个对话框出现时你创建一个消息框,使用对话框的句柄作为参数。这个句柄参数不能作为一个子窗口,正如对话框里的控件。

 

05. 结尾语


    第一个Windows API程序出来啦啦啦~ 使用HahaWorld代替了HelloWorld,这是因为,Haha这词,好像显得更加有活力。现在只是开篇,Windows API编程,更精彩的知识还在后头呢~

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值