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

Started by
17 comments, last by nekobasu33 16 years, 9 months ago
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]
Advertisement
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?
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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.
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.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
I appreciate your fast reply.

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. :)
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).
@phil_t: SetWindowText() just changes the title bar of the specified window. Nothing close to what the OP is wanting.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

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.
Quote:Original post by nekobasu33
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?


Here are two google searches

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 nekobasu33
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?


GetDlgItem Function

HWND hStaticControl = GetDlgItem(hDialog, MYSTATIC_ID);


Quote:Original post by nekobasu33
Will 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]
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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?

This topic is closed to new replies.

Advertisement