zoukankan      html  css  js  c++  java
  • 第一个Windows窗口应用程序

    学习目的

    熟悉开发工具Visual C++ 6.0和MSDN 2001的使用. 应用Windows API函数, 手工编写具有最基本构成的Windows窗口应用程序(包含WinMain入口函数, 消息循环, 窗口函数), 并调试成功.

    1.熟悉开发工具

    熟悉开发工具visual studio的使用:

    在visual studio中新建win32空项目

     

    2.熟悉MSDN帮助的使用

    练习使用MSDN查询windows相关函数信息

     

    3. 应用Windows API函数, 手工编写具有最基本构成的Windows窗口应用程序(包含WinMain入口函数, 消息循环, 窗口函数)

    程序代码如下:

    #include <windows.h>

    #include<tchar.h>

     

    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //窗口函数说明

     

    //------------ 初始化窗口类----------------

    int WINAPI WinMain(HINSTANCE hInstance, //WinMain函数说明

        HINSTANCE hPrevInst,

        LPSTR lpszCmdLine,

        int nCmdShow)

    {

        HWND hwnd;

        MSG Msg;

        WNDCLASS wndclass;

        TCHAR lpszClassName[] = _T("窗口"); //窗口类名

        TCHAR lpszTitle[] = _T("My_Windows"); //窗口标题名

     

        //窗口类的定义

        wndclass.style = 0; //窗口类型为缺省类型

        wndclass.lpfnWndProc = WndProc; //窗口处理函数为WndProc

        wndclass.cbClsExtra = 0; //窗口类无扩展

        wndclass.cbWndExtra = 0; //窗口实例无扩展

        wndclass.hInstance = hInstance; //当前实例句柄

        wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

        //窗口的最小化图标为缺省图标

        wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);

        //窗口采用箭头光标

        wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);

        //窗口背景为白色

        wndclass.lpszMenuName = NULL; //窗口中无菜单

        wndclass.lpszClassName = lpszClassName;

        //窗口类名为"窗口示例"

     

        //--------------- 窗口类的注册 -----------------

        if (!RegisterClass(&wndclass)) //如果注册失败则发出警告声音

        {

            MessageBeep(0);

            return FALSE;

        }

     

        //创建窗口

        hwnd = CreateWindow(lpszClassName, //窗口类名

            lpszTitle, //窗口实例的标题名

            WS_OVERLAPPEDWINDOW, //窗口的风格

            CW_USEDEFAULT,

            CW_USEDEFAULT, //窗口左上角坐标为缺省值

            CW_USEDEFAULT,

            CW_USEDEFAULT, //窗口的高和宽为缺省值

            NULL, //此窗口无父窗口

            NULL, //此窗口无主菜单

            hInstance, //创建此窗口的应用程序的当前句柄

            NULL); //不使用该值

     

        //显示窗口

        ShowWindow(hwnd, nCmdShow);

        //绘制用户区

        UpdateWindow(hwnd);

        //消息循环

        while (GetMessage(&Msg, NULL, 0, 0))

        {

            TranslateMessage(&Msg);

            DispatchMessage(&Msg);

        }

        return Msg.wParam; //消息循环结束即程序终止时将信息返回系统

    }

     

    //窗口函数

    LRESULT CALLBACK WndProc(HWND hwnd,

        UINT message,

        WPARAM wParam,

        LPARAM lParam)

    {

        switch (message){

        case WM_DESTROY:

            PostQuitMessage(0); //调用PostQuitMessage发出WM_QUIT消息

        default: //缺省时采用系统消息缺省处理函数

            return DefWindowProc(hwnd, message, wParam, lParam);

        }

        return (0);

    }

    编译并调试程序,

     

    Windows窗口应用程序运行结果

     

    通过本次学习,熟悉了开发工具Visual C++ 6.0和MSDN 2001的使用.

    通过应用Windows API函数, 手工编写Windows窗口应用程序了解了windows窗口应用最基本的构成:包含WinMain入口函数, 消息循环, 窗口函数……

  • 相关阅读:
    nohup npm start &启动之后关闭终端程序没有后台运行
    C++标准库之string返回值研究
    Apache Thrift的C++多线程编程定式
    实战C++对象模型之成员函数调用
    std::string的拷贝赋值研究
    REdis AOF文件结构分析
    使用Linux自带日志滚动工具logrotate滚动redis日志示例
    源码分析MySQL mysql_real_query函数
    源码解读Linux的limits.conf文件
    C++中的return和exit区别
  • 原文地址:https://www.cnblogs.com/leftshine/p/5698589.html
Copyright © 2011-2022 走看看