Thread: GDI: Creating a pen VS taking it as a stock object.

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    5

    GDI: Creating a pen VS taking it as a stock object.

    Hi there. I am studying some Visual C++ MFC programming just to understand some GUI programming in Windows.

    Mind you that my environment is Visual Studio 6, and I'm following the book "Learn Visual C++ Programming in 21 Days".

    (I am following too a book on C++ and one on Direct-X, the first just to make the transition from Java, and the latter because I am interested in Direct X).

    Anyway, my problem (was) this:

    In one of the examples wanted to trace a white line instead of a black one. First I came across (nice MSDN documentation) the DeviceContext.GetStockObject() function, and I tried to pass it to DeviceContext.SelectObject()...

    I tried to do this:

    Code:
    dc.SelectObject((CPen *)(dc.SelectStockObject(WHITE_PEN))
    This did not work, even if it looked as a legal function: the value returned was different than null (signal that the White Pen was loaded) and I think no exception was thrown since the program continued to go on without problems (mind you, I come from a Java background).

    After some research I was convinced to create a white pen by myself and to use it:

    Code:
    CPen zPen(PS_SOLID, 1, RGB(255, 255, 255));
    dc.SelectObject(&zPen);
    This works.
    Why?
    Is the GetStockObject an obsolete function? Does it work in Windows 2000? Did I make a mess out of pointers? (I used some C a long long long time ago... that is 2 years ago, always hated all the pointer stuff )

    Thankyou at least for reading!

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>dc.SelectObject((CPen *)(dc.SelectStockObject(WHITE_PEN))

    I think you are trying to select the pen twice

    try either;

    dc.SelectObject((CPen *)(dc.GetStockObject(WHITE_PEN))

    or

    dc.SelectStockObject(WHITE_PEN);



    By the way, if you change the GDI objects in a DC you MUST return the original before you delete the DC.

    If you create a GDI object then you must remove it from the DC then delete it (or release it if you used a Get function)

    Stock objects do not have to be deleted but must be removed from the dc.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    5
    So you mean something like...

    Code:
    CPen tempPen = dc.GetCurrentPen();
    dc.RemoveObject(tempPen);
    dc.SelectStockObject(WHITE_PEN);
    ?

    Edit:

    Ok, RemoveObject doesn't exist (I wrote it without checking the manuals before).
    What do I have to use in this case to remove the stuff? Haven't found anything, neither watching the CClientDC, nor the CDC member functions.

    SelectStockObject instead works fine ^^;

    Thankyou.
    Last edited by limacat; 03-31-2003 at 01:52 AM.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    the return from SelectObject() is the GDI resource already contained in the DC. Catch it for later use.


    hdc=GetDC(NULL);
    hOldPen=(HPEN*)SelectObject(hdc,hNewPen);

    //draw

    SelectObject(hdc,hOldPen);
    DeleteObject(hNewpen);
    ReleaseDC(NULL,hdc);//if you created the dc then you must use deleteDC
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    5
    Thank you very much novacain. I owe you a beer (If you like beers that is ).

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> If you like beers that is

    He's Australian...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stock Taking program
    By C Babe in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2003, 07:40 PM
  2. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM
  3. Creating object of type HWND from a dll
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-13-2002, 12:40 AM
  4. Replies: 3
    Last Post: 12-03-2001, 01:45 PM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM