Like Tree2Likes
  • 1 Post By CommonTater
  • 1 Post By novacain

The handle in WM_CREATE

This is a discussion on The handle in WM_CREATE within the Windows Programming forums, part of the Platform Specific Boards category; I would like to use the handle of CreateWindow in WM_CREATE outside of WM_CREATE to update the picture in the ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    796

    The handle in WM_CREATE

    I would like to use the handle of CreateWindow in WM_CREATE outside of WM_CREATE to update the picture in the listbox but i get
    error 1400 Invalid window handle.

    Code:
    switch(uMsg)
        {
        case WM_CREATE:
        {
            hwndImage = CreateWindowEx(0, WC_LISTVIEW,
                                       TEXT(""), WS_CHILD | WS_VISIBLE,
                                       0, 0, width, height,
                                       hwnd, NULL,
                                       (HMODULE) GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
                                       NULL);
    
            hWnd_Image2 = hwndImage;
            ImageBox_SetImage(hwndImage, TEXT("D:\\1.jpg"));
            return 0;
        }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What I usually do is make a global array of window handles and assign into it from the CreateWindow() calls. I also save the HINSTANCE parameter from the WinMain globally. It saves a lot of time and code figuring these things out on the fly.

    Code:
    HINSTANCE PgmInst;                    // global instance handle
    HWND        Wind[10];              // global window handles
    
    
    
    //create windows
        // main window
        Wind[0] = CreateWindowEx(WS_EX_CONTROLPARENT | WS_EX_APPWINDOW,
                      RMC_CLASS,L"Remote Media Client", 
                      WS_TILED | WS_SYSMENU | WS_MINIMIZEBOX,// | WS_VISIBLE,
                      CW_USEDEFAULT,CW_USEDEFAULT,500,400,NULL,NULL,PgmInst,NULL);
    
        Wind[1] = CreateWindow(L"BUTTON",L" Server ",
                      WS_CHILD | WS_VISIBLE |
                      BS_GROUPBOX,
                      10,5,180,182,Wind[0],NULL,PgmInst,NULL);
    
        Wind[2] = CreateWindow(L"COMBOBOX",NULL,
                      WS_CHILD | WS_VISIBLE | WS_TABSTOP |
                      CBS_DROPDOWNLIST | CBS_SORT,
                      20,20,160,100,Wind[0],(HMENU)100,PgmInst,NULL); 
        SendMessage(Wind[2],CB_SETEXTENDEDUI,1,0);
    
    // etc.
    Last edited by CommonTater; 06-15-2011 at 06:55 AM.
    Ducky likes this.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    If you use the WS_CHILD style when creating a window then the HMENU param should be the ID# of the window (cast to a HMENU).

    This means you do not have to save all the HWNDs but can use GetDlgItem() to retrieve the HWND as required (works with any child, not just controls).

    You should not use a NULL HMENU param when creating a control/window with the WS_CHILD style.

    Windows/dialogs with multiple controls with the same ID (apart from static controls) may experience undefined behaviour (as the ID number may be used to identify which child control notified the parent about some event).
    Ducky likes this.
    "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

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Great! Thanks a lot CommonTater and Novacain!
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. First call is WM_PAINT or WM_CREATE ?
    By Ducky in forum Windows Programming
    Replies: 7
    Last Post: 09-02-2008, 03:36 AM
  2. What you should know about WM_CREATE
    By maxorator in forum Windows Programming
    Replies: 4
    Last Post: 09-26-2006, 02:05 PM
  3. WM_CREATE and BOOLs
    By kidburla in forum Windows Programming
    Replies: 3
    Last Post: 09-18-2006, 11:55 PM
  4. WM_PAINT and WM_CREATE
    By actionbasti in forum Windows Programming
    Replies: 5
    Last Post: 11-25-2003, 09:17 PM
  5. WM_CREATE not working??
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 05-09-2002, 02:17 AM

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