My application requires a dialog box and a parent window, from the window proc of the parent window I'm trying to draw on the dialog box. How might I do this?

Thank you for reading :)

Dani AI

Generated

As hinted, the practical way to get text onto a dialog created by the UI thread is to have the dialog perform its own painting and to send it data from background work. The pattern below is simple, safe, and works reliably: a worker thread prepares the text (or other data) then posts a custom message to the dialog; the dialog copies the data into its own buffer or updates a control and then calls InvalidateRect/UpdateWindow so the UI thread does the actual drawing.

Example pattern (minimal):

#define WM_APP_UPDATE_TEXT (WM_APP + 1)

/* worker thread */
void WorkerProc(HWND hDlg) {
    const char *s = "Hello from worker";
    char *buf = _strdup(s);                       /* allocate for transfer */
    PostMessage(hDlg, WM_APP_UPDATE_TEXT, 0, (LPARAM)(uintptr_t)buf);
}
/* in dialog proc */
case WM_APP_UPDATE_TEXT:
{
    char *buf = (char*)(uintptr_t)lParam;
    if (buf) {
        SetDlgItemText(hDlg, IDC_STATIC1, buf);  /* runs on dialog (UI) thread */
        free(buf);                               /* free here, not in worker */
        InvalidateRect(GetDlgItem(hDlg, IDC_STATIC1), NULL, TRUE);
        UpdateWindow(GetDlgItem(hDlg, IDC_STATIC1));
    }
}
break;

Troubleshooting notes: post the message only after the dialog exists (check hDlg), avoid passing stack pointers — always allocate or copy, and free on the UI side. Use PostMessage for asynchronous updates; use SendMessage only if you need synchronous behavior (it will block the caller). Never call GDI drawing functions (TextOut, BeginPaint, GetDC used for drawing) from a non-UI thread — do the drawing inside WM_PAINT or by updating controls on the dialog. This approach keeps drawing in the dialog’s thread while still letting worker threads do background work.

Recommended Answers

All 11 Replies

parent window shouldn't be trying to draw on the dialog box -- let the dialog box do all the drawing.

So is it not possible?:)

Thanks for the reply!

To help I will explain further, from the wm_paint handler in my parent window proc call textout in order to display text on the dialog box in my program. :D

I'm not saying it's impossible, but normally the parent will send a message to dialog so that dialog can update the text in the dialog's window. You can set up private messages to do that.

Ok sorry if I'm using up too much of your time, but how do I use drawing command in a thread and then use _beginthread function during the wm_paint handler of a dialog box I have tried but I can't seem to get it to display anything? :)

Thanks for the reply!

You can't -- drawing must be done in the same thread as the dialog box. If you don't want the dialog box in the same thread as the parent then either put the entire dialog box in another thread or create a modeless dialog box.

Why Windows Threads Are Better Than POSIX Threads (e.g. CreateThread() better than _beginthread() )

What I'm trying to do is have my dialog box appear when a menu item is pressed (I have done that) but then a thread containing textout function to display text on the dialog box begins but the text isn't displayed, could this be a defect of _beginthread?

Thanks for the reply :)

could this be a defect of _beginthread?

No -- that's how Microsoft designed the api.

So I'm not able to add gdi functions to threads that begin in a dialog box?

Thanks for the reply :)

That's right -- drawing must be done in the same thread as the window or dialog box.

Ok thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.