Home > Back-end >  C how to display text in child window at real time
C how to display text in child window at real time

Time:12-02

enter image description here

This application creates a child window (which is the white box) when I right click anywhere, and destroys the child window after another right click. I have implemented the mechanics to expand and shrink the red rectangle through Direct 2D. I would like the child window to display the width and height of the rectangle at real time, as I make changes to it. I do not interact with the child window at all: it doesn't need an "x" button for closing and stuff; it just prints out a couple lines of data.

Here is what I have in my main window procedure:

case WM_RBUTTONUP:
            {
                DemoApp *pDemoApp = reinterpret_cast<DemoApp *>(static_cast<LONG_PTR>(
                    ::GetWindowLongPtrW(hwnd, GWLP_USERDATA)));
                pDemoApp->showTextBox = !pDemoApp->showTextBox;  //showTextBox is a boolean
                if (pDemoApp->showTextBox) {
                    POINTS cursor = MAKEPOINTS(lParam);
                    pDemoApp->child_hwnd = CreateWindowEx(
                        WS_EX_TOPMOST,
                        "LISTBOX",
                        "I dont need a title here",
                        WS_CHILDWINDOW,
                        cursor.x,
                        cursor.y,
                        100,
                        200,
                        pDemoApp->main_hwnd,
                        NULL,
                        HINST_THISCOMPONENT,
                        pDemoApp
                    );
                    ShowWindow(pDemoApp->child_hwnd, SW_SHOWNORMAL);
                    UpdateWindow(pDemoApp->child_hwnd);
                }
                else {
                    DestroyWindow(pDemoApp->child_hwnd);
                }
            }
            break;

How may I go from here? I would like to know:

  1. Is using Direct Write to draw text in the child window my only option? I see the dashed lines in the white box so I assume there must be a way to display plain text.

  2. I used LISTBOX here, which is a predefined windows class name. How do I set a procedure for it? What else predefined class name can better suit my need? Or do I have to register a custom one;

  3. I would like to drag the child window around, how can I set it up so that the system handles dragging for me.

  4. Would popping a dialog box to display text be better than popping a child window?

Thanks.

CodePudding user response:

Since you are using a Win32 LISTBOX control as the child window, then you have a couple of options for displaying the rectangle's dimensions in it:

  • give the ListBox the LBS_HASSTRINGS style, and add 1-2 items to it. Then, any time you change the rectangle, send LB_DELETESTRING and LB_ADDSTRING messages to the ListBox's HWND to display the updated dimensions as needed. A ListBox does not have a way to update an existing item, so to change an existing item's text, you have to remove the item and then re-add it with the new text.

  • give the ListBox an LBS_OWNERDRAW... style, and add 1-2 blank item(s) in it. Then have the ListBox's parent window handle WM_MEASUREITEM and WM_DRAWITEM notifications from the ListBox to display the rectangle's current dimensions in the item(s) as needed. Whenever the rectangle is changed, call InvalidateRect() on the ListBox's HWND to trigger a redraw.

  • Related