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

Started by
17 comments, last by nekobasu33 16 years, 9 months ago
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.
Advertisement
Quote:Original post by nekobasu33
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"));

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 nekobasu33
I 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 nekobasu33
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 might help: Flicker-Free Drawing in Windows
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Quote:Original post by nekobasu33
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.


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.
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
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;}
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]
Quote:Original post by nekobasu33
For 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"


my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
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.
Doing it that way is fine.. but you don't need another static text. Just call "SetDlgItemText" directly on the button.
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Yes, I see what you mean.
Thank you :)

This topic is closed to new replies.

Advertisement