I omitted the patterns init code because i thought it's not important.
I attach fully working source code. I still have the problem on this small demo too.
On XP, it draws 4 lines but 3 on Win7.
#include <windows.h>
#include <tchar.h>
#include <assert.h>
TCHAR *szTitle = _T("ExtCreatePen DEMO");
TCHAR *szWindowClass = _T("ExtCreatePenClass");
void OnPaint(HDC hdc)
{
static BYTE patterns[] = { 1, 2, 4, 8, 16, 32, 64, 128, 1 };
LOGBRUSH lb;
lb.lbStyle = BS_DIBPATTERN;
lb.lbColor = DIB_RGB_COLORS;
int cb = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 2 + 8*4;
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, cb);
BITMAPINFO* pbmi = (BITMAPINFO*) GlobalLock(hg);
ZeroMemory(pbmi, cb);
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = 8;
pbmi->bmiHeader.biHeight = 8;
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biBitCount = 1;
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biSizeImage = 8;
pbmi->bmiHeader.biClrUsed = 2;
pbmi->bmiHeader.biClrImportant = 2;
pbmi->bmiColors[1].rgbBlue =
pbmi->bmiColors[1].rgbGreen =
pbmi->bmiColors[1].rgbRed = 0xFF;
DWORD* p = (DWORD*) &pbmi->bmiColors[2];
for(int k=0; k<8; k++) *p++ = patterns[k] | patterns[k+1];
GlobalUnlock(hg);
lb.lbHatch = (LONG) hg;
HPEN hNewPen = ExtCreatePen(PS_GEOMETRIC, 1, &lb, 0, NULL);
assert(hNewPen);
GlobalFree(hg);
MoveToEx(hdc, 5,5, NULL);
LineTo(hdc, 100, 5);
HPEN oldpen = (HPEN) SelectObject(hdc, hNewPen);
MoveToEx(hdc, 5,50, NULL);
LineTo(hdc, 150, 50); // drawn on XP but not win7
SetROP2(hdc, R2_XORPEN);
MoveToEx(hdc, 5, 100, NULL);
LineTo(hdc, 200, 100);
SelectObject(hdc, oldpen);
DeleteObject(hNewPen);
SetROP2(hdc, R2_COPYPEN);
MoveToEx(hdc, 5, 150, NULL);
LineTo(hdc, 250, 150);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
switch (message)
{
case WM_PAINT:
BeginPaint(hWnd, &ps);
OnPaint(ps.hdc);
EndPaint(hWnd, &ps);
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASS wcex;
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
return RegisterClass(&wcex);
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HWND hWnd;
MyRegisterClass(hInstance);
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DestroyWindow(hWnd);
return (int) msg.wParam;
}