hello, i have a program to draw a rubber band line, i use classic XOR, in Win32 GDI, this is SetROP2(R2_XORPEN) method, it workd in xp/vista/win7/win8/win10, but recently, after i update win11, found the rubber band line become flickering. this never happen before, is it a win11 bug? how to fix this? thanks!
i write a simple demo program, you can build and run it under win11, and see flickering
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = MyWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = _T("MyRubberBandLine");
wcex.hIconSm = NULL;
RegisterClassEx(&wcex);
HWND hWnd = CreateWindow(_T("MyRubberBandLine"), _T("RubberBandLine"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK MyWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT rect;
GetClientRect(hWnd, &rect);
COLORREF clrbak = SetBkColor(hdc, RGB(255, 0, 0));
ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
int x = (rect.right - rect.left ) / 8;
int y = (rect.bottom - rect.top) / 8;
rect.left += x; rect.right -= x;
rect.top += y; rect.bottom -= y;
SetBkColor(hdc, RGB(0, 255, 0));
ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
rect.left += x; rect.right -= x;
rect.top += y; rect.bottom -= y;
SetBkColor(hdc, RGB(0, 0, 255));
ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
rect.left += x; rect.right -= x;
rect.top += y; rect.bottom -= y;
SetBkColor(hdc, RGB(0, 0, 0));
ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
SetBkColor(hdc, clrbak);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_MOUSEMOVE:
{
static POINT ptPrev1{ -1,-1 };
static POINT ptPrev2{ -1,-1 };
HDC hdc = GetDC(hWnd);
int modebak = SetROP2(hdc, R2_XORPEN);
HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));
HGDIOBJ hOld = SelectObject(hdc, hPen);
// erase prev line
if (ptPrev1.x != -1 && ptPrev2.x != -1)
{
MoveToEx(hdc, ptPrev1.x, ptPrev1.y, NULL);
LineTo(hdc, ptPrev2.x, ptPrev2.y);
ptPrev1.x = ptPrev1.y = ptPrev2.x = ptPrev2.y = -1;
}
// draw new line
RECT rect;
GetClientRect(hWnd, &rect);
POINT ptCenter{ (rect.left + rect.right) / 2, (rect.top + rect.bottom) / 2 };
POINT ptMouse{ LOWORD(lParam), HIWORD(lParam) };
if ((ptMouse.x != ptCenter.x) || (ptMouse.y != ptCenter.y))
{
MoveToEx(hdc, ptCenter.x, ptCenter.y, NULL);
LineTo(hdc, ptMouse.x, ptMouse.y);
ptPrev1 = ptCenter;
ptPrev2 = ptMouse;
}
SelectObject(hdc, hOld);
SetROP2(hdc, modebak);
ReleaseDC(hWnd, hdc);
DeleteObject(hPen);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}