2022.7.30
新版:(转 https://qa.codebus.cn/question/2260)
#include <windows.h>
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <time.h>
bool change_size = false;
int change_size_width;
int change_size_height;
WNDPROC EasyXProcess;
LRESULT MyWindowProcess(HWND handle, UINT message, WPARAM wparam, LPARAM lparam) {
switch (message) {
case WM_SIZING: {
RECT* rect = (RECT*)lparam;
change_size = true;
change_size_width = rect->right - rect->left - 18;
change_size_height = rect->bottom - rect->top - 39;
Resize(NULL, change_size_width, change_size_height);
cleardevice();
FlushBatchDraw();
return 0;
}
}
return EasyXProcess(handle, message, wparam, lparam);
}
int main() {
initgraph(500, 300, SHOWCONSOLE);
// 取消拖动改变大小限制
SetWindowLong(GetHWnd(), GWL_STYLE, GetWindowLong(GetHWnd(), GWL_STYLE) | (WS_MAXIMIZEBOX));
SetWindowLong(GetHWnd(), GWL_STYLE, (GetWindowLong(GetHWnd(), GWL_STYLE) | WS_THICKFRAME));
EasyXProcess = reinterpret_cast<WNDPROC>(GetWindowLongPtr(GetHWnd(), GWLP_WNDPROC));
SetWindowLongPtr(GetHWnd(), GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(MyWindowProcess));
// 计时器
time_t timer = clock();
setbkcolor(RGB(41, 42, 43));
BeginBatchDraw();
cleardevice();
FlushBatchDraw();
while (true) {
Sleep (10);
//if (clock() - timer >= 38) {
// if (change_size == true) {
// change_size = false;
// // Resize(NULL, change_size_width, change_size_height);
// cleardevice();
// FlushBatchDraw();
// }
//}
}
EndBatchDraw();
_getch();
return 0;
}
旧版:
#include <graphics.h>
#include <conio.h>
static WNDPROC OldProc;
static WORD nWidth, nHeight;
static bool size_flag = false;
LRESULT CALLBACK NewProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_SIZE:
{
// 在EasyX默认的处理函数前先将窗口大小调整消息处理一次
nWidth = LOWORD(lParam); // width of elient area
nHeight = HIWORD(lParam); // height of client area
size_flag = true;
}
default:
return CallWindowProc(OldProc, hWnd, message, wParam, lParam);
}
}
HWND new_graph_window(int width, int height)
{
// 设置窗口样式,使窗口大小可调
HWND graphwindow = initgraph(width, height);
LONG oldstyle = GetWindowLong(graphwindow, GWL_STYLE);
SetWindowLong(graphwindow, GWL_STYLE, oldstyle | WS_MAXIMIZEBOX | WS_SIZEBOX);
OldProc = (WNDPROC)GetWindowLong(graphwindow, GWL_WNDPROC);
SetWindowLong(graphwindow, GWL_WNDPROC, (LONG)NewProc);
return graphwindow;
}
int main()
{
new_graph_window(640,480);
BeginBatchDraw();
nWidth = getwidth();
nHeight = getheight();
setbkcolor(WHITE);
setfillcolor(BLACK);
setlinecolor(BLACK);
while (true)
{
if (size_flag)
{
Resize(NULL,nWidth,nHeight);
size_flag = false;
}
cleardevice();
fillcircle(nWidth / 2, nHeight / 2, nWidth > nHeight ? nHeight / 2 : nWidth / 2);
FlushBatchDraw();
}
return 0;
}
上述代码由如下帖吧稍加改动而成,可以直接编译运行。
https://tieba.baidu.com/p/6014139638?pid=123764910327&cid=0#123764910327