我之前看了一些其他作者的方法,选了一个比较简单的,能解决的,我再简化一下,用分步骤的方式发布出来。
1.下载vs2022
官网:Visual Studio 2022 IDE - 适用于软件开发人员的编程工具
进入官网,选择community 2022下载。
这个界面中,勾上c++桌面开发,右边一定要勾上MSVC相关的。
2. 检查环境
首先,在C盘创建一个文件夹,位置:C:\testEasyx\demo。
然后,使用windows自带搜索工具搜索Developer Command Prompt,以管理员身份打开。
输入cl.exe,如果出现版号,说明c++编辑器安装成功。
接着输入 code C:\testEasyx\demo (也就是刚才建的文件夹位置)。
然后就会自动弹出vscode;
然后新建一个文件,将这个代码复制进去。
#include
#include
#include
using namespace std;
int main()
{
vector msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
注意:按Ctrl+F5,选择c++(windows),再接着选择cl.exe。(如下图)
然后如果出现这样,就代表成功。
3.安装easyx
官网:EasyX Graphics Library for C++
下载后,安装到vs2022。
4.将一些文件进行转移
如果你的vs2022是默认安装路径,那么就在C:\Program Files (x86)\Microsoft VisualStudio\2022
然后
1.进入Community\VC\Auxiliary\VS\lib\x64,把这两个文件复制一下
粘贴到:Community\VC\Tools\MSVC\14.30.30705\lib\x64 (注意这里14.30.30705每个人可能不一样,下面也是)
2.进入Community\VC\Auxiliary\VS\lib\x86,把这两个文件复制一下
粘贴到:Community\VC\Tools\MSVC\14.30.30705\lib\x86
3.进入Community\VC\Auxiliary\VS\include,把这两个文件复制一下
粘贴到:Community\VC\Tools\MSVC\14.30.30705\include
5.修改vscode里tasks.json
打开后:
然后进行修改,注意箭头的地方。
修改为:
代码如下:
{
"tasks": [
{
"type": "shell",//需要改为shell
"label": "C/C++: cl.exe 生成活动文件",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"kernel32.lib",//这里开始
"gdi32.lib",
"winspool.lib",
"comdlg32.lib",
"advapi32.lib",
"shell32.lib",
"ole32.lib",
"oleaut32.lib",
"uuid.lib",
"odbc32.lib",
"odbccp32.lib",
"user32.lib",//这里结束
"/nologo",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
然后用如下代码进行测试:
#include
#include
#include
#define Width 640
#define Height 480
#define PI 3.14159
int main()
{
// 初始化绘图窗口
initgraph(640, 480, SHOWCONSOLE);
//秒针起始坐标
int center_x = Width / 2, center_y = Height / 2;
//秒针终点坐标
int secondEnd_x, secondEnd_y;
//分针终点坐标
int minuteEnd_x, minuteEnd_y;
//时针终点坐标
int hourEnd_x, hourEnd_y;
//秒针长度
int secondLength = Width / 4;
//分针长度
int minuteLength = Width / 5.5;
//时针长度
int hourLength = Width / 7;
//秒针对应转到角度
float secondAngle = 0;
//分针对应转到角度
float minuteAngle = 0;
//时针对应转到角度
float hourAngle = 0;
//定义变量存储系统时间
SYSTEMTIME ti;
BeginBatchDraw();
while (1)
{
setfillcolor(YELLOW);
setlinestyle(PS_DASHDOTDOT, 5);
setlinecolor(0x555555);
circle(center_x, center_y, secondLength + 30);
setcolor(0xAAAAAA);
setlinestyle(PS_DOT | PS_ENDCAP_SQUARE, 2);
circle(center_x, center_y, secondLength + 15);
for (int i = 0; i < 12; i++)
{
int x = center_x + cos(i * 30.0 / 360 * 2 * PI) * (secondLength + 15.0);
int y = center_y - sin(i * 30.0 / 360 * 2 * PI) * (secondLength + 15.0);
fillcircle(x, y, 5);
}
GetLocalTime(&ti);
secondAngle = (ti.wSecond / 60.0) * (2 * PI);
minuteAngle = (ti.wMinute / 60.0) * (2 * PI);
hourAngle = ((ti.wHour % 12) / 12.0) * (2 * PI) + (ti.wMinute / 60.0) * (2 * PI / 12.0);
secondEnd_x = center_x + secondLength * sin(secondAngle);
secondEnd_y = center_y - secondLength * cos(secondAngle);
minuteEnd_x = center_x + minuteLength * sin(minuteAngle);
minuteEnd_y = center_y - minuteLength * cos(minuteAngle);
hourEnd_x = center_x + hourLength * sin(hourAngle);
hourEnd_y = center_y - hourLength * cos(hourAngle);
//画秒针
setlinestyle(PS_SOLID, 1);
setcolor(WHITE);
line(center_x, center_y, secondEnd_x, secondEnd_y);
//画分针
setlinestyle(PS_SOLID, 2);
setcolor(GREEN);
line(center_x, center_y, minuteEnd_x, minuteEnd_y);
//画时针
setlinestyle(PS_SOLID, 5);
setcolor(RED);
line(center_x, center_y, hourEnd_x, hourEnd_y);
FlushBatchDraw();
setlinestyle(PS_SOLID, 1);
setcolor(BLACK);
line(center_x, center_y, secondEnd_x, secondEnd_y);
setlinestyle(PS_SOLID, 2);
setcolor(BLACK);
line(center_x, center_y, minuteEnd_x, minuteEnd_y);
setlinestyle(PS_SOLID, 5);
setcolor(BLACK);
line(center_x, center_y, hourEnd_x, hourEnd_y);
}
EndBatchDraw();
system("pause");
closegraph();
return 0;
}
按Ctrl+F5,应该就成功了。