我更新啦!纯手工编写C++画图,有注释!
感谢Ttcoffee提的建议(私聊)
本次更新内容:
新增两个按钮,操作更易上手
以及......................................一个失败内容(屏幕不刷新,我已不指望了)
#include <windows.h>
#include <gdiplus.h>
using namespace std;
HWND hButton,hButton2;
POINT g_startPos, g_endPos;
RECT g_originalRect;
LONG_PTR g_originalStyle;
bool g_isFullscreen = false;
HBITMAP g_hMemBitmap = NULL;
HDC g_hMemDC = NULL;
int g_width = 0, g_height = 0;
// 初始化双缓冲
void InitDoubleBuffer(HWND hwnd) {
RECT rc;
GetClientRect(hwnd, &rc);
g_width = rc.right - rc.left;
g_height = rc.bottom - rc.top;
HDC hdc = GetDC(hwnd);
g_hMemDC = CreateCompatibleDC(hdc);
g_hMemBitmap = CreateCompatibleBitmap(hdc, g_width, g_height);
SelectObject(g_hMemDC, g_hMemBitmap);
ReleaseDC(hwnd, hdc);
// 初始清屏(可选)
FillRect(g_hMemDC, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH));
}
// 切换全屏函数
void ToggleFullscreen(HWND hwnd) {
if (!g_isFullscreen) {
// 进入全屏
GetWindowRect(hwnd, &g_originalRect);
g_originalStyle = GetWindowLongPtr(hwnd, GWL_STYLE);
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
SetWindowLongPtr(hwnd, GWL_STYLE, WS_POPUP);
SetWindowPos(hwnd, HWND_TOP,
0, 0, screenWidth, screenHeight,
SWP_FRAMECHANGED | SWP_SHOWWINDOW
);
} else {
// 退出全屏
SetWindowLongPtr(hwnd, GWL_STYLE, g_originalStyle);
SetWindowPos(hwnd, NULL,
g_originalRect.left,
g_originalRect.top,
g_originalRect.right - g_originalRect.left,
g_originalRect.bottom - g_originalRect.top,
SWP_FRAMECHANGED | SWP_NOZORDER
);
}
g_isFullscreen = !g_isFullscreen;
}
// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_SIZE:
// 窗口大小变化时重置缓冲
if (g_hMemDC) {
DeleteDC(g_hMemDC);
DeleteObject(g_hMemBitmap);
}
InitDoubleBuffer(hwnd);
break;
case WM_KEYDOWN:
if (wParam == VK_BACK) { // 按Backspace切换全屏
ToggleFullscreen(hwnd);
} else if (wParam == VK_CONTROL) {
DestroyWindow(hwnd);
HWND hwnwd;
hwnwd = FindWindow("ConsoleWindowClass", NULL);
if (hwnwd) {
ShowOwnedPopups(hwnwd, SW_SHOW);
ShowWindow(hwnwd, SW_SHOW);
}
}
return 0;
case WM_PAINT: { // 窗口需要绘制时触发
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps); // 获取设备上下文
// 设置背景模式为透明
BitBlt(hdc, 0, 0, g_width, g_height, g_hMemDC, 0, 0, SRCCOPY);
SetBkMode(hdc, TRANSPARENT);
HPEN hDashPen = CreatePen(PS_DASH, 1, RGB(256, 256, 256));
SelectObject(hdc, hDashPen);
MoveToEx(hdc, g_startPos.x, g_startPos.y, NULL);
LineTo(hdc, g_endPos.x, g_endPos.y);
return 0;
}
case WM_CLOSE:
break;
case WM_DESTROY:
break;
// 处理鼠标消息
case WM_LBUTTONDOWN:
g_startPos.x = LOWORD(lParam);
g_startPos.y = HIWORD(lParam);
return 0;
case WM_MOUSEMOVE:
if (wParam & MK_LBUTTON) {
g_endPos.x = LOWORD(lParam);
g_endPos.y = HIWORD(lParam);
InvalidateRect(hwnd, NULL, TRUE);
}
return 0;
case WM_ERASEBKGND:
return 1; // 禁用背景擦除
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
case WM_CREATE: {
// 创建按钮
hButton = CreateWindow(
"BUTTON", // 窗口类(按钮)
"点击我关闭", // 按钮文本
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // 样式
50, 50, // 位置 (x, y)
100, 30, // 大小 (宽度, 高度)
hwnd, // 父窗口句柄
(HMENU)1, // 控件ID
((LPCREATESTRUCT)lParam)->hInstance, // 实例句柄
NULL // 附加数据
);
hButton2 = CreateWindow(
"BUTTON", // 窗口类(按钮)
"点击我切换全屏", // 按钮文本
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // 样式
850, 50, // 位置 (x, y)
100, 30, // 大小 (宽度, 高度)
hwnd, // 父窗口句柄
(HMENU)2, // 控件ID
((LPCREATESTRUCT)lParam)->hInstance, // 实例句柄
NULL // 附加数据
);
return 0;
}
case WM_COMMAND: {
int ButtonID=LOWORD(wParam);
switch(ButtonID){
case 1:
DestroyWindow(hwnd);
HWND hwnwd;
hwnwd = FindWindow("ConsoleWindowClass", NULL);
if (hwnwd) {
ShowOwnedPopups(hwnwd, SW_SHOW);
ShowWindow(hwnwd, SW_SHOW);
}
break;
case 2:
ToggleFullscreen(hwnd);
break;
}
return 0;
}
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow) {
HWND hwnwd;
hwnwd = FindWindow("ConsoleWindowClass", NULL);
if (hwnwd) {
ShowOwnedPopups(hwnwd, SW_HIDE);
ShowWindow(hwnwd, SW_HIDE);
}
const char* CLASS_NAME = "CustomWindowClass";
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = TEXT(CLASS_NAME);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
if (!RegisterClass(&wc)) return 0;
// 创建窗口
HWND hwnd = CreateWindowEx(
0, // 扩展样式
TEXT(CLASS_NAME), // 窗口类
" 绘画", // 窗口标题
WS_OVERLAPPEDWINDOW | WS_VISIBLE, // 窗口样式
CW_USEDEFAULT, CW_USEDEFAULT, // 位置
1000, 1000, // 大小
NULL, // 父窗口
NULL, // 菜单
hInstance, // 实例句柄
NULL // 附加数据
);
if (!hwnd) return 0;
// 消息循环
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
和之前一样,附录也是有的。
先一键三连
#include <windows.h>
// 全局变量存储按钮句柄
HWND hButton,hhh2;
// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
// 创建按钮
hButton = CreateWindow(
"BUTTON", // 窗口类(按钮)
"点击我", // 按钮文本
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // 样式
50, 50, // 位置 (x, y)
100, 30, // 大小 (宽度, 高度)
hwnd, // 父窗口句柄
(HMENU)1, // 控件ID
((LPCREATESTRUCT)lParam)->hInstance, // 实例句柄
NULL // 附加数据
);
// 创建按钮
hhh2 = CreateWindow(
"BUTTON", // 窗口类(按钮)
"点击我", // 按钮文本
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // 样式
150, 50, // 位置 (x, y)
100, 30, // 大小 (宽度, 高度)
hwnd, // 父窗口句柄
(HMENU)1, // 控件ID
((LPCREATESTRUCT)lParam)->hInstance, // 实例句柄
NULL // 附加数据
);
return 0;
}
case WM_COMMAND: {
// 处理按钮点击
if (LOWORD(wParam) == 1) { // 控件ID为1
MessageBox(hwnd, "按钮被点击了!", "提示", MB_OK);
Sleep(1000);
}
return 0;
}
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
// WinMain 函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
const char CLASS_NAME[] = "MyWindowClass";
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
if (!RegisterClass(&wc)) return 0;
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
"按钮示例",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
400, 300,
NULL, NULL, hInstance, NULL
);
if (!hwnd) return 0;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
一个非常非常简单的按钮实例
对了,线上之星期间(虽然结束了)但关注我的,并找我私聊,可以拉票(可以预约)
还有,想要关注的,直接关注我,一天内回关
还有一期清理缓存的,也是直接上代码
#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
using namespace std;
void ClearCache() {
printf("缓存清理中.\n");
Sleep(300);
system("cls");
printf("缓存清理中..\n");
Sleep(300);
system("cls");
printf("缓存清理中...\n");
Sleep(300);
system("cls");
printf("缓存清理中.\n");
Sleep(300);
system("cls");
printf("缓存清理中..\n");
Sleep(300);
system("cls");
printf("缓存清理中...\n");
Sleep(300);
system("cls");
printf("缓存清理中.\n");
Sleep(300);
system("cls");
printf("缓存清理中..\n");
Sleep(300);
system("cls");
printf("缓存清理中...\n");
Sleep(300);
system("cls");
printf("缓存清理完毕\n");
}
void UpSpeed() {
srand(time(0));
printf("加速中.\n");
Sleep(300);
system("cls");
printf("加速中..\n");
Sleep(300);
system("cls");
printf("加速中...\n");
int MB = rand() % 1023 + 1;
int KB = rand() % 1024;
printf("已经腾出了%d MB %d KB空间", MB, KB);
}
void ClearVirus() {
srand(time(0));
printf("查找病毒中.\n");
Sleep(300);
system("cls");
printf("查找病毒中..\n");
Sleep(300);
system("cls");
printf("查找病毒中...\n");
system("cls");
if (rand() % 2 == 0) {
string s = "";
for (int i = 0; i < rand() % 10 + 1; i++) {
char pp = 'a' + rand() % 26;
s = s + pp;
}
printf("发现病毒 ");
for (int i = 0; i < s.size(); i++) {
cout << s[i];
}
printf(".exe,是否清理? 1.Yes 2.No\n");
char c = getch();
if (c == '1') {
printf("清理病毒中.\n");
Sleep(300);
system("cls");
printf("清理病毒中..\n");
Sleep(300);
system("cls");
printf("清理病毒中...\n");
Sleep(300);
system("cls");
printf("清理病毒中.\n");
Sleep(300);
system("cls");
printf("清理病毒中..\n");
Sleep(300);
system("cls");
printf("清理病毒中...\n");
Sleep(300);
system("cls");
printf("清理病毒完成\n");
return;
} else {
printf("请按任意键继续\n");
return;
}
} else {
printf("未发现病毒\n");
return;
}
}
int main() {
while (1) {
cout << "1.清理缓存" << endl;
cout << "2.加速" << endl;
cout << "3.查杀病毒" << endl;
[[maybe_unused]] char c = getch();
switch (c) {
case '1':
system("cls");
ClearCache();
getch();
system("cls");
break;
case '2':
system("cls");
UpSpeed();
getch();
system("cls");
break;
case '3':
system("cls");
ClearVirus();
getch();
system("cls");
break;
default:
break;
}
system("cls");
}
}
作者亲测
(|-呜呜呜,好亏啊-|)
最后,我还要给大家上一期字符串操作的代码
#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
struct Cstring {
private:
string s;
void PrivateRes() {
s = "";
}
void PrivateCsinsert() {
cout << "S:" << s << endl;
int n;
cin >> n;
n = n - 1;
char c;
string m;
for (int i = 0; i <= n - 1; i++) {
c = s[i];
m += c;
}
string ss;
cin >> ss;
m += ss;
for (int i = n; i < s.size(); i++) {
m += s[i];
}
s = m;
}
void PrivatesetCs() {
string sss;
cin >> sss;
s = sss;
}
void PrivatePrintCs() {
cout << s << endl;
}
int PrivateCsSize() {
return s.size();
}
public:
void ReS() {
system("cls");
cout << "Clear:" << endl;
PrivateRes();
system("pause");
}
void Csinsert() {
system("cls");
cout << "Insert:" << endl;
PrivateCsinsert();
system("pause");
}
void SetCs() {
system("cls");
cout << "Set:" << endl;
PrivatesetCs();
system("pause");
}
void printCs() {
system("cls");
cout << "Print:" << endl;
PrivatePrintCs();
system("pause");
}
void printSizeCs() {
system("cls");
cout << "Size:" << endl;
cout<<PrivateCsSize()<<endl;
system("pause");
}
};
int main() {
Cstring Cs;
while (1) {
cout << "1.clear" << endl;
cout << "2.insert" << endl;
cout << "3.set" << endl;
cout << "4.print" << endl;
cout << "5.get size" << endl;
char pp;
pp = getch();
switch (pp) {
case '1':
Cs.ReS();
break;
case '2':
Cs.Csinsert();
break;
case '3':
Cs.SetCs();
break;
case '4':
Cs.printCs();
break;
case '5':
Cs.printSizeCs();
break;
default:
break;
}
system("cls");
}
}
一键三连!!