zoukankan      html  css  js  c++  java
  • 01 Windows编程——Hello World

     源码

    #include "stdafx.h"
    #include<Windows.h>
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE tmp,LPSTR strCmd,int nShow)
    {
        MessageBox(NULL, "Hello World!", "Title", 0);
    }
    View Code

    1 int MessageBox(
    2   HWND    hWnd,
    3   LPCTSTR lpText,
    4   LPCTSTR lpCaption,
    5   UINT    uType
    6 );

    hWnd:指向message box 的 owner window的句柄,如果为NULL,表明没有owner window,他的owner window就是桌面

    lpText:要显示消息的内容

    lpCaption:message box的标题,如果为NULL,则显示为默认值 Error

    uType:指定一个决定对话框的内容和行为的位标志集。

    报 类型参数不匹配 bug

    原因是字符集设置有问题,改成ASCII

    修改后不再报 类型参数不匹配,build一下,build失败

     原因是入口函数没有设置正确。WIndows窗体程序的入口函数是WinMain。控制台程序的入口函数是main。

    修改如下

    此时可以正常运行

     代码讲解

    Winddows窗体程序的入口函数是WinMain,这个函数定义如下

    int CALLBACK WinMain(
      _In_ HINSTANCE hInstance,
      _In_ HINSTANCE hPrevInstance,
      _In_ LPSTR     lpCmdLine,
      _In_ int       nCmdShow
    );
    View Code

    HINSTANCE这个类型是我们没见过的。F12逐层查看定义如下。##是连字符

    DECLARE_HANDLE(HINSTANCE);
    DECLARE_HANDLE(name) struct name##__{ int unused; }; typedef struct name##__ *name
    View Code

    宏展开后代码如下。可见HINSTANCE是一个指向结构体的指针。

    struct hinstance__ 
    { 
        int unused; 
    }; 
    typedef struct hinstance__ *hinstance;
    View Code
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE tmp,LPSTR strCmd,int nShow)

    HINSTANCE:执行当前应用程序实力的句柄

    hInst:该应用程序的基地址,指向当前程序实例的句柄

    tmp:这个值现在一直为NULL

    strCmd:除了命令名,命令后面的参数

    nShow:控制窗口如何被显示

     打印内存基地址源码

    #include "stdafx.h"
    #include<Windows.h>
    #include <stdio.h>
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE tmp,LPSTR strCmd,int nShow)
    {
        char str[100];
        sprintf(str,"0x%x",hInst);
        MessageBox(NULL, str, "内存基地址", 0);
    }
    View Code

    默认情况下VS 2017开启的是随机基地址,可以在属性->连接器->高级 下边设置

    属性->连接器->高级->入口点

    Windows窗体程序默认的入口点是WInMain,当让我们也可以修改成其他的字符

    build一下,报错,还需要设置子系统

    几乎所有的Windows窗体应用程序都需要包含 #include<Windows.h>

    Windows.h是一个最重要的包含文件,它囊括了若干其他Windows头文件,其中的某些头文件又包含另外的一些头文件。下列几个是最重要也是最基本的头文件:

    #include <windef.h>     基本数据类型定义。

    #include <winbase.h>  内核函数。

    #include <wingdi.h>     图形设备接口函数。

    #include <winuser.h>   用户界面函数。

  • 相关阅读:
    Eclipse安装Hadoop插件
    (转)Ubuntu14.0.4中hadoop2.4.0伪分布模式配置
    Hadoop--DataNode无法启动
    启动与关闭hadoop
    hadoop中执行命令时发生错误
    strings命令
    Deriving data from ElasticSearch Engine
    elasticsearch data importing
    reading words in your computer and changing to female voice, linux festival text2wave saving wav files
    DDNS client on a Linux machine
  • 原文地址:https://www.cnblogs.com/kelamoyujuzhen/p/9296786.html
Copyright © 2011-2022 走看看