Home > Blockchain >  Deselect edit control win32 c
Deselect edit control win32 c

Time:07-09

How would I go about deselecting the text in edit control?

After entering the input I want the user to be able to deselect the edit control. Because even after you click out of it and press a key, it gets entered into the edit.

Here is the code for my edit control:

HFONT fontMain = CreateFont(
            -16,                                                // Height Of Font
            0,                                                  // Width Of Font
            0,                                                  // Angle Of Escapement
            0,                                                  // Orientation Angle
            0,      // Font Weight
            false,                              // Italic
            false,                          // Underline
            false,                          // Strikeout
            ANSI_CHARSET,                               // Character Set Identifier
            OUT_TT_PRECIS,                                      // Output Precision
            CLIP_DEFAULT_PRECIS,                                // Clipping Precision
            ANTIALIASED_QUALITY,                                // Output Quality
            FF_DONTCARE|DEFAULT_PITCH,                          // Family And Pitch
            TEXT("Calibri"));

HWND editControl = CreateWindow(
                TEXT("EDIT"),
                TEXT("TEST TEXT"),
                WS_CHILD | WS_VISIBLE | ES_LEFT | ES_MULTILINE,
                x, y, width, height,
                window,
                (HMENU) 100,
                instance,
                NULL);
SendMessage(window /* parent window */, WM_SETFONT, (WPARAM)fontMain, NULL);
DeleteObject(fontMain);

I have checked MSDN docs and have not found any additional styles to add to achieve the task.

If you have any ideas on how to achieve this task could you help me out?
Thank you.

CodePudding user response:

You could use the same trick that works to dismiss dropdown list (of combo box), popup menus, and the like.

  1. You'll need to subclass the EDIT control so you receive messages first to your own window procedure.

  2. In your textbox subclass WM_SETFOCUS handler, call SetCapture so the next click is delivered to the textbox even if it's outside.

  3. In the textbox subclasses's handler for mouse click messages, test the location and if outside the textbox, call SetFocus(NULL) to give up the focus. (This is where a popup would dismiss itself). Also call ReleaseCapture().

  4. Also call ReleaseCapture() in the subclass's WM_KILLFOCUS handler, since mouse clicks are not the only way to lose focus.


The above is the way to have any click outside the textbox remove its focus. If you only want clicks in the client area of your parent window to defocus the textbox, then you can certainly skip the subclassing and capture, and just handle the mouse click events in the parent window, calling SetFocus(NULL) or SetFocus(parentHWnd).

CodePudding user response:

The answer that worked for me was:

SetFocus(/* parent window */);

The answers were provided by:

@PaulSanders and @BenVoigt

  • Related