常用的代码
DUhongchang120303
·
·
个人记录
代码
1.隐藏exe的窗口(黑框) :
#include<windows.h>
using namespace std;
int main(){
HWND hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_HIDE);
//隐藏exe的黑框仅限于windows 10
return 0;
}
2.控制窗口大小:
#include<windows.h>
using namespace std;
int main(){
system("title (填名称)");
return 0;
}
3.控制窗口名称:
#include<windows.h>
using namespace std;
int main(){
system("mode con: cols=(填数字) lines=(填数字)");
//数字过小不可以执行
return 0;
}
4.获取前台窗口的句柄
#include<windows.h>
using namespace std;
int main(){
GetForegroundWindow();
//可以将其存入一个变量,如:
//HWND h;
//h=GetForegroundWindow();
return 0;
}
5.判断U盘是否插入,并输出是哪个盘
#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
char buf[10];
int detection() {
for(int i=0; i<26; i++) {
sprintf(buf,"%c:\\",'A'+i);
if(GetDriveType(buf)==DRIVE_REMOVABLE) return 1;
}
return 0;
}
int main() {
int last=0,now=0;
while(1) {
last=now,now=detection();
if(now&&!last){
puts("IN");
puts(buf);
}
else if(last&&!now){
puts("OUT");
}
Sleep(10);
}
return 0;
}
6.同时运行多个函数
#include <bits/stdc++.h>
//#include <thread> // 必须包含线程头文件
//#include <chrono> // 用于模拟函数执行耗时
using namespace std;
int a;
// 定义第一个要并发运行的函数
void func1() {
}
// 定义第二个要并发运行的函数
void func2() {
}
int main() {
// 1. 创建线程对象,分别绑定要执行的函数(此时函数立即开始运行)
thread t1(func1); // 线程t1执行func1
thread t2(func2); // 线程t2执行func2
// 2. 等待线程结束(必须join,否则主线程退出会导致程序崩溃)
t1.join(); // 主线程等待t1执行完
t2.join(); // 主线程等待t2执行完
cout<<"完成";
return 0;
}