Why typecast with HBRUSH in the WNDCLASS

This is a discussion on Why typecast with HBRUSH in the WNDCLASS within the Windows Programming forums, part of the Platform Specific Boards category; When I/you declare the fields of the WNDCLASS, the one of them is hbrBackground. Well, the way petzold told me ...

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Why typecast with HBRUSH in the WNDCLASS

    When I/you declare the fields of the WNDCLASS, the one of them is hbrBackground. Well, the way petzold told me to set the background of the window class is this:
    Code:
    wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    Why do you need to typecast with the HBRUSH type?

    Thanks.

    --Garfield
    1978 Silver Anniversary Corvette

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    because GetStockObject() returns a HGDIOBJ and we need a HBRUSH hence the cast.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    3
    Hi Garfield,

    The return from GetStockObject is a handle to a GDI object (HGDIOBJ). Some compilers may be picky and give you a compiler error if it's not cast to the exact type of the structure member.

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Thanks for the explanation!

    --Garfield
    1978 Silver Anniversary Corvette

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Compilers whould warn this it but they shouldn't give an error. I mean, both HBRUSH and HGDIOBJ are DWORDs (There are only three regular datatypes in the world!).
    // Gliptic

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. x.h header file
    By Ducky in forum Windows Programming
    Replies: 11
    Last Post: 11-01-2008, 12:12 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 06:36 AM
  3. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  4. '=' : cannot convert from 'void *' to 'struct HBRUSH__ *'
    By Bajanine in forum Windows Programming
    Replies: 9
    Last Post: 10-14-2002, 08:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21