zoukankan      html  css  js  c++  java
  • 如何在Console下面生成一个WIN32窗口

    一个小挑战?

    VS2017里面,新建一个控制台工程,输入名字(你不需要也成,有默认的),得到一个控制台工程。

    image

    好了,生成的代码,如下:


    // Win32InConsole.cpp : This file contains the 'main' function. Program execution begins and ends there.
    //
    
    #include "pch.h"
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello World!
    "; 
    }
    
    // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
    // Debug program: F5 or Debug > Start Debugging menu
    
    // Tips for Getting Started: 
    //   1. Use the Solution Explorer window to add/manage files
    //   2. Use the Team Explorer window to connect to source control
    //   3. Use the Output window to see build output and other messages
    //   4. Use the Error List window to view errors
    //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
    //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file


    这里有一大堆的废代码,但是,我还是把它放上来了,忘了说,我的VS2017语言版本是英文版的,为什么弄英文版呢?因为我不想遇到太多的与中文与VS2017相关的麻烦,反正就是,设置为英文,好外多多,你不会动不动因为这VS的一些BUG而导致工作流中断,最后,你找来找去,才发现,原来是VS的问题。

    现在上正题,代码如下:

    // Win32InConsole.cpp : This file contains the 'main' function. Program execution begins and ends there.
    //
    
    #include "pch.h"
    #include <iostream>
    
    #include <windows.h>
    #define CLASS_NAME TEXT("First")
    int NewWindow();
    
    int main()
    {
        std::cout << "Hello World!
    "; 
        return NewWindow();
    }
    
    // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
    // Debug program: F5 or Debug > Start Debugging menu
    
    // Tips for Getting Started: 
    //   1. Use the Solution Explorer window to add/manage files
    //   2. Use the Team Explorer window to connect to source control
    //   3. Use the Output window to see build output and other messages
    //   4. Use the Error List window to view errors
    //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
    //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
    
    
    
    //我就不声明了,在这里直接上代码
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
    {
        switch (message)
        { 
        case WM_DESTROY:
            PostQuitMessage(0); //一定要加啊
            break;
        default:
            break;
        }
        
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
     
    
    
    int NewWindow()
    {
        WNDCLASS ws = { 0 };
        ws.cbClsExtra = sizeof(ws);
        ws.lpfnWndProc = WndProc;
        ws.lpszClassName = CLASS_NAME;
        if (!::RegisterClass(&ws)) return -1;
        HWND hWnd = CreateWindow(CLASS_NAME, CLASS_NAME, WS_OVERLAPPEDWINDOW
            , 100, 100, 800, 600, NULL, NULL, NULL, NULL);
        if (!hWnd) return -2;
    
        ShowWindow(hWnd, SW_SHOW);
        
        MSG msg;
        while (GetMessage(&msg, NULL, NULL, NULL))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return msg.wParam;
    
    }


    好了,事情就是这么简单!!!

    附上两个图:

    image


    关闭WIN32窗口后

    image

    PS:会笑的人,运气通常都会比别人好。
  • 相关阅读:
    帝国 标签模板 使用程序代码 去除html标记 并 截取字符串
    iis6 伪静态 iis配置方法 【图解】
    您来自的链接不存在 帝国CMS
    帝国cms Warning: Cannot modify header information headers already sent by...错误【解决方法】
    .fr域名注册 51元注册.fr域名
    帝国网站管理系统 恢复栏目目录 建立目录不成功!请检查目录权限 Godaddy Windows 主机
    星外虚拟主机管理平台 开通数据库 出现Microsoft OLE DB Provider for SQL Server 错误 '8004' 从字符串向 datetime 转换失败
    ASP.NET 自定义控件学习研究
    CSS层叠样式表之CSS解析机制的优先级
    ASP.NET程序员工作面试网络收藏夹
  • 原文地址:https://www.cnblogs.com/thinkinc999/p/12056075.html
Copyright © 2011-2022 走看看