gpt4 book ai didi

c++ - win32 c++ 弹出窗口在以编程方式调整大小时闪烁很多

转载 作者:行者123 更新时间:2023-11-30 05:39:47 25 4
gpt4 key购买 nike

我正在展示一个带动画的简单窗口(使用计时器)。

这是我正在使用的示例代码。显示窗口时,它从右侧闪烁很多。虽然隐藏期间,并没有闪烁。此外,如果我删除 WS_POPUP 样式,则不会出现闪烁。

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>

static TCHAR szWindowClass[] = _T("win32app");

HWND main_wnd_ = 0;
DWORD timer_id = 1000;
DWORD timer_id_2 = 1001;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

BOOL
register_main_window_class(HINSTANCE hinstance) {
WNDCLASSEX wcx;
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = WndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hinstance;
wcx.hIcon = 0;
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = CreateSolidBrush(0xFF0000);
wcx.lpszMenuName = 0;
wcx.lpszClassName = _T("MainWndClass");
wcx.hIconSm = 0;
return RegisterClassEx(&wcx);
}


void CALLBACK show(HWND hwnd, UINT msg, UINT id, DWORD time) {
RECT r = { 0 };
::GetWindowRect(hwnd, &r);
if (r.left > 400) {
SetWindowPos(hwnd, 0, r.left - 10, r.top, r.right - r.left + 10, r.bottom - r.top, SWP_NOZORDER);
} else {
KillTimer(hwnd, timer_id);
}
}

void CALLBACK hide(HWND hwnd, UINT msg, UINT id, DWORD time) {
RECT r = { 0 };
::GetWindowRect(hwnd, &r);
if (r.left < 1500) {
SetWindowPos(hwnd, 0, r.left + 10, r.top, r.right - r.left - 10, r.bottom - r.top, SWP_NOZORDER);
} else {
DestroyWindow(hwnd);
KillTimer(hwnd, timer_id_2);
}
}

BOOL
create_main_window(HINSTANCE hinstance) {
main_wnd_ = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TOPMOST,
_T("MainWndClass"), _T(""),
WS_VISIBLE | WS_POPUP,
1500, 800, 0, 200,
0, 0, hinstance, 0);
if (!main_wnd_) {
return FALSE;
}
ShowWindow(main_wnd_, SW_SHOW);
UpdateWindow(main_wnd_);
SetTimer(main_wnd_, timer_id, 10, (TIMERPROC)show);
return TRUE;
}

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
int x = 0;
if (!register_main_window_class(hInstance)) {
x = 0;
}
if (!create_main_window(hInstance)) {
x = 0;
}

MSG msg;
BOOL result;
while ((result = GetMessage(&msg, 0, 0, 0)) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 1;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
SetTimer(main_wnd_, timer_id_2, 10, (TIMERPROC)hide);
break;
}

return DefWindowProc(hWnd, message, wParam, lParam);
}

最佳答案

这是 Win32 API 中的一个严重问题。并且没有简单的解决方案。

您可以 try catch 顶层 WM_ENTERSIZEMOVE 并使用 WM_PRINT 方法将控件绘制到位图中,然后使用(缩放的)位图将对话框控件重新绘制到另一个位图中,然后交换这两个位图。

添加此双缓冲需要大量工作,如果 WM_PRINT 在某些第 3 方客户端控件(如所有 MFC 功能包)上不起作用,则无法正常工作。

但是当你完成它时它工作正常。您还需要它来实现所有其他使 MacOSX 如此出色的快速 GUI 动画。

关于c++ - win32 c++ 弹出窗口在以编程方式调整大小时闪烁很多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32116317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com