Thank you Rick, I did almost as you said, but you definitely set me in the right direction. I couldn't use the ExtCreatePen as it returns an HPEN, and I need a CPen. (I tried to find a way to convert but failed, maybe there is one?). So I used the other version of CreatePen:
LOGBRUSH lbrush;
lbrush.lbColor = colour;
lbrush.lbStyle = BS_SOLID;
pen->CreatePen(PS_SOLID | PS_GEOMETRIC | PS_ENDCAP_SQUARE, 500000, &lbrush);
m_CDC->SelectObject(pen);
However, I've no idea why the following didn't flatten the ends:
pen->CreatePen(PS_SOLID | PS_GEOMETRIC | PS_ENDCAP_FLAT, 500000, (COLORREF)colour);
m_CDC->SelectObject(pen);