INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

refresh static text in dialog win32 api

refresh static text in dialog win32 api

(OP)
hi there!

I'm struggling with an issue and I hope someone could help me!

Actually, I'm trying to edit every time a static text in a dialog box edited in my ressource file.

Because I'm using the rc file, my static text isn't linked to an HWND variable. It only has a reference (IDC_STATIC_TEXT).

In the same way, my dialog has a reference (IDD_DIALOG) and is called in my program as following:

DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG), hWnd, Function);

where hWnd is my main HWND and Function is the function handling events from my dialog box.
This's its declaration:

INT_PTR CALLBACK Function(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);

I would like to find a mean to send a message either to my static text or to my dialog from the while loop of my main function of my API:

For example:

int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR    lpCmdLine,int       nCmdShow){
   /* declaration */
   while (GetMessage(&msg, NULL, 0, 0)){
         /*send message to my static text or dialog*/
   }
}

Thanks in advance for your replies !

Sincerely

M.


 

RE: refresh static text in dialog win32 api

First of all, you need to record the handle of your dialog, when it is created. You can do it easily, by creating a public hDialog variable and storing the value of hDlg in dialog box callback function when WM_INITDIALOG is received.

This will stablish a channel for communication between your "Main" function and your dialog box. Also, you can assign NULL to hDialog just after DialogBox() function. This will ensure that the hDialog does not contain an invalid dialog reference after it is closed.

Once you get a valid reference to hDialog in your "Main" function, there are many ways to access the static text you are interested in. You can call GetDlgItem function to retrieve the handle of static text by supplying the dialog box handle and static text control Id (IDC_STATIC_TEXT). After you get handle of the static text, you may probably call SetWindowText or send WM_SETTEXT message to change the text in static control.

There is even a simpler way. You can use SetDlgItemText function to directly set text in the static control without knowing its window handle. Similarly, GetDlgItemText function retrieves the current text from a control. Both of these functions need just the dialog box handle and control Id.

Hope that makes sense.

RE: refresh static text in dialog win32 api

(OP)
Thank Hypedia for your reply !

Let me sumarize what you said.
You want me to create a HWND global variable:

HWND hDialog=NULL;

then in my main function to do that:

int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR    lpCmdLine,int       nCmdShow){
   /* declaration */
   while (GetMessage(&msg, NULL, 0, 0)){
         if(my IDD_Dialog is created)
             SetDlgItemText(hDialog,IDC_STATIC_TEXT,L"salut");
//here i display "salut" but we could imagine displaying a changing content
   }
}

finaly in my function Function to do that:

INT_PTR CALLBACK Function(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
        hDialog=hDlg;
        /*the rest of my function*/

   }
}

/////////////////////////////////////////////////////

Tell me if I well understood !

M.

RE: refresh static text in dialog win32 api

Two more things.

1.
INT_PTR CALLBACK Function(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    if (message==WM_INITDIALOG) hDialog=hDlg;
        /*the rest of your function*/
   }
}

You need to initialize hDialog only once, when the dialog is created.

2.
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG), hWnd, Function);
hDialog=NULL;

hDialog will be set to NULL as soon as the dialog is closed and DialogBox function returns. This will make sure hDialog does not contain an invalid reference, after the dialog is closed.
This will also help you determine, if your dialog is active or not, as you do in your main function... "if(my IDD_Dialog is created)". If hDialog is NULL, then the dialog is not created, otherwise it is.

Rest is exactly as I told.

RE: refresh static text in dialog win32 api

(OP)
Thank you Hypetia !

I had already tried to put a global HWND hDialog in my program and put my hDlg in it and it never run !
But when I did it, I wasn't sure it could!

As you said to me that it was the solution I insisted in this way and I succeded to do what I wanted to do!

I realized where was my mistake, actually I'm working with a main window and dialog boxes which appear when I click on my menu buttons on my main window.
When none of my dialog boxes are opened my program is blocked in the while loop of the APIENTRY _tWinMain function (it's the default name when I create a new API Win32 project in VS 2010).
But when I click on a menu button in order to get one of my dialog box appeared, my program leaves this while loop until the current dialog box is closed.
Hence, all my tries to change static text content fail because they are never done....

I solved this issue using thread.
I don't know if you have an other solution.

Thank again!
M.  

RE: refresh static text in dialog win32 api

By changing static text, if you want to initialize or preset the static text (or other controls) on your dialog, you should do it in WM_INITDIALOG message, which is intended for this purpose.

On the other hand, if you want to change the static text based on some response or interaction with user, then the code to change static text should reside in the dialog box callback function, which will be receiving messages for user interaction with the dialog.

In both cases, the code shall be residing in the dialog box callback function.

Its OK too, if you have solved your issue with threads.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close