1

My question is: What needs to be done to make a functional edit control in a child window with a caption?

I've been developing a wrapper for Win32 and ran into a little trouble. Overlapped Windows with child captioned windows don't seem to fully activate or receive focus. Maybe it's because of this lack of focus that an edit control in the child captioned window will not function.

The experiment to isolate my error is as follows:

  1. Setup a window class
  2. Register that class
  3. Create an overlapped window with the class
  4. Create a child window with a caption using the same class
  5. Create an edit control in the child window

The observations are in accordance with my wrapper project: The child window can be moved and sized depending on style bits. While being selected, its caption doesn't change color to imply that it is in focus. The Edit control does not respond to clicking. No caret appears. Button controls within the child window respond normally.

Additionally, removing the caption from the child window allows the edit control to be interacted with properly.

Here is the test code:

// Create a WNDCLASSEX for registering windows
WNDCLASSEX wndclass;
ZeroMemory(&wndclass, sizeof(WNDCLASSEX));
wndclass.cbSize         = sizeof(WNDCLASSEX);
wndclass.style          = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc    = WndProc;
wndclass.cbClsExtra     = 0;
wndclass.cbWndExtra     = 0;
wndclass.hInstance      = hInstance;
wndclass.hIcon          = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground  = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName   = NULL;
wndclass.lpszClassName  = L"MyTestClassForText";

// Check for register problem
if (!RegisterClassEx(&wndclass))
{
    MessageBox( NULL , L"Window Couldn't be Created" , L"Error" , MB_OK );
    return false;
}

// Create a child window to the main window and put an edit contol inside. What happends?
HWND parWnd     = CreateWindowEx( NULL              , L"MyTestClassForText" , L"Parent Window"      , WS_OVERLAPPEDWINDOW | WS_VISIBLE                  ,0 ,0  ,340 ,240 , NULL     , NULL , hInstance , NULL );
HWND childWnd   = CreateWindowEx( NULL              , L"MyTestClassForText" , L"Child Window"       , WS_CHILDWINDOW | WS_VISIBLE | WS_OVERLAPPEDWINDOW ,0 ,0  ,300 ,200 , parWnd   , NULL , hInstance , NULL );
HWND editWnd    = CreateWindowEx( WS_EX_CLIENTEDGE  , L"EDIT"               , L"Try To Edit This."  , WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL            ,4 ,4  ,270 ,40  , childWnd , NULL , hInstance , NULL );
HWND buttonWnd  = CreateWindowEx( NULL              , L"BUTTON"             , L"This Works"         , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON             ,4 ,52 ,100 ,30  , childWnd , NULL , hInstance , NULL );

You can assume the windows default process is used sufficiently in the mentioned WndProc and that hInstance is consistent throughout the program. Changing the style flag in the child window from WS_OVERLAPPEDWINDOW to WS_CAPTION results in the same problem.

I understand having a window directly within another window isn't normal, but it should be possible.

Your help and consideration are much appreciated,

Caleb

Caleb
  • 11
  • 4
  • I reproduced the behavior, the problem description appears correct. I also know a not-too-complex fix the presumably will work, namely using MDI window (it's sort of what it's *for*). But I don't know why that edit control behaves as if it's disabled. I suspected some perhaps numerically-identical style bit, doesn't seem to be. I observed some pecular window caption values in 32-bit Spy++, could be related. – Cheers and hth. - Alf Jul 13 '15 at 07:47
  • Since I can't give really good advice about the stated problem (other than suggesting MDI), here's some advice about the code in general. (1) Don't use `ZeroMemory`, just write `= {}`. (2) For the error message box, at least add `MB_SETFOREGROUND`. (3) Returning `false` is incorrect, that indicates success. Return e.g. `E_FAIL`, or C/C++ `EXIT_FAILURE` (`E_FAIL` has some advantages). (4) Don't create the main window visible. Create it hidden, add all child windows etc. Then do `ShowWindow`. – Cheers and hth. - Alf Jul 13 '15 at 07:52
  • If a window hosts controls, it should specify the `WS_EX_CONTROLPARENT` [Extended Window Style](https://msdn.microsoft.com/en-us/library/windows/desktop/ff700543.aspx). – IInspectable Jul 13 '15 at 09:13
  • 1
    It is technically possible since Winforms knows [how to pull it off](http://stackoverflow.com/a/7692113/17034). Use Spy++ to look at style flags and messages to reproduce that in your program. – Hans Passant Jul 13 '15 at 09:57
  • Thanks for the help. MDI really does seem like a better way to go about doing this. I found a great example [here](http://winapi.foosyerdoos.org.uk/code/mdi/htm/createmdi.php) Alf, thanks for the windows coding advice. I'll take the everything under consideration for the work to come. – Caleb Jul 14 '15 at 07:14

0 Answers0