如何在Console下面生成一个WIN32窗口
一个小挑战?
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窗口后

如何在Console下面生成一个WIN32窗口的更多相关文章
- cmake 如何生成一个win32工程
只需要加上下面一句连接选项就可以了. IF(WIN32) SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:W ...
- 要生成一个窗口,通常使用Window的子类Frame类进行实例化
要生成一个窗口,通常使用Window的子类Frame类进行实例化,而不是直接使用Window 类,框架的外观就像平常Windows系统下的窗口,有标题.边框. 菜单 和大小等. setSize()方法 ...
- Nehe OpenGL教程第一课-创建一个OpenGL窗口(Win32)
原文英文地址为:Creating an OpenGL Window (Win32),翻译的chm中文格式文档下载地址为:OpenGL教程电子书(chm格式)中文版,源代码在官网上也可以下载到,每 ...
- 如何在github上展示作品——为你的项目生成一个快速访问的网址如(DaisyWang88.github.io)
(这里值针对Windos系统的,因为本人用的是Window系统,暂时没有条件在其他平台上测试) 1.创建命名为 <userName>.github.io的仓库. 这里的u ...
- 第一个手写Win32窗口程序
第一个手写Win32窗口程序 一 Windows编程基础 1 Win32应用程序的基本类型 1.1 控制台程序 不需要完善的Windows窗口,可以使用DOS窗口 的方式显示. 1.2 Win32窗口 ...
- (7)nehe教程1 创建一个OpenGL窗口:
不要用那个nehe ndk了 误人子弟! 转自: 一个窗口,代码可真多啊 http://www.yakergong.net/nehe/ 在这个教程里,我将教你在Windows环境中创建OpenGL程序 ...
- 如何在pyqt中给无边框窗口添加DWM环绕阴影
前言 在之前的博客<如何在pyqt中通过调用SetWindowCompositionAttribute实现Win10亚克力效果>中,我们实现了窗口的亚克力效果,同时也用SetWindowC ...
- 让我们写一个 Win32 文本编辑器吧 - 2. 计划和显示
让我们写一个 Win32 文本编辑器吧 - 2. 计划和显示 如果你已经阅读了简介,相信你已经对我们接下来要做的事情有所了解. 本文,将会把简介中基础程序修改为一个窗体应用程序.并对编辑器接下来的编辑 ...
- Win32 - 窗口
Win32 - 窗口 目录 Win32 - 窗口 前言 流程图 创建项目 VS MinGW Win32API字符串 Unicode 和 ANSI 函数 TCHAR WinMain:Win32 Appl ...
随机推荐
- Codeforces Round #594 (Div. 1) A. Ivan the Fool and the Probability Theory 动态规划
A. Ivan the Fool and the Probability Theory Recently Ivan the Fool decided to become smarter and stu ...
- Python程序中的进程操作-进程同步(multiprocess.Lock)
目录 一.多进程抢占输出资源 二.使用锁维护执行顺序 三.多进程同时抢购余票 四.使用锁来保证数据安全 通过刚刚的学习,我们千方百计实现了程序的异步,让多个任务可以同时在几个进程中并发处理,他们之间的 ...
- celery定时器
Celery 1.什么是Clelery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 Celery架构 Celery的架构由三部分组 ...
- JS做的类似腾讯专题图片播放器,大家可以一起来改进!
我是一个应届生,来公司不久,根据需求,网站需要一个专题图片轮播的页面.网上确实有很多现成的插件,但是,作为一个JS还不是很牛的应届生,我决定自己写一个! 话说忽然想到做个这个还真不容易,一时思绪理不清 ...
- Java13 闪亮来袭,你是否还停留在 Java8
近期 Java 界好消息频传.先是 Java 13 发布,接着 Eclipse 也发布了新版本表示支持新版本的 Java 特性. 本文介绍了 Java 13 的新特性并展示了相关的示例. 2019 年 ...
- Java设计模式:Simple Factory(简单工厂)模式
概念定义 简单工厂(Simple Factory)模式,又称静态工厂方法(Static Factory Method)模式,即定义一个工厂类,根据传入的不同参数创建不同的产品实例,这些实例对象具有共同 ...
- Ubuntu安装CUDA、CUDNN比较有用的网址总结
Ubuntu安装CUDA.CUDNN比较有用的网址总结 1.tensorflow各个版本所对应的的系统要求和CUDA\CUDNN适配版本 https://tensorflow.google.cn/in ...
- node.js如何批量赋值
1. 数组解析赋值 let a = 1; let b = 2; let c = 3; 等同于 let [a, b, c] = [1, 2, 3]; 默认值 let [a, b = "B&qu ...
- Spring Boot 2.X整合Spring-cache,让你的网站速度飞起来
计算机领域有人说过一句名言:“计算机科学领域的任何问题都可以通过增加一个中间层来解决”,今天我们就用Spring-cache给网站添加一层缓存,让你的网站速度飞起来. 本文目录 一.Spring Ca ...
- ASP.NET Core框架深度学习(一) Hello World
对于学习Core的框架,对我帮助最大的一篇文章是Artech的<200行代码,7个对象——让你了解ASP.NET Core框架的本质>,最近我又重新阅读了一遍该文.本系列文章就是结合我的阅 ...