跳到正文

科技·工程

qwinku0.5

zhiyin123约 22 分钟
原文

这是 2025年10月5日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2024年3月1日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 22 分钟

版本

  1. v2当前
  2. v1

编译器命令

代码
-std=c++14 -Wall -mwindows -O3

连接器命令

代码
-static-libgcc -lmsimg32

示例代码

C++
#include "qwinku.cpp"
namespace winku{
	void set_win(winmain_arg_list_t list){
		win_info.class_name = "test";
		win_info.caption = "test";
		return ;
	}
	void main(proc_arg_list_t list){
		pic_layer_map[0].tmp_color = RGB(200, 200, 0);
		
		pic_layer_map[0].bfs(1, 1);
		
		pic_layer_map[1];
		return ;
	}
	void win_proc(proc_arg_list_t arg){
		static bool need_draw = false;
		static int last_x = 0, last_y = 0;
		switch(arg.message){
	case WM_LBUTTONDOWN:
			need_draw = true;
			need_update = true;
			last_x = LOWORD(arg.lparam);
			last_y = HIWORD(arg.lparam);
			break;
	case WM_LBUTTONUP:
			need_draw = false;
	case WM_MOUSEMOVE:
			int now_x = LOWORD(arg.lparam), now_y = HIWORD(arg.lparam);
			if (need_draw){
				need_update = true;
				pic_layer_map[1].line(last_x, last_y, now_x, now_y, 3);
			}
			last_x = now_x; last_y = now_y;
		}
	/*	static HPEN pen = CreatePen(PS_SOLID, 10, RGB(255, 0, 0));
		static HBRUSH brush = CreateSolidBrush(RGB(255, 255, 0));
		HDC hdc = GetDC(list.hwnd);
		SelectObject(hdc, pen);
		SelectObject(hdc, brush);
		Rectangle(hdc, 100, 100, 200, 200);
		ReleaseDC(list.hwnd, hdc);*/
		return ;
	}
}

库代码

C++
#ifndef __MIAO_MIAO_WINMAN__
#define __MIAO_MIAO_WINMAN__ __miao_miao__
#include <cassert>
#include <windows.h>
#include <string>
#include <queue>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <map>
namespace winku{
	
	struct pos_t{
		int x, y;
		pos_t():x(0),y(0){}
		pos_t(int _x, int _y):x(_x), y(_y){}
	};
	
	namespace math{
		long long pow_2(long long d){
			return d * d;
		}
	}
	
	namespace mem{
		template<typename Tp>
		void alloc(Tp*& arr, int si){
			arr = new Tp[si];
			return ;
		}
		
		template<typename Tp, typename... Arg>
		void alloc(Tp*& arr, int si, Arg... ls){
			arr = new Tp[si];
			for (int i = 0; i < si; i++){
				alloc(arr[i], ls...);
			}
		}
		
		template<typename Tp>
		void free(Tp* arr){
			delete[] arr;
			return ;
		}
		
		template<typename Tp, typename... Arg>
		void free(Tp* arr, int si, Arg... ls){
			for (int i = 0; i < si; i++) free(arr[i], ls...);
			delete[] arr;
			return ;
		}
	}
	
	std::ofstream ferr("err.out"); using std::endl;
	
	constexpr int SIZE_OF_ST = 1024;
	
	struct win_info_t{
		std::string class_name;
		std::string caption;
		int win_pos_x;
		int win_pos_y;
		int win_width;
		int win_height;
		int background;
	//	std::string cursor;
	//	std::string icon;
		std::string menu_name;
		int style;
		win_info_t():
			class_name("DefautClassName"),
			caption("DefautCaption"),
			win_pos_x(100),
			win_pos_y(100),
			win_width(1280),
			win_height(960),
			background(0),
	//		cursor(IDC_ARROW),
	//		icon(IDI_APPLICATION),
			menu_name("DefautMenuName"){}
	} win_info;
	
	struct winmain_arg_list_t{
		HINSTANCE hInst;
		HINSTANCE hPrev;
		std::string lpCmdLine;
		int nShowCmd;
		winmain_arg_list_t(){}
		winmain_arg_list_t(HINSTANCE _hInst, 
					HINSTANCE _hPrev, 
					std::string _lpCmdLine, 
					int _nShowCmd):
					hInst(_hInst),
					hPrev(_hPrev),
					lpCmdLine(_lpCmdLine),
					nShowCmd(_nShowCmd){}
	};
	
	struct proc_arg_list_t{
		HWND hwnd;
		unsigned message;
		unsigned long long wparam;
		long long lparam;
		proc_arg_list_t(){}
		proc_arg_list_t(HWND _hwnd,
				unsigned _message,
				unsigned long long _wparam,
				long long _lparam):
				hwnd(_hwnd),
				message(_message),
				wparam(_wparam),
				lparam(_lparam){}
	};
	
	struct dot_t{
		bool alive;
		COLORREF color;
		dot_t():alive(false), color(RGB(0, 0, 0)){}
		void clear(){
			alive = false;
			color = RGB(0, 0, 0);
			return ;
		}
		void set(COLORREF _color){
			color = _color;
			alive = true;
		}
		bool operator==(dot_t& _b){
			return alive == _b.alive && color == _b.color;
		}
	};
	
	struct bitmap_t{
		static constexpr unsigned short red = 2, green = 1, blue = 0;
		unsigned char* arr;
		unsigned short width, height, byte_len;
		bitmap_t():arr(nullptr), width(0), height(0), byte_len(0){}
		void clear(){
			std::fill(arr, arr + byte_len * height, 0);
			return ;
		}
		void res(){
			width = win_info.win_width;
			height = win_info.win_height;
			byte_len = width * 3 & (~3);
			arr = new unsigned char[byte_len * height + 1000];
			clear();
			return ;
		}
		unsigned char& at(unsigned short x, unsigned short y, unsigned short col){
			return arr[x * 3 + y * byte_len + col];
		}
		void set(int x, int y, COLORREF color){
			at(x, y, red) = GetRValue(color);
			at(x, y, green) = GetGValue(color);
			at(x, y, blue) = GetBValue(color);
			return ;
		}
	};
	
	namespace _bfs_help{
		static constexpr int rdx[4] = {1, -1, 0, 0};
		static constexpr int rdy[4] = {0,  0, 1,-1};
	}
	
	struct pic_layer_t{
		dot_t** arr;
		int maxx, maxy;
		COLORREF tmp_color;
		void res(){
			mem::alloc(arr, win_info.win_width, win_info.win_height);
			maxx = win_info.win_width; maxy = win_info.win_height;
			return ;
		}
		
		pic_layer_t(int _hahaha):arr(nullptr), maxx(-1), maxy(-1), tmp_color(RGB(225, 0, 0)){}
		
		pic_layer_t():arr(nullptr), maxx(-1), maxy(-1), tmp_color(RGB(225, 0, 0)){
			res();
		}
		~pic_layer_t(){
			if(arr != nullptr){
				mem::free(arr, win_info.win_width); 
			}
		}
		bool is_ok(int x, int y){
			return 0 <= x && x < maxx && 0 <= y && y < maxy;
		}
		bool set(int x, int y, bool is_clear = false){
			if (!is_ok(x, y)) return false;
			if (is_clear){
				arr[x][y].clear();
			}else{
				arr[x][y].set(tmp_color);
			}
			return true;
		}
		void clear(){
			for (int x = 0; x < maxx; x++){
				std::fill(arr[x], arr[x] + maxy, dot_t());
			}
			return ;
		}
		
		void bfs(int x, int y, bool is_clear = false){
			using namespace _bfs_help;
			static bool** vis = nullptr;
			if (vis == nullptr){
				mem::alloc(vis, maxx + 1, maxy + 1);
			}
			for (int i = 0; i < maxx; i++){
				std::fill(vis[i], vis[i] + maxy, false);
			}
			std::queue<pos_t> wt; wt.emplace(x, y);
			set(x, y, is_clear); vis[x][y] = true;
			while(!wt.empty()){
				pos_t tmp = wt.front(); wt.pop();
				for (int i = 0; i < 4; i++){
					pos_t n(tmp.x + rdx[i], tmp.y + rdy[i]);
					if (!is_ok(n.x, n.y) || vis[n.x][n.y]) continue;
					vis[n.x][n.y] = true;
					wt.push(n);
					set(n.x, n.y, is_clear);
				}
			}
			return ;
		}
		void dot(int x, int y, int r = 2, bool is_clear = false){
			using namespace math;
			for (int dx = 0; dx <= r; dx++){
				for (int dy = 0; dy <= r; dy++){
					if (pow_2(dx) + pow_2(dy) <= pow_2(r)){
						set(x + dx, y + dy, is_clear);
						set(x - dx, y + dy, is_clear);
						set(x - dx, y - dy, is_clear);
						set(x + dx, y - dy, is_clear);
					}
				}
			}
			return ;
		}
		void line(int x1, int y1, int x2, int y2, int r = 2, bool is_clear = false){
			if (x2 == x1){
				if (y1 > y2) std::swap(y1, y2);
				for (int y = y1; y <= y2; y++){
					dot(x1, y, r, is_clear);
				}
			}else if(abs(y1 - y2) < abs(x1 - x2)){
				if (x1 > x2){
					std::swap(x1, x2);
					std::swap(y1, y2);
				}
				for (int x = x1; x <= x2; x++){
					dot(x, 1ll * (y2 - y1) * (x - x1) / (x2 - x1) + y1, r, is_clear);
				}
			}else{
				if (y1 > y2){
					std::swap(x1, x2);
					std::swap(y1, y2);
				}
				for (int y = y1; y <= y2; y++){
					dot(1ll * (x2 - x1) * (y - y1) / (y2 - y1) + x1, y, r, is_clear);
				}
			}
		}
		void draw_to(pic_layer_t& other){
			for (int x = 0; x < maxx; x++){
				for (int y = 0; y < maxy; y++){
					if (!arr[x][y].alive) continue;
					other.tmp_color = arr[x][y].color;
					other.set(x, y);
				}
			}
			return ;
		}
		void draw_to(bitmap_t& bit_map){
			for (int x = 0; x < maxx; x++){
				for (int y = 0; y < maxy; y++){
					if (!arr[x][y].alive) continue;
					bit_map.set(x, y, arr[x][y].color);
				}
			}
			return ;
		}
	};
	
	bitmap_t main_bit_map;
	BITMAPINFO main_bit_map_info;
	
	void set_win(winmain_arg_list_t);
	
	void win_proc(proc_arg_list_t);
	
	void main(proc_arg_list_t);
	
	namespace _hdc_space{
		HDC hdc, hmap_dc;
		HBITMAP hmap;
	}
	
	void _begin_work(proc_arg_list_t arg){
		using namespace _hdc_space;
		main_bit_map.res();
		main_bit_map_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		main_bit_map_info.bmiHeader.biWidth = main_bit_map.width;
		main_bit_map_info.bmiHeader.biHeight = -main_bit_map.height;
		main_bit_map_info.bmiHeader.biPlanes = 1;
		main_bit_map_info.bmiHeader.biBitCount = 24;
		main_bit_map_info.bmiHeader.biCompression = BI_RGB;
		main_bit_map_info.bmiHeader.biSizeImage = 0;
		main_bit_map_info.bmiHeader.biClrUsed = 0;
		main_bit_map_info.bmiHeader.biClrImportant = 0;//填写位图头
		
		hdc = GetDC(arg.hwnd);
		
		hmap_dc = CreateCompatibleDC(nullptr);
		
		hmap = CreateCompatibleBitmap(hdc, win_info.win_width, win_info.win_height);
		
		SelectObject(hmap_dc, hmap);
		return ;
	}
	
	std::map<int, pic_layer_t> pic_layer_map;
	
	bool need_update = false;
	
	void _update_window(proc_arg_list_t list){
		using namespace _hdc_space;
		main_bit_map.clear();
		
		for (auto& tmp_map : pic_layer_map){
			tmp_map.second.draw_to(main_bit_map);
		}
		
		SetDIBits(hmap_dc, hmap, 0, win_info.win_height, main_bit_map.arr,
			&main_bit_map_info, DIB_RGB_COLORS);
		
		BitBlt(hdc, 0, 0,
			win_info.win_width, win_info.win_height,
			hmap_dc, 0, 0, SRCCOPY);
		return ;
	}
	
	void _end_work(proc_arg_list_t list){
		using namespace _hdc_space;
		ReleaseDC(list.hwnd, hdc);
		exit(0);
		return ;
	}
	
	long long _window_proc(HWND hwnd, unsigned message, unsigned long long wparam, long long lparam){
		static bool is_begin = true;
		auto list = proc_arg_list_t(hwnd, message, wparam, lparam);
		need_update = false;
		if (is_begin){
			_begin_work(list);
			main(list);
			is_begin = false;
		}
		switch(message){
	case WM_QUIT:
	case WM_CLOSE:
	case WM_DESTROY:
			_end_work(list); 
			break;
	default:
			break;
		}
		win_proc(list);
		_update_window(list);
		return DefWindowProc(hwnd, message, wparam, lparam);
	}
	
}
int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrev, char* lpCmdLine, int nShowCmd){
	
	using namespace winku;
	
	set_win(winmain_arg_list_t(hInst, hPrev, lpCmdLine, nShowCmd));
	
	WNDCLASSA wc;
	
	memset(&wc, 0, sizeof(wc));
	wc.hbrBackground = (HBRUSH)GetStockObject(win_info.background);//-mwindows
	wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
	wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
	wc.lpszClassName = win_info.class_name.c_str();
	wc.hInstance = hInst;
	wc.lpfnWndProc = _window_proc;
	wc.lpszMenuName = win_info.menu_name.c_str();
	wc.style = win_info.style;
	RegisterClassA(&wc);
	
	HWND hwnd = 
		CreateWindowA(win_info.class_name.c_str(),
				win_info.caption.c_str(),
				WS_VISIBLE | WS_OVERLAPPEDWINDOW,
				win_info.win_pos_x, win_info.win_pos_y,
				win_info.win_width,  win_info.win_height,
				nullptr, nullptr, hInst, nullptr);
				
	ShowWindow(hwnd, SW_SHOWNORMAL);
    UpdateWindow(hwnd);
    
	MSG message;

	while(true){
		if (GetMessage(&message, nullptr, 0, 0)){
			TranslateMessage(&message);
			DispatchMessageA(&message);
		}
	}
	
	return 0;
}
#endif