一个小挑战?
VS2017里面,新建一个控制台工程,输入名字(你不需要也成,有默认的),得到一个控制台工程。
好了,生成的代码,如下:
// 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!\n";
} // 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!\n";
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; }
好了,事情就是这么简单!!!
附上两个图:
关闭WIN32窗口后