Jump to content
  • Advertisement
Sign in to follow this  
nekobasu33

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!