# Win32 dialog box's static text from a file or string

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to create a windows program using C++ that will open up a dialog box where the text in it will be loaded from a file or a string. Is there a way to do that instead from the the visual Studio editor? If there is, can anybody please guide me on how to accomplish it? Any help would be appreciated. Thank you in advance [Edited by - nekobasu33 on June 26, 2007 1:13:20 PM]

##### Share on other sites
Do you want to create the dialog from resources or on the fly or do you want to change the text in the dialog, for example, to localize the display language?

##### Share on other sites
I don't need to create the dialog box on the fly nor change the text in the dialog box. All I need to do is to create a dialog box from the resource and put some static text from a file or string.

##### Share on other sites
Ok. Here's one approach. Place a static window control in the dialog box. Get the hwnd to that window, get the hdc for that hwnd and then use TextOut to write the text to that window.

##### Share on other sites

When you mean by "place a static window control", does it mean using the "Static Text" and then click and drag the mouse to make a window?

To get the hdc, I can just simply do this:

dc = GetDC(hWnd);

But how do you get the hwnd to the static window? Can you please give me a code snippet for this?

Will this method also work for a button? (if I want to change the caption of a button)

Thank you for the help. :)

##### Share on other sites
Use ::SetWindowText to set the text of a static control (or a button).

You don't have to worry about the HDC.

Given the dialog's hwnd and the control id of the static control (or whatever), use ::GetDlgItem to get the hwnd of the static control (or whatever).

##### Share on other sites
@phil_t: SetWindowText() just changes the title bar of the specified window. Nothing close to what the OP is wanting.

##### Share on other sites
It most certainly does exactly what he wants.

To quote the link you just provided:
Quote:
 If the specified window is a control, the text of the control is changed.

##### Share on other sites
Quote:
 Original post by nekobasu33When you mean by "place a static window control", does it mean using the "Static Text" and then click and drag the mouse to make a window?

static control window
static control window resource

Here's a very old school overview of Static Controls

Add the following to a dialog definition inside a resource script (".rc") file:

CONTROL "Sample &Text", 104, "Static", SS_LEFT | SS_NOPREFIX, 33, 91, 94, 13

According to that article, you can use SetDlgItemText to change the text. The key is the nIDDlgItem parameter. In the above rc file snippet that would be 104. You can use a #define in a "resource.h" header to declare an ID macro (eg #define MYSTATIC_ID 104), then include "resource.h" in the .rc file and the source code file where you want to call SetDlgItemText.

CONTROL "Sample &Text", MYSTATIC_ID, "Static", SS_LEFT | SS_NOPREFIX, 33, 91, 94, 13

SetDlgItemText(hDialog, MYSTATIC_ID, "Hello WOrlds!");

Quote:
 Original post by nekobasu33To get the hdc, I can just simply do this: dc = GetDC(hWnd);But how do you get the hwnd to the static window? Can you please give me a code snippet for this?

GetDlgItem Function

HWND hStaticControl = GetDlgItem(hDialog, MYSTATIC_ID);

Quote:
 Original post by nekobasu33Will this method also work for a button? (if I want to change the caption of a button)

Probably. Try it out and see if it works. [smile]

##### Share on other sites
Everybody, thank you for all the help... :)

I got it to work.
This is what I did:

1. Get the HWND:
HWND myhwnd = ::GetDlgItem(hDlg, IDC_MYSTATIC3);

2. Then, I Set up the text:
::SetWindowText(myhwnd, _T("Hello"));

I used "_T" because if I cast the string to LPCWSTR, it gives me some weird chinese characters. Anyway, does anybody know why?

LessBread, thank you for the step by step instruction. The SetDlgItemText doesn't work for me. I am under the impression that I didn't set it up correctly.

After getting the text on the dialog box, I still have 1 concern, though, the static text that I successfully put in the dialog box flickers when I move the mouse pointer on the dialog box. Does this have anything to do with double buffering? Does anybody know the solution to this?

##### Share on other sites
By the way, I tried the same method on a button. It works, I am able to see the new static text on the button, but seems like it broke the whole dialog box; It froze, all objects except the button on the dialog box disappear and the static text flickers. It's rather an unexpected result.

##### Share on other sites
Quote:
 Original post by nekobasu33Everybody, thank you for all the help... :)I got it to work.This is what I did: 1. Get the HWND: HWND myhwnd = ::GetDlgItem(hDlg, IDC_MYSTATIC3);2. Then, I Set up the text: ::SetWindowText(myhwnd, _T("Hello"));LessBread, thank you for the step by step instruction. The SetDlgItemText doesn't work for me. I am under the impression that I didn't set it up correctly.

I don't know why SetDlgItemText didn't work. It most likely executes code similar to yours.

Quote:
 Original post by nekobasu33I used "_T" because if I cast the string to LPCWSTR, it gives me some weird chinese characters. Anyway, does anybody know why?

Yes. Search these forums for the word UNICODE. The short of it is that prepending L to a string literal tells the compiler to embed the string using UNICODE values rather than ANSI values. The _T macro automates that for you by prepending L when UNICODE is defined and not prepending it when it's not. You can't cast a string from ANSI to UNICODE, you have to use a conversion function.

Quote:
 Original post by nekobasu33After getting the text on the dialog box, I still have 1 concern, though, the static text that I successfully put in the dialog box flickers when I move the mouse pointer on the dialog box. Does this have anything to do with double buffering? Does anybody know the solution to this?

This might help: Flicker-Free Drawing in Windows

##### Share on other sites
Quote:
 Original post by nekobasu33By the way, I tried the same method on a button. It works, I am able to see the new static text on the button, but seems like it broke the whole dialog box; It froze, all objects except the button on the dialog box disappear and the static text flickers. It's rather an unexpected result.

You don't need to put a static text on a button. Text buttons display their own text automatically.

The flicker is probably due to the fact that both the button and your static text are updating themselves and invalidating each other.

##### Share on other sites
For button, I need to be able to customize the caption without using the VS resource editor. This will be useful if I need to pull up some data from a database or what not to be put on the button's caption.

By the way, LessBread, I got the SetDlgItemText to work, I just didn't use the right HWND before.

I have tried so many things to fix this flickering problem.
However, I just cannot solve it.
Here's my code snippet, I use "About" dialog box as an example. And the code to add the static text in the dialog box is in the first two lines

INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){	HWND myhwnd = ::GetDlgItem(hDlg, IDC_MYSTATIC3);	::SetWindowText(myhwnd, _T("Test Static"));        //SetDlgItemText(hDlg, IDC_MYSTATIC3, _T("Test"));	UNREFERENCED_PARAMETER(lParam);	switch (message)	{	case WM_INITDIALOG:		return (INT_PTR)TRUE;	case WM_COMMAND:		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)		{			EndDialog(hDlg, LOWORD(wParam));			return (INT_PTR)TRUE;		}		break;	}	return (INT_PTR)FALSE;}

##### Share on other sites
Got it to work....

I guess the problem was just my amateurism in WIN32 programming. It's NOT about double buffering issue.

Here's what I did, which is WRONG, when the flickering happen:
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){	SetDlgItemText(hDlg, IDC_MYSTATIC3, _T("Static Text")); //This is the problem	UNREFERENCED_PARAMETER(lParam);	switch (message)	{	case WM_INITDIALOG:		return (INT_PTR)TRUE;// etc...

What SHOULD've been done:
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){	UNREFERENCED_PARAMETER(lParam);	switch (message)	{	case WM_INITDIALOG:		SetDlgItemText(hDlg, IDC_MYSTATIC3, _T("Static Text")); //Fixing the flickering problem		return (INT_PTR)TRUE;//etc...

Thank you for all the help, though. :)

[Edited by - nekobasu33 on June 27, 2007 6:40:42 PM]

##### Share on other sites
Quote:
 Original post by nekobasu33For button, I need to be able to customize the caption without using the VS resource editor.

You still don't need to put the static text on top of the button. Just use the button's ID and "SetWindowText"

##### Share on other sites
Verg,

If we use "SetWindowText", we need more information than just the button's ID, which is the button's HWND using "GetDlgItem()".
And to get that information, we need to write another line of code.

2 lines
HWND myhwnd = ::GetDlgItem(hDlg, IDC_MYSTATIC3);::SetWindowText(myhwnd, _T("Test Static"));

VS

1 line
SetDlgItemText(hDlg, IDC_MYSTATIC3, _T("Test Static"));

Please let me know if I misunderstood you.

##### Share on other sites
Doing it that way is fine.. but you don't need another static text. Just call "SetDlgItemText" directly on the button.

##### Share on other sites
Yes, I see what you mean.
Thank you :)