不多说直接上代码,代码里面自己看注释就行
先创建头文件stdlibs.h
#pragram once
//运行C时的头文件
#include<stdio.h>
#include<time.h>
#include<string.h>
#include<math.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
//运行C++的头文件时
#include<iostream>
#include<fstream>
//运行Windows头文件时
#include<windows.h>
注意:这里的头文件有可能不会用到,这只是为了平时写的时候方便。
一般来说如果是控制台的输出控制,只用上头文件:
1.windows.h
2.iostream
3.stdio.h
#include"stdlibs.h"
#include <windows.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)//获取键盘事件
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
//using namespace std 可要可不要
void ConsoleOPType(int height,int width){//可以用CreateFont代替,而且CreateFont效果更好。其中height代表字体高度,width代表字体宽度
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof cfi;
cfi.nFont = 0;
cfi.dwFontSize.X = height;
cfi.dwFontSize.Y =width;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
}
void color(int num){//控制控制台输出颜色
hout=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hout,num);
}
void gotoxy(HANDLE hout,int x,int y){//控制台输出的光标位置
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(hout,pos);
}
int main(){
RECT rect;
HWND hwnd=GetForegroundWindow();//获取句柄
//Sleep(int number);//延迟函数,中间参数填数字,以毫秒位单位,前面最好加一个srand(time(NULL))
Sleep(2);
int cx=GetSystemMetrics(SM_CXFULLSCREEN);//x
int cy=GetSystemMetrics(SM_CYFULLSCREEN);//不包括任务栏的全屏尺寸,y
int px=GetSystemMetrics(SM_CXSCREEN);//全屏,x
int py=GetSystemMetrics(SM_CYSCREEN); //y
// MoveWindow(窗口句柄,你所需要的窗口位置——长,你所需要的窗口位置——宽,你所需要的窗口的大小——长,你所需要的窗口的大小——宽,true||false);
MoveWindow(hwnd,cx*0.4,cy*0.4,cx*0.2,cy*0.4,true);
//ConsoleOPType(11,23);
HWND Button;
Button=CreateWindow(
TEXT("button"),//输出的就是按钮
TEXT("OK"),//按钮上的字
WS_CAPTION|...,//按钮格式
x,y,height,width,
父窗口句柄,
消息列表,
程序实例句柄,
NULL);
/*
MessageBox(父窗口句柄,
标题变量(字符串),
内容(字符串),
格式,比如MB_OK或MB_ICONHAND
);
*/
printf("Hello, world!");
}