Windows 7 API 'LineTo' bug
Hi, I think I have stumbled across a bug in the Windows 7 API. When I define a clip region, and then draw a line using 'MoveToEx' and 'LineTo' functions, I get two lines instead of one. And, part of one of the lines is drawn outside the clip region. This does not seem to occur when the clip region is not defined manually, and even when defined manually, it occurs only for certain line slopes. I have tried this on three different machines running Windows 7 (two running the 32-Bit and one running the 64-Bit version), and it reproduces on all machines. Has anyone else encountered this issue before? I would greatly appreciate if anyone could help me figure out what is going on here. I am pasting a minimal code below that reproduces the issue. If this is not the forum to be posting this issue, can someone kindly refer me to the right forum. Thanks a lot, Abhay #include <windows.h> LPCTSTR ClsName = "GDI test"; LPCTSTR WndName = "GDI test"; LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG Msg; //message HWND hWnd; //handle to the window HDC hDeviceContext; //handle to device context WNDCLASSEX WndClsEx; //window class // Create the application window WndClsEx.cbSize = sizeof(WNDCLASSEX); WndClsEx.style = CS_HREDRAW | CS_VREDRAW; WndClsEx.lpfnWndProc = WndProcedure; WndClsEx.cbClsExtra = 0; WndClsEx.cbWndExtra = 0; WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW); WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); WndClsEx.lpszMenuName = NULL; WndClsEx.lpszClassName = ClsName; WndClsEx.hInstance = hInstance; WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // Register the application RegisterClassEx(&WndClsEx); // Create the window object hWnd = CreateWindow(ClsName, WndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); // Find out if the window was created if( !hWnd ) // If the window was not created, return 0; // stop the application // Display the window to the user ShowWindow(hWnd, SW_SHOWNORMAL); UpdateWindow(hWnd); // Decode and treat the messages // as long as the application is running while( GetMessage(&Msg, NULL, 0, 0) ) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { HDC hDC; PAINTSTRUCT ps; switch(Msg) { case WM_PAINT: { hDC = BeginPaint(hWnd, &ps); //create the clip region HRGN clipRegion = CreateRectRgn(0, 0, 0, 0); HRGN rectangle1 = CreateRectRgn(111, 71, 165, 225); HRGN rectangle2 = CreateRectRgn(160, 220, 262, 528); CombineRgn(clipRegion, rectangle1, NULL, RGN_COPY); CombineRgn(clipRegion, rectangle2, clipRegion, RGN_OR); int result = SelectClipRgn(hDC, clipRegion); //draw a stupidly huge rectangle // These lines can be commented and the bug will still reproduce. But these // let you see the clip region, which might help, since the bug only reproduces // when the clip region is set. COLORREF newColor = RGB(rand() * 255 +1, rand() * 255 +1, rand() * 255 +1); HBRUSH hNewBrush = CreateSolidBrush( newColor ); HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hNewBrush); Rectangle(hDC, 0, 0, 700, 700); hNewBrush = (HBRUSH)SelectObject(hDC, hOldBrush); DeleteObject(hNewBrush); //draw the required line MoveToEx(hDC, 112, 72, NULL); LineTo(hDC, 261, 527); EndPaint(hWnd, &ps); } break; case WM_DESTROY: // then close it PostQuitMessage(WM_QUIT); break; default: // Process the left-over messages return DefWindowProc(hWnd, Msg, wParam, lParam); } return 0; }
July 19th, 2010 10:03pm

Hi, This is a development related questions which would be best addressed in MSDN forums. I suggest that you post to the corresponding MSDN forum for better support. MSDN forum can be access from: http://forums.microsoft.com/MSDN. Regards,Please remember to click Mark as Answer on the post that helps you, and to click Unmark as Answer if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. answer your question. This can be beneficial to other community members reading the thread.
Free Windows Admin Tool Kit Click here and download it now
July 21st, 2010 5:46am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics