Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

refresh static text in dialog win32 api 1

Status
Not open for further replies.

miqui0202

Programmer
Feb 16, 2012
3
FR
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.


 
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.
 
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.
 
Two more things.

1.
[tt]INT_PTR CALLBACK Function(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message==WM_INITDIALOG) hDialog=hDlg;
/*the rest of your function*/
}
}[/tt]
You need to initialize hDialog only once, when the dialog is created.

2.[tt]
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG), hWnd, Function);
hDialog=NULL;[/tt]
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.
 
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.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top