Thread: Child control creation doubt.

  1. #1
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26

    Child control creation doubt.

    While using the CreateWindow function to create child controls inside the main window do I have to pass the main hInstance to it? I know I have to pass it while creating the main window, but what about child controls? If the answer is no, could you explain why so?

  2. #2
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Yes you'd need to. Well dont ask me why I'm no expert in these matters. You dont need to copy the hInstace to a global variable or some such crap...a GetModuleHandle(NULL) call would give you what you need. Example:

    Code:
     HWND button=CreateWindowEx(0,"BUTTON","Click meh!",BS_PUSHBUTTON|WS_CHILD,10,20,100,30,parentHwnd,(HMENU)cmdId,GetModuleHandle(NULL),NULL);
    Last edited by the_jackass; 01-01-2015 at 10:25 AM. Reason: corrected CreateWindow call to CreateWindowEx
    "Highbrow philosophical truth: Everybody is an ape in monkeytown" --Oscar Wilde

  3. #3
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26
    I mean to say that i have seen some example pass just NULL as that second-to-last argument, instead of hInstance or GetModuleHandle(NULL). Is there a time when we should pass an hInstance and a time when we should pass NULL?

  4. #4
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    "Is there a time when we should pass an hInstance and a time when we should pass NULL?"
    Cant say. I tried with passing NULL right now and it worx. I prefer to be on the safe side though and always pass GetModuleHandle(NULL).

    RegisterWindowEx() always fails when WNDCLASSEX object's hInstance is set to NULL.
    "Highbrow philosophical truth: Everybody is an ape in monkeytown" --Oscar Wilde

  5. #5
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26
    OK. Thank you for your input.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    IIRC (I remember reading it somewhere, but never confirmed if it is so) passing NULL for the hInstance is the same as passing the hInstance which is the first argument of WinMain() for the application.

    It is probably not a good idea to do that if calling CreateWindow() within a DLL, since those windows need the hInstance of the DLL, not the application using the DLL. This distinction may be significant for globally registered window classes (which includes all standard buttons, widgets, etc) but not for window classes that are specific to the application or DLL.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Font creation doubt
    By pc2-brazil in forum Windows Programming
    Replies: 3
    Last Post: 07-02-2013, 09:16 AM
  2. Position of child control..?
    By 39ster in forum Windows Programming
    Replies: 11
    Last Post: 02-28-2008, 07:35 PM
  3. Unknown child control
    By Snip in forum Windows Programming
    Replies: 1
    Last Post: 12-27-2005, 09:27 AM
  4. Changing the Style of a child control
    By filler_bunny in forum Windows Programming
    Replies: 2
    Last Post: 04-28-2004, 05:46 PM
  5. Problems with child process creation
    By Niloc1 in forum C Programming
    Replies: 0
    Last Post: 02-09-2003, 02:52 PM

Tags for this Thread