创建 Win32 应用程序 (C++)

根据建立一个AppWizard应用程序六步骤建立一个名称为MyProg1的工程,然后在MyProg1.cpp中添加以下代码:

 

//因为应用程序代码必须使用现有的定义,所以应将 include 语句添加到文件中以使用它们
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include "stdafx.h"


//HINSTANCE hInst;

//在 Win32 应用程序中,每个应用程序必须具有一个 WinMain 函数
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow);


//除 WinMain 外,每个 Win32 应用程序还必须具有第二个函数(通常称为 WndProc),它代表窗口过程。
//此函数的用途是处理应用程序从操作系统接收的任何消息。应用程序何时从操作系统接收消息?始终接收!
//例如,假设我们创建了包含“确定”按钮的对话框。当用户单击该按钮时,操作系统向应用程序发送消息,
//使我们知道某位用户按下了此按钮。WndProc 函数负责响应该事件。在本示例中,适当的响应可能是关闭对话框。

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

//首先,在 WinMain 函数内部创建 WNDCLASSEX 类型的窗口类结构。
//此结构包含有关窗口的信息,如应用程序图标、窗口的背景色、在标题栏中显示的名称、窗口过程函数的名称等等。
//典型的 WNDCLASSEX 结构如下:

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
 static char szWindowClass[] ="MyProg1";//程序名
 HWND hwnd;                             // 窗口句柄
 MSG msg;                               //消息类型
    WNDCLASSEX wcex;                        //窗口类型        

 

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;//控制窗口的一些重要属性,CS_的常量在windows.h中定义
    wcex.lpfnWndProc    = WndProc;//是一个指向一个函数的指针,他与用这个新登记的窗口类创建的窗口有关
    wcex.cbClsExtra     = 0;//类的扩展域
    wcex.cbWndExtra     = 0;//窗口的扩展域
    wcex.hInstance      = hInstance;//使windows将窗口连接到正确的程序
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));//该应用程序所使用图表的句柄
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);//用作光标的位图的句柄
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);//背景刷
    wcex.lpszMenuName   = NULL;//窗口类所使用菜单类的名字
    wcex.lpszClassName  = szWindowClass;//窗口类所用的名字
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

 //现在已经创建了窗口类,接下来您必须注册它。使用 RegisterClassEx 函数,并将窗口类结构作为参数传递:
    if (RegisterClassEx(&wcex)==0)
    {
       /* MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);*/

        return 0;
    }

    //hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application does not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application

 //现在已经注册了您自己的类,接下来创建窗口。使用 CreateWindow 函数,如下所示:
    HWND hWnd = CreateWindow(
        szWindowClass,//注册名称
        szWindowClass,//窗口标题
        WS_OVERLAPPEDWINDOW,//窗口类型
        CW_USEDEFAULT, CW_USEDEFAULT,//窗口X.Y位置
        500, 100,//窗口X.Y长度
        NULL,//父窗口的句柄
        NULL,//窗口菜单的句柄
        hInstance,//当前窗口程序句柄
        NULL//所用参数
    );

    if (hWnd==0)
    {
        /*MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);*/

        return 0;
    }

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain

 //创建了窗口后,我们可以使用以下代码将其显示在屏幕上:
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);//UpdateWindow()调用将把一条WM_PAINT消息发送给应用程序,要求它重画窗口。

    // Main message loop:
   
 //WinMain 的最后一步是消息循环。此循环的用途是侦听操作系统发送的消息。
 //应用程序收到消息后,将该消息调度到 WndProc 函数,以便进行处理。消息循环类似于:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//WndProc 函数的用途是处理应用程序接收的消息。通常使用 Switch 函数实现此操作。

//我们将处理的第一个消息是 WM_PAINT 消息。当必须更新应用程序窗口的一部分时,应用程序会收到此消息。
//首次创建窗口时,必须更新整个窗口,并传递此消息以指示此操作。

//当处理 WM_PAINT 消息时,首先应做的是调用 BeginPaint,最后应做的是调用 EndPaint。
//在这两个函数调用之间,您可以处理所有的逻辑,以在窗口中排列文本、按钮和其他控件。
//对于此应用程序,我们在窗口中显示字符串“Hello, World!”。若要显示文本,请使用 TextOut 函数,如下所示:
//应用程序通常会处理许多其他消息,如 WM_CREATE 和 WM_DESTROY。一个简单而完整的 WndProc 函数如下:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("Hello, World!");

    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);

        // Here your application is laid out.// For this introduction, we just print out "Hello, World!"
        TextOut(hdc,
            5, 5,
            greeting, _tcslen(greeting));
        // End application-specific layout section.EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值