Good evening Cboard!
I have been programming as a hobby, on and off, for the past seven years or so and have recently decided to take the plunge into Windows programming. Currently, I am creating a frame-based GUI from scratch, including a couple of groupboxes generated with CreateWindow().
Along with the controls within the box, I wish to display the text stored in an std::string variable called sBracedUser. My current method, based on the TextOut() API, is failing; if I set the first parameter to the function (HWND) as the handle to the groupbox's parent window, the program does indeed print the text, but cannot print it on top of the groupbox. If I set the handle parameter of TextOut() as the handle to the groupbox, the function fails. The relevant code sections are as follows:
Global typedef of String (for ANSI and Unicode builds respectively):
Code:
#ifndef UNICODE
typedef std::string String;
#else
typedef std::wstring String;
#endif
This is my OutputTextToWindow() function:
Code:
int OutputTextToWindow(HWND hwnd, String text, int xpos, int ypos)
{
HDC hDeviceContext = GetDC(hwnd); // Get handle to device context
HGDIOBJ NewFont = GetStockObject(DEFAULT_GUI_FONT); // Create NewFont object.
HGDIOBJ OldFont = SelectObject(hDeviceContext, NewFont); // Load NewFont into hDevice Context
// N.B. Displaced font in HDC must be reloaded again
SetBkMode(hDeviceContext, TRANSPARENT); // Set text background as transparent
TextOut(hDeviceContext, xpos, ypos, text.c_str(), text.length()); // Print text
SelectObject(hDeviceContext, OldFont); // Clean up - OldFont is reloaded into DC
DeleteObject(OldFont); // OldFont object is deleted
DeleteObject(NewFont); // NewFont object is deleted
ReleaseDC(hwnd, hDeviceContext); // Device context is released
return 0;
}
This code is in a control-creation function:
Code:
HWND hUsersBox = CreateWindow(_T("BUTTON"), _T("Select Users"), BS_GROUPBOX | WS_CHILD, 9, 10, 232, 235, hwnd, NULL, hInstance, NULL);
// More code in between these lines.
String sBracedUser = BraceEnclosure(lpUser, Size); // Initialise String and set it equal to (<username>).
// More arbitrary code.
ShowWindow(hUsersBox, SW_SHOW);
OutputTextToWindow(hwnd, sBracedUser.c_str(), 50, 150); // Output user name in brackets.
// OutputTextToWindow(hUsersBox, sBracedUser.c_str(), 50, 150) fails due to failure of the TextOut() function.
Obviously the code needs alot more work to manage error-checking etc. I was just wondering if there is any way to modify the text output function to allow printing text to a groupbox object. Or would a completely different approach entirely be necessary, perhaps because, by design, the Win32 API forbids the TextOut() function to interact with non-window objects?
My other question about the Win32 API is a quick one. All of the sample code I have found for basic window creation initialise the classname variable for WNDCLASSEX as a global variable. Is this strictly necessary? As far as I'm aware, the only use for this would be for calls to CreateWindow/CreateWindowEx in functions other than the original window-creation function.
Any help would be greatly appreciated!
Many thanks,
Abyssion
P.S. This is slightly off topic, I apologise: does this site not allow for SSL connections to the forum servers? I am very wary, in general, of sending user credentials in plaintext over the internet. Cheers.