Search code examples
mfcgdipen

GDI : Get LOGPEN from HGDIOBJ or HPEN


I would like to get LOGPEN structure for currently using HGDIOBJ(which is actually HPEN). Lets assume we have something like this:

CPen ColoredPen;
ColoredPen.Create(...);
...
HGDIOBJ PriorPen = SelectObject(PaintingDC, ColoredPen);

Now I need to get LOGPEN structure from PriorPen. I tried in 2 ways:

1. LOGPEN LogPen;
CPen* pPen = CPen::FromHandle((HPEN)PriorPen);
pPen->GetLogPen(&LogPen);

2. LOGPEN LogPen;
GetObject(PriorPen, sizeof(LogPen), &LogPen);

None of these give me a correct LOGPEN structure object because all fields are 0. I also tried to get LOGPEN for actual CPen and it works perfectly:

ColoredPen.GetLogPen(&LogPen);

but I need to work only with HPEN. So my question is how can I get LOGPEN from HPEN?


Solution

  • You can do the following:

    LOGPEN LogPen;     
    CPen* pTempPen = CPen::FromHandle(hPen);
    pTempPen->GetLogPen(&LogPen);
    

    Please note that this temporary CPen object is valid only until the next time the application has idle time in its event loop, at which time all temporary graphic objects are deleted. In other words, the temporary object is only valid during the processing of one window message.