zoukankan      html  css  js  c++  java
  • 函数参数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编程,更出色的知识还在后头呢~

    文章结束给大家分享下程序员的一些笑话语录: 打赌
    飞机上,一位工程师和一位程序员坐在一起。程序员问工程师是否乐意和他一起玩一种有趣的游戏。工程师想睡觉,于是他很有礼貌地拒绝了,转身要睡觉。程序员坚持要玩并解释说这是一个非常有趣的游戏:"我问你一个问题,如果你不知道答案,我付你5美元。然后你问我一个问题,如果我答不上来,我付你5美元。"然而,工程师又很有礼貌地拒绝了,又要去睡觉。  程序员这时有些着急了,他说:"好吧,如果你不知道答案,你付5美元;如果我不知道答案,我付50美元。"果然,这的确起了作用,工程师答应了。程序员就问:"从地球到月球有多远?"工程师一句话也没有说,给了程序员5美元。  现在轮到工程师了,他问程序员:"什么上山时有三条腿,下山却有四条腿?"程序员很吃惊地看着工程师,拿出他的便携式电脑,查找里面的资料,过了半个小时,他叫醒工程师并给了工程师50美元。工程师很礼貌地接过钱又要去睡觉。程序员有些恼怒,问:"那么答案是什么呢?"工程师什么也没有说,掏出钱包,拿出5美元给程序员,转身就去睡觉了。

  • 相关阅读:
    大数据集群迁移的那一夜是怎么过的
    什么是Lambda架构
    从0到1搭建自助分析平台
    业务重要?还是技术重要?
    如何从0到1搭建大数据平台
    从0到1搭建大数据平台之数据采集系统
    高频面试题:秒杀场景设计
    面试官:面对千万级、亿级流量怎么处理?
    来自朋友最近阿里、腾讯、美团等P7岗位面试题
    《我想进大厂》之JVM夺命连环10问
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3093582.html
Copyright © 2011-2022 走看看