4

i'm new to C++ as well as for windows programming..

i have created a window using msdn CreateWindow() function

which works correctly..now i would like to create a child window...the parent window should control the child window...

Any helps sample code regarding this .

Thanks in advance

Shog9
  • 156,901
  • 35
  • 231
  • 235

4 Answers4

7

Roughly speaking, in the handler for the parent, where you wish to create the child, you call CreateWindow, passing in the window for the parent as the hwndParent - probably, you also want to set certain styles on the child such as WS_CHILD. Your interaction with the child window then depends on the type of the window you created. Some windows (such as buttons) are designed to work as child windows, so they send a lot of notification messages, so you would set up your parent to listen for those notification messages.

1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
6

It Is 2018 by now.. doing some retro work on this I found SetParent() to come out handy, to assure a child window remains inside the client region of the parent.. Before SetParent() the child need not be registered as a child. In CreateWindowEx the parent handle can be NULL at first. Style WS_CHILD is not needed, but WS_CLIPSIBLINGS comes out handy, it avoids flickering.

This is my code for creating a child window:

    HWND hwnd = CreateWindowExA(0, "WindowOfDLL", ctitle, WS_SIZEBOX | WS_CLIPSIBLINGS, CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, hMenu, inj_hModule, NULL);
    SetParent(hwnd, hwndparent);
    ShowWindow(hwnd, SW_SHOWNORMAL);

I've seen no problems (yet) with threading in Win10. Each of below child windows is created in a DLL which manages the assembly. When a child window is created, the DLL adds a new member to the assembly and launches a thread, which will serve the child window and performs above code in its initialisation.

parent window with WS_CLIPCHILDREN

Goodies
  • 1,951
  • 21
  • 26
  • So I'm thinking about posting my own question related to getting a child window to work because I have not been able to get one to load. I've used your line with some changes - HWND hwnd = CreateWindowExA(0, "WindowOfDLL", "child", WS_SIZEBOX | WS_CLIPSIBLINGS, CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, NULL, hInstance, NULL); - gave explicit child name, removed the menu, and pointed at my hInstance. I have been unsuccessful in getting one to load. Could you post more of your code so I can see what I'm doing wrong? – Brad B. Jan 17 '19 at 21:18
6

I'd highly recommend having a read through Charles Petzold's "Programming Windows" if you can obtain a copy.

Otherwise, to answer your question, pass the parent window's handle as the parent when you create the child window (using either CreateWindow or CreateWindowEx):

HWND CreateWindowEx
(      
    DWORD dwExStyle,
    LPCTSTR lpClassName,
    LPCTSTR lpWindowName,
    DWORD dwStyle,
    int x,
    int y,
    int nWidth,
    int nHeight,
    HWND hWndParent,      /// pass the parent window handle here
    HMENU hMenu,
    HINSTANCE hInstance,
    LPVOID lpParam
);

..As 1800 Info has also stated, perhaps also set the WS_CHILD style (more oon Window Styles here). This is just the rudimentary plumbing, really..

Can you be a bit more specific when you say "control the child window..."?

RobS
  • 9,382
  • 3
  • 35
  • 63
0
"could you post more of your code "

@BradB you're right.. our answers are all not very specific, this is old stuff it is difficult to put a complete frame in one post. Read the book RobS suggested I would say... and... take a look at Visual studio and more modern methods to design "child windows", like Winforms, WPF and Universal !

Here is some more of my legacy code, just the preparations, to let it be a CHILD window. The events etc you will have to fill in yourself..

 BOOL RegisterDLLWindowClass(LPCWSTR s)
    {
      WNDCLASSEX wc;
      wc.hInstance = NULL; // inj_hModule;
      wc.lpszClassName = s;
      wc.lpfnWndProc = DLLWindowProc;
      wc.style = CS_DBLCLKS;
      wc.cbSize = sizeof(WNDCLASSEX);
      wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
      wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
      wc.hCursor = LoadCursor(NULL, IDC_ARROW);
      wc.lpszMenuName = NULL;
      wc.cbClsExtra = 0;
      wc.cbWndExtra = 0;
      wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
      if (!RegisterClassEx(&wc))
        return 0;
    }

    HMENU CreateDLLWindowMenu()
        {
          WriteLine("Create menu");
          HMENU hMenu;
          hMenu = CreateMenu();
          HMENU hMenuPopup;
          if (hMenu == NULL)
            return FALSE;
          hMenuPopup = CreatePopupMenu();
          AppendMenu(hMenuPopup, MF_STRING, MYMENU_FILTERSOFF, TEXT("Off"));
          AppendMenu(hMenuPopup, MF_STRING, MYMENU_CANNY, TEXT("Canny"));
          AppendMenu(hMenuPopup, MF_STRING, MYMENU_SOBEL, TEXT("Sobel"));
          AppendMenu(hMenuPopup, MF_STRING, MYMENU_THRESHOLD, TEXT("Threshold"));
          AppendMenu(hMenuPopup, MF_STRING, MYMENU_DILATE, TEXT("Dilate"));
          AppendMenu(hMenuPopup, MF_STRING, MYMENU_HARRIS, TEXT("Harris"));
          CheckMenuItem(hMenuPopup, 0, MF_BYPOSITION | MF_CHECKED);

          AppendMenuW(hMenu, MF_POPUP, (UINT_PTR)hMenuPopup, TEXT("Filter"));

          HMENU hMenuPopup2 = CreatePopupMenu();
          AppendMenu(hMenuPopup2, MF_STRING, MYMENU_SAMPLE1, TEXT("Change parameter"));
          AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hMenuPopup2, TEXT("Test"));
          return hMenu;
        }

    void WINAPI CreateAWindow(PassedToDLL *lpParam, DWORD dwStyle)
        {   
          if (nInstances == 0) RegisterDLLWindowClass((LPCWSTR)L"WindowOfDLL");
          HMENU hMenu = CreateDLLWindowMenu();
          nInstances++;
          CommonInfo[nInstances - 1] = lpParam;
          int i = 20 + 4 * (rand() % 10);
          lpParam->p = NewPanningStruct();
          lpParam->hWndChild = CreateWindowEx(0, L"WindowOfDLL", lpParam->title,
                dwStyle,
                i, i, 800, 550, lpParam->hWndParent, hMenu, NULL, NULL);
          if (wcslen(lpParam->filename) > 0)
          {
            LoadBitmapFromBMPFileW(lpParam->hWndChild, lpParam->filename, &(lpParam->hBmp),
                &(lpParam->hPalette));
          } 
        }
Goodies
  • 1,951
  • 21
  • 26