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:会笑的人,运气通常都会比别人好。
  • 相关阅读:
    The AndroidManifest.xml File
    handlebars简单用法
    高性能跨语言模板引擎Crox
    C++17 新特性
    C++ 14新特性
    [lua]笔记
    [lua]笔记
    delphi关键字
    delphi 基础
    TCP/UDP
  • 原文地址:https://www.cnblogs.com/thinkinc999/p/12056075.html
Copyright © 2011-2022 走看看