winapic++builderc++builder-10-seattle

SelectObject WinApi function Cannot Compiling in Rad Studio Seatle


I'm trying to compile the below code snippet in Rad Studio Seatle CBuilder 10.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    HPEN hpen, hpenOld;
    HBRUSH hbrush, hbrushOld;
    HDC hdc = this->Canvas->Handle;

    // Red pen for the border
    hpen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
    // Blue brush for the interior.
    hbrush = CreateSolidBrush(RGB(0, 0, 255));

    // Select the new pen and brush then draw a rectangle.
    hpenOld = SelectObject(hdc, hpen);
    hbrushOld = SelectObject(hdc, hbrush);
    Rectangle(hdc, 100,100, 400,250);

    // After using your objects, delete them and restore the originals
    SelectObject(hdc, hpenOld);
    DeleteObject(hpen);
    SelectObject(hdc, hbrushOld);
    DeleteObject(hbrush);
}

But cannot compiling that line and showing error message.

hpenOld = SelectObject(hdc, hpen);

[bcc32 Error] Unit1.cpp(132): E2034 Cannot convert 'void *' to 'HPEN__ *' Full parser context Unit1.cpp(121): parsing: void _fastcall TForm1::Button1Click(TObject *)

I can compile successfully this source code in "c++ builder 6".

Also I've checked SelectObject function declaration in CBuilder 6 and Cbuilder 10

CBuilder 6 Declaration(wingdi.h)

WINGDIAPI HGDIOBJ WINAPI SelectObject(IN HDC, IN HGDIOBJ);

CBuilder 10 Declaration(wingdi.h)

WINGDIAPI HGDIOBJ WINAPI SelectObject(_In_ HDC hdc, _In_ HGDIOBJ h);

I don't seen any different.

What's my problem.

Thanks.


Solution

  • SelectObject returns HGDIOBJ which is not compatible with HPEN. The point is that SelectObject can return a variety of different GDI object types. It's up to you to know what type is returned, and cast accordingly. For instance:

    hpenOld = (HPEN)SelectObject(hdc, hpen);
    

    You'll need to do likewise for the other call to SelectObject.