Results 1 to 7 of 7

Thread: SelectObject

  1. #1

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Unhappy SelectObject

    Code:
    HPEN pen_use; //pen to use
    HPEN pen_old; 
    
    pen_use = GetPen(1,GetSysColor(COLOR_BTNSHADOW));
    pen_old = SelectObject(m_hDC, pen_use);
    SelectObject(m_hDC, pen_old);
    DeleteObject(pen_use);
    O, GetPen is a function I wrote, it works fine. But what I don't understand, is that it gives me this error when i compile:
    Code:
    error C2440: '=' : cannot convert from 'void' to 'struct HPEN__ *'
            Expressions of type void cannot be converted to other types
    I went on msdn and they even have an example doing the same thing, and they shay it shyould workd. Help? Thanks
    Amon Ra
    Amon Ra
    The Power of Learning.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    What does GetPen return? You should just be able to use a C-style cast "(HPEN)(a_void*_pointer)".
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Wink sorry...

    i pressed the wrong button, so i answered to you in another thread it is named "here..."
    Amon Ra
    The Power of Learning.

  4. #4

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500
    Thanks again Parksie So, is it possible to do a cast everytime there is a void? Because, if it is void, it does not return anything, right. So how can you convert nothing to an HPEN?
    Amon Ra
    Amon Ra
    The Power of Learning.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    void is nothing, and can't be casted. void* is different - it's a generic pointer, and so could be anything. C allows you to do this:
    Code:
    int *p;
    void *x;
    int a;
    
    p = &a; /* ok in C and C++ */
    x = p; /* ok in C and C++ */
    p = x; /* only ok in C, C++ needs (int*)x */
    The reason you can cast to HPEN is because you provided an HPEN, and by design SelectObject returns the same type of GDI object, even though technically it's the same type. However, since you know better, you can override the compiler and cast it.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Question so..

    Code:
    p = x; /* only ok in C, C++ needs (int*)x */
    this line converts x to an int*, right?
    Thanks
    Amon Ra
    Amon Ra
    The Power of Learning.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    C will automatically convert the pointer, but in C++ this violates the basic type safety rules, and as such is forbidden by the compiler without a cast.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width