被玩坏的C程序控制台窗口

2023-11-22 01:59
文章标签 窗口 程序控制 玩坏

本文主要是介绍被玩坏的C程序控制台窗口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

嗯?听说你在Windows系统上C语言程序?那你对控制台窗口了解多少?

Linux、Mac 的Shell以后有时间再玩….

听说你会写Helloworld程序,我猜一定是这个样子的:

这里写图片描述

#include<stdio.h>
int main() {return 0&printf("Hello, world!\n");
}

那么问题来了,你会不会写下面这种风格的?
这里写图片描述

#include<Windows.h>
int main() {HWND hWnd = GetConsoleWindow();HDC hdc = GetDC(hWnd);HFONT font = CreateFont(64, // nHeight0, // nWidth-50, // nEscapement20, // nOrientationFW_BOLD, // nWeightTRUE, // bItalicTRUE, // bUnderline0, // cStrikeOutANSI_CHARSET, // nCharSetOUT_DEFAULT_PRECIS, // nOutPrecisionCLIP_DEFAULT_PRECIS, // nClipPrecisionDEFAULT_QUALITY, // nQualityDEFAULT_PITCH | FF_SWISS,L"Arial" // nPitchAndFamily Arial);SelectObject(hdc, font);SetBkColor(hdc, 0x000000);SetTextColor(hdc, RGB(100,149,237));TextOut(hdc, 30, 30, L"Hello, world!", 13);ValidateRect(hWnd, 0);return 0;
}

绘制文字算什么,还能在控制台窗口上添加按钮 ↓ ↓ ↓,没错就是每天都会点的那种按钮,点了还会有反应!

这里写图片描述

#include<Windows.h>
#include<iostream>
int main() {HWND hWnd = GetConsoleWindow();HFONT font = CreateFont(20, // nHeight0, // nWidth0, // nEscapement0, // nOrientationFW_NORMAL, // nWeightFALSE, // bItalicFALSE, // bUnderline0, // cStrikeOutANSI_CHARSET, // nCharSetOUT_DEFAULT_PRECIS, // nOutPrecisionCLIP_DEFAULT_PRECIS, // nClipPrecisionDEFAULT_QUALITY, // nQualityDEFAULT_PITCH | FF_SWISS,L"新宋体" // nPitchAndFamily Arial);HWND b1 = CreateWindow(L"BUTTON",   // predefined classL"确定",       // button textWS_VISIBLE | WS_CHILD,  //values for buttons.100,         // starting x position100,         // starting y position100,        // button width40,        // button heighthWnd,       // parent window0,       // No menu(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),NULL);HWND b2 = CreateWindow(L"BUTTON",   // predefined classL"取消",       // button textWS_VISIBLE | WS_CHILD,  //values for buttons.240,         // starting x position100,         // starting y position100,        // button width40,        // button heighthWnd,       // parent window0,       // No menu(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),NULL);SendMessage(b1, WM_SETFONT, (WPARAM)font, 1);SendMessage(b2, WM_SETFONT, (WPARAM)font, 1);MSG msg;while (GetMessage(&msg, 0, 0, 0)) {TranslateMessage(&msg);DispatchMessage(&msg);if (msg.hwnd==b1 && msg.message== WM_LBUTTONDOWN) {MessageBox(hWnd, L"点击了确定按钮", L"Hello", MB_ICONINFORMATION);}if (msg.hwnd == b2 && msg.message == WM_LBUTTONDOWN) {MessageBox(hWnd, L"点击了取消按钮", L"Hello", MB_ICONINFORMATION);}}
}

还有更多玩法,既然可以绘制不同字体的文字,当然也可以绘制图片

了解音频视频编码的同学把控制台窗口改造成视频播放器也是可以的!

这里写图片描述

#include<Windows.h>
#include<stdio.h>
#include<gdiplus.h>
#pragma comment(lib,"gdiplus.lib")
using namespace Gdiplus;int main() {Gdiplus::GdiplusStartupInput m_gdiplusStartupInput;ULONG_PTR m_gdiplusToken;GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);HWND hWnd = GetConsoleWindow();HDC hdc = GetDC(hWnd);Image* pImage = Image::FromFile(L"Flandre.jpg");if (!pImage) {printf("无法打开图片 Flandre.jpg");return -1;}Graphics graphics(hdc);graphics.DrawImage(pImage, 0, 0, pImage->GetWidth()/2.5, pImage->GetHeight()/2.5);return 0;
}

接下来是一个圆周运动停不下来的控制台窗口(Enter键终止)

#include<Windows.h>
#include<math.h>
#include<stdio.h>#define PI 3.14159265
#define POSCOUNT 10int position[POSCOUNT][2];DWORD WINAPI task(VOID* hWnd) {RECT rc;GetWindowRect((HWND)hWnd, &rc);int i = 0;while (true){MoveWindow((HWND)hWnd, position[i][0],position[i][1],rc.right - rc.left,rc.bottom - rc.top,TRUE);ShowWindow((HWND)hWnd, SW_SHOW);printf("当前窗口位置: %d,   %d\n", position[i][0], position[i][0]);i++;if (i >= POSCOUNT)i = 0;Sleep(20);}
}int main() {HWND hWnd = GetConsoleWindow();int i = 0;for (;i < POSCOUNT;i++) {position[i][0] = 100 * sin(PI * 2 * i / POSCOUNT) + 300;position[i][1] = 100 * cos(PI * 2 * i / POSCOUNT) + 300;}CreateThread(0, 0, task, hWnd, 0, 0);getc(stdin);
}

最后呢,在控制台窗口上来一发3D渲染好了

这里写图片描述

#include<d3d9.h>
#include<d3dx9.h>
#pragma comment(lib, "winmm.lib")
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9             g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DTEXTURE9      g_pTexture = NULL; // Our textureLPDIRECT3DVERTEXBUFFER9 g_pVBGeometry = NULL; // Buffer to hold vertices
LPDIRECT3DINDEXBUFFER9 g_pIBGeometry = NULL;                                                                                   // A structure for our custom vertex type. We added texture coordinatesstruct Vertex {D3DXVECTOR3 position;D3DXVECTOR3 normal;FLOAT u, v;
};
#define VertexFormat (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)Vertex box[]{{ { -0.5,-0.5,-0.5 },{ 0,0,-1 },0,1 },{ { -0.5,0.5,-0.5 },{ 0,0,-1 },0,0 },{ { 0.5,0.5,-0.5 },{ 0,0,-1 },1,0 },{ { 0.5,-0.5,-0.5 },{0,0,-1 },1,1 },{ { -0.5,-0.5,0.5 },{0,0,1 },0,1 },{ { -0.5,0.5,0.5 },{ 0,0,1 },0,0 },{ { 0.5,0.5,0.5 },{ 0,0,1 },1,0 },{ { 0.5,-0.5,0.5 },{ 0,0,1 },1,1 },{ { -0.5,-0.5,-0.5 },{ -1,0,0 },0,1 },{ { -0.5,-0.5,0.5 },{ -1,0,0 },0,0 },{ { -0.5,0.5,0.5 },{ -1,0,0 },1,0 },{ { -0.5,0.5,-0.5 },{ -1,0,0 },1,1 },{ { 0.5,-0.5,-0.5 },{ 1,0,0 },0,1 },{ { 0.5,-0.5,0.5 },{ 1,0,0 },0,0 },{ { 0.5,0.5,0.5 },{ 1,0,0 },1,0 },{ { 0.5,0.5,-0.5 },{ 1,0,0 },1,1 },{ { -0.5,-0.5,-0.5 },{ 0,-1,0 },0,1 },{ { -0.5,-0.5,0.5 },{ 0,-1,0 },0,0 },{ { 0.5,-0.5,0.5 },{ 0,-1,0 },1,0 },{ { 0.5,-0.5,-0.5 },{ 0,-1,0 },1,1 },{ { -0.5,0.5,-0.5 },{ 0,1,0 },0,1 },{ { -0.5,0.5,0.5 },{ 0,1,0 },0,0 },{ { 0.5,0.5,0.5 },{ 0,1,0 },1,0 },{ { 0.5,0.5,-0.5 },{ 0,1,0 },1,1 },};int index[]{0,1,2, 0,2,3,4,5,6, 4,6,7,8,9,10, 8,10,11,12,13,14, 12,14,15,16,17,18, 16,18,19,20,21,22, 20,22,23
};//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D(HWND hWnd)
{// Create the D3D object.if (NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))return E_FAIL;// Set up the structure used to create the D3DDevice. Since we are now// using more complex geometry, we will create a device with a zbuffer.D3DPRESENT_PARAMETERS d3dpp;ZeroMemory(&d3dpp, sizeof(d3dpp));d3dpp.Windowed = TRUE;d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;d3dpp.EnableAutoDepthStencil = TRUE;d3dpp.AutoDepthStencilFormat = D3DFMT_D16;// Create the D3DDeviceif (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp, &g_pd3dDevice))){return E_FAIL;}// Turn off cullingg_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);// Turn off D3D lightingg_pd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);// Turn on the zbufferg_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);return S_OK;
}//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Create the textures and vertex buffers
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{// Use D3DX to create a texture from a file based imageif (FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, L"9.jpg", &g_pTexture))){// If texture is not in current folder, try parent folderif (FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, L"9.jpg", &g_pTexture))){MessageBox(NULL, L"Could not find 9.jpg", L"Texture file not found", MB_OK);return E_FAIL;}}if (FAILED(g_pd3dDevice->CreateVertexBuffer(sizeof(box),0, VertexFormat,D3DPOOL_DEFAULT, &g_pVBGeometry, NULL))){return E_FAIL;}if (FAILED(g_pd3dDevice->CreateIndexBuffer(sizeof(index), 0, D3DFMT_INDEX32,D3DPOOL_DEFAULT, &g_pIBGeometry, 0))) {                return E_FAIL;}void *p = NULL;g_pVBGeometry->Lock(0, 0, &p, 0);memcpy(p, box, sizeof(box));g_pVBGeometry->Unlock();g_pIBGeometry->Lock(0, 0, &p, 0);memcpy(p, index, sizeof(index));g_pIBGeometry->Unlock();return S_OK;
}//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{if (g_pTexture != NULL)g_pTexture->Release();if (g_pVBGeometry != NULL)g_pVBGeometry->Release();if (g_pIBGeometry != NULL)g_pIBGeometry->Release();if (g_pd3dDevice != NULL)g_pd3dDevice->Release();if (g_pD3D != NULL)g_pD3D->Release();
}//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
VOID SetupMatrices()
{// Set up world matrixD3DXMATRIXA16 matWorld;D3DXMatrixIdentity(&matWorld);D3DXMatrixRotationY(&matWorld, timeGetTime() / 1000.0f);g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);// Set up our view matrix. A view matrix can be defined given an eye point,// a point to lookat, and a direction for which way is up. Here, we set the// eye five units back along the z-axis and up three units, look at the// origin, and define "up" to be in the y-direction.D3DXVECTOR3 vEyePt(0.0f, 2.0f, -3.0f);D3DXVECTOR3 vLookatPt(0.0f, 0.0f, 0.0f);D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f);D3DXMATRIXA16 matView;D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec);g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);// For the projection matrix, we set up a perspective transform (which// transforms geometry from 3D view space to 2D viewport space, with// a perspective divide making objects smaller in the distance). To build// a perpsective transform, we need the field of view (1/4 pi is common),// the aspect ratio, and the near and far clipping planes (which define at// what distances geometry should be no longer be rendered).D3DXMATRIXA16 matProj;RECT rc;GetClientRect(GetConsoleWindow(), &rc);D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, 1.0f*(rc.right-rc.left)/(rc.bottom-rc.top), 1.0f, 100.0f);g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}//-----------------------------------------------------------------------------
// Name: SetupLights()
// Desc: Sets up the lights and materials for the scene.
//-----------------------------------------------------------------------------
VOID SetupLights()
{// Set up a material. The material here just has the diffuse and ambient// colors set to yellow. Note that only one material can be used at a time.D3DMATERIAL9 mtrl;ZeroMemory(&mtrl, sizeof(D3DMATERIAL9));mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;mtrl.Diffuse.b = mtrl.Ambient.b = 1.0f;mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;mtrl.Specular.r = 0.4f;mtrl.Specular.g = 0.4f;mtrl.Specular.b = 0.4f;mtrl.Specular.a = 0.4f;g_pd3dDevice->SetMaterial(&mtrl);// Set up a white, directional light, with an oscillating direction.// Note that many lights may be active at a time (but each one slows down// the rendering of our scene). However, here we are just using one. Also,// we need to set the D3DRS_LIGHTING renderstate to enable lightingD3DXVECTOR3 vecDir;D3DLIGHT9 light;ZeroMemory(&light, sizeof(D3DLIGHT9));light.Type = D3DLIGHT_DIRECTIONAL;light.Diffuse.r = 1.0f;light.Diffuse.g = 1.0f;light.Diffuse.b = 1.0f;light.Specular.r = 1.0f;light.Specular.g = 1.0f;light.Specular.b = 1.0f;light.Ambient.r = 0.1f;light.Ambient.g = 0.1f;light.Ambient.b = 0.1f;vecDir = D3DXVECTOR3(cosf(timeGetTime() / 350.0f-60),-1.0f,sinf(timeGetTime() / 350.0f - 60));D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &vecDir);light.Range = 1000.0f;g_pd3dDevice->SetLight(0, &light);g_pd3dDevice->LightEnable(0, TRUE);g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);// Finally, turn on some ambient light.g_pd3dDevice->SetRenderState(D3DRS_AMBIENT, 0x00202020);
}//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{// Clear the backbuffer and the zbufferg_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);// Begin the sceneif (SUCCEEDED(g_pd3dDevice->BeginScene())){// Setup the world, view, and projection matricesSetupMatrices();// Setup our texture. Using textures introduces the texture stage states,// which govern how textures get blended together (in the case of multiple// textures) and lighting information. In this case, we are modulating// (blending) our texture with the diffuse color of the vertices.g_pd3dDevice->SetTexture(0, g_pTexture);g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);g_pd3dDevice->SetStreamSource(0, g_pVBGeometry, 0, sizeof(Vertex));g_pd3dDevice->SetIndices(g_pIBGeometry);g_pd3dDevice->SetFVF(VertexFormat);g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);// End the sceneg_pd3dDevice->EndScene();}// Present the backbuffer contents to the displayg_pd3dDevice->Present(NULL, NULL, NULL, NULL);
}int main() {InitD3D(GetConsoleWindow());InitGeometry();SetupLights();while (true){Render();}
}

这篇关于被玩坏的C程序控制台窗口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/406663

相关文章

使用JS/Jquery获得父窗口的几个方法(笔记)

<pre name="code" class="javascript">取父窗口的元素方法:$(selector, window.parent.document);那么你取父窗口的父窗口的元素就可以用:$(selector, window.parent.parent.document);如题: $(selector, window.top.document);//获得顶级窗口里面的元素 $(

专题二_滑动窗口_算法专题详细总结

目录 滑动窗口,引入: 滑动窗口,本质:就是同向双指针; 1.⻓度最⼩的⼦数组(medium) 1.解析:给我们一个数组nums,要我们找出最小子数组的和==target,首先想到的就是暴力解法 1)暴力: 2)优化,滑动窗口: 1.进窗口 2.出窗口 3.更新值 2.⽆重复字符的最⻓⼦串(medium) 1)仍然是暴力解法: 2)优化: 进窗口:hash[s[rig

hot100刷题第1-9题,三个专题哈希,双指针,滑动窗口

求满足条件的子数组,一般是前缀和、滑动窗口,经常结合哈希表; 区间操作元素,一般是前缀和、差分数组 数组有序,更大概率会用到二分搜索 目前已经掌握一些基本套路,重零刷起leetcode hot 100, 套路题按套路来,非套路题适当参考gpt解法。 一、梦开始的地方, 两数之和 class Solution:#注意要返回的是数组下标def twoSum(self, nums: Lis

主窗口的设计与开发(二)

主窗口的设计与开发(二) 前言         在上一集当中,我们完成了主窗口的初始化,主窗口包括了左中右三个区域。我们还完成了对左窗口的初始化,左窗口包括了用户头像、会话标签页按钮、好友标签页按钮以及好友申请标签页按钮。对于切换每个标签页,我们还做了初始化信号槽的内容。最后我们将整个MainWidget类设置为单例模式。         那么这一集我们将继续完成主窗口的设计与开发,这一集我

QtC++截图支持窗口获取

介绍 在截图工具中你会发现,接触到窗口后会自动圈出目标窗口,个别强大一点的还能进行元素识别可以自动圈出元素,那么今天简单分析一下QTc++如何获取窗口并圈出当前鼠标下的窗口。 介绍1.如何获取所有窗口2.比较函数3.实现窗口判断 结尾 1.如何获取所有窗口 1.我们需要调用windows接口EnumWindowsProc回调函数来获取所有顶级窗口,需要包含windows.

运行.bat文件,如何在Dos窗口里面得到该文件的路径

把java代码打包成.jar文件,编写一个.bat文件,执行该文件,编译.jar包;(.bat,.jar放在同一个文件夹下) 运行.bat文件,如何在Dos窗口里面得到该文件的路径,并运行.jar文件: echo 当前盘符:%~d0 echo 当前路径:%cd% echo 当前执行命令行:%0 echo 当前bat文件路径:%~dp0 echo 当前bat文件短路径:%~sdp0 nc

类codepen的实现可拖拽窗口demo

首先说下思想 flex或者其他布局方式,实现左右分割布局,主盒子宽度100%,左右布局中包含一个分割条(可在布局容器中,也可以单独定义)为分隔条绑定鼠标点击事件,为document绑定鼠标移动事件和鼠标放开事件,通过监听鼠标移动事件和上一个状态保存下来的鼠标位置作对比,判断当前鼠标移动方向(往左还是往右)然后计算当前鼠标位置和鼠标点击位置的距离,来计算左右容器的变化,然后通过dom的方式设置宽度

【leetcode详解】考试的最大困扰度(滑动窗口典例)

实战总结: sum += answerKey[right] == c; 经典操作,将判断语句转化为0, 1接收来计数//大问题分解: 对'T'还是'F'做修改, 传参为c//滑动窗口: 遍历, 维护left& right指向 及 c的个数, 更新不知从何下手写代码时:考虑先写好第一次的,然后以此为基础补充代码以适后续情况 题面: 解题感受: 思路总体好想, 实现略有挑战。 思路分析:

【每日一题】LeetCode 2379.得到K个黑块的最少涂色次数(字符串、滑动窗口)

【每日一题】LeetCode 2379.得到K个黑块的最少涂色次数(字符串、滑动窗口) 题目描述 给定一个字符串 blocks,其中每个字符代表一个颜色块,可以是 ‘W’(白色)或 ‘B’(黑色)。你需要找到一个至少包含 k 个连续黑色块的子串。每次操作可以将一个白色块变成黑色块。你的任务是找到至少出现一次连续 k 个黑色块的最少操作次数。 和该题目类似:【每日一题】LeetCode 202

【hive 函数】Hive分析函数和窗口函数

拿一个例子来说 数据集: cookie1,2015-04-10 10:00:02,url2 cookie1,2015-04-10 10:00:00,url1 cookie1,2015-04-10 10:03:04,1url3 cookie1,2015-04-10 10:50:05,url6 cookie1,2015-04-10 11:00:00,url7 cookie1,2