Win32 Edit printing incorrect characters - c++

I am using a Win32 edit to display debugging information, and I have placed the edit, along with the rest of my basic GUI in a class. But when I output anything to the edit it displays '??????????????????????????'. I think the error lies in my MyGUI::append(LPCSTR) method, although it has always worked perfectly in the past. Any comments/ideas/solutions will be appreciated. If I need to post all the code pertaining to my GUI class please let me know so.
My class lies in the namespace Interface, along with the stand-alone WindowProcedure function, which I call when registering the application with the WNDCLASSEX object.
The win32 edit is not created in the WM_CREATE handle within the WindowProcedure(as it probably should be) as I could not place the function inside my GUI class.
Method that creates the edit:
HWND createEdit( HINSTANCE hInst, HWND hwnd, int appBott, int appTop ){
return CreateWindowEx( WS_EX_APPWINDOW,
TEXT("EDIT"), TEXT(""),
WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY | ES_MULTILINE| WS_VSCROLL | WS_HSCROLL,
10, 10, appBott-25, appTop-50,
hwnd,
(HMENU) 102,
hInst,
NULL );
}
Used in 'guiCreate()' method as:
HWND hEdit = createEdit( hInst, hWin, appWidth, appHeight );
Method that displays text in edit:
void Interface::MyGUI::append( LPCSTR text ){
if( created && !stopAll ){
int TextLen = SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
SendMessageW(hEdit, EM_SETSEL, (WPARAM)TextLen, (LPARAM)TextLen);
SendMessageW(hEdit, EM_REPLACESEL, FALSE, (LPARAM) text);
}
}
Used in main program as:
MyGUI form(); //initialize form
form.append( (LPCSTR)"Example text\n" );
Input text: 'Example text.\n'
Displayed text: '?????????????? l'

You are targeting ANSI it would seem. In that case, don't call SendMessageW, call SendMessageA or even SendMessage and let that be expanded to SendMessageA.
You call SendMessageW but pass ANSI encoded text. When you called SendMessageW you promised to send UTF-16 encoded text.
However, you should stop targeting ANSI I think. Target Unicode instead. Stop using the TEXT() macro and use the L prefix for your string literals. And stop casting string types. That (LPCSTR) cast is asking for trouble. When you cast like that you tell the compiler that you know better than it does. And usually that is not the case.

Related

Is it possible to change font for an edit control without affecting the other lines?

Hello I want to know if it is possible to change the font of an edit control for some lines only without affecting the remaining:
In my Edit control I have a text but I want some headlines and titles in bigger font and bold while the other lines are with smaller font.
I tried SendMessage(hEdit, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(0, true));
But it sets the whole text in the passed in font.
I thought some messing up with SelectObject(hDcEdit, hFont); But I don't know if it is correct and how.
A standard Edit Control (think, Notepad) does not support what you are looking for. It only supports one Font for the entire text.
What you are looking for is a RichEdit Control instead (think, Wordpad), and in particular its EM_SETCHARFORMAT message, which can be used to apply different formatting (including fonts, colors, etc) to different sections of text.
This is not working with the default Editcontrol, but you can use a Richeditcontrol
#include <Windows.h>
#include <CommCtrl.h>
HINSTANCE relib = LoadLibrary("riched32.dll");
if (relib == NULL) {
MessageBox(NULL, "couldn't load richedit32.dll", "", MB_ICONEXCLAMATION);
hEdit = CreateWindow(RICHEDIT_CLASS, "", WS_VISIBLE | WS_CHILD | ES_MULTILINE |
ES_AUTOHSCROLL | ES_AUTOVSCROLL | WS_VSCROLL | WS_HSCROLL, 0, 0, 200, 200, hWnd, NULL,
NULL, NULL);
Now to set the font to your Richeditcontrol use:
CHARFORMAT2 cf;
memset(&cf, 0, sizeof cf);
cf.cbSize = sizeof cf;
cf.dwMask = CFM_FACE;
wsprintf(cf.szFaceName, "Arial"); //Here you can set the fontname you wont (C:/Windows/Fonts)
SendMessage(hEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);

Modify main window's locale after user changes settings in Control Panel->Regional and Language Options

INTRODUCTION AND RELEVANT INFORMATION:
I have MS Access 2007 database which I fill using ADO.
Among other data types ( string, integer...) I also have a double.
Since I work on Windows XP and use pure Win32 API to create GUI, I collect data from edit controls with GetDlgItemText API and then I convert that text into double using _wtof_l.
PROBLEM:
Everything works well if the user sets English or Serbian ( we use European notation for decimal and group separator ) locale and then starts the program, but the problem occurs when user changes locale settings while the program is working.
Let me demonstrate this on a small example:
Let us assume that user has English locale set.
Then user starts my application.
Then user decides to change the locale ( Control Panel->Regional and Language Settings for Windows XP ) before he hits the "Save" button.
After the changes apply he then enters data and then hits "save".
The error in converting text to double must occur ( _wtof_l will now truncate 1.25 to 1 ), since my program uses default ANSI "C" locale and did not adapt it to reflect users modification.
MY EFFORTS TO SOLVE THE PROBLEM:
To prevent this I need to adapt my program to the possibility described above-I need to set my locale to the one user selected before executing query.
To do so I use message from answer to my previous question to detect when user changes settings in Control Panel->Regional and Language Options.
Then I use _wsetlocale(LC_ALL,"") to set my applications locale to the one selected by the user.
However, wrong conversion from text to decimal number described above still occurs.
This only happens when I change the locale during my program's work. If I leave locale untouched ( as 99.9% of users will ) everything seems to work fine.
To further help the community I have made a demo application that illustrates the problem.
It will be presented in the APPENDIX section at the end of this post.
QUESTION:
How can I respond to WM_SETTINGCHANGE message to set my application's locale to the one currently selected by the user so my "save" button handler can perform proper conversion from string to double with _wtof_l function?
Thank you.
Best regards.
APPENDIX:
Steps to create the demo application that illustrates my problem:
1.Create default Win32 project in Visual Studio.
2.Add the following WM_CREATE handler:
case WM_CREATE:
{
_wsetlocale( LC_ALL, L"" ); //set current locale at window creation
INITCOMMONCONTROLSEX iccex;
memset( &iccex, 0, sizeof(INITCOMMONCONTROLSEX) );
iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccex.dwICC = ICC_STANDARD_CLASSES | ICC_TAB_CLASSES | ICC_BAR_CLASSES;
InitCommonControlsEx( &iccex );
// text
HWND hEdit = CreateWindowEx( 0, L"Edit", L"",
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
50, 50, 150, 25, hWnd, (HMENU)8002, hInst, 0 );
// decimal number
HWND hEdit1 = CreateWindowEx( 0, L"Edit", L"",
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,
250, 50, 150, 25, hWnd, (HMENU)8003, hInst, 0 );
HWND hButton = CreateWindowEx( 0, L"Button", L"Save",
WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON,
50, 150, 150, 25, hWnd, (HMENU)8004, hInst, 0 );
SendMessage( hEdit, EM_LIMITTEXT, (WPARAM)9, (LPARAM)0 );
SendMessage( hEdit1, EM_LIMITTEXT, (WPARAM)4, (LPARAM)0 );
}
return 0L;
3.Add the following handler to detect when user changes locale settings
case WM_SETTINGCHANGE:
if( !wParam && !wcscmp( (wchar_t*)lParam, L"intl" ) )
{
_wsetlocale( LC_ALL, L"" ); //set it to current locale
return 0L; // "say" we handled it
}
else
break; // pass it to default window procedure
4.In WM_COMMAND handler add the following cases:
case 8004:
{
// initialize COM
HRESULT hr = CoInitialize(NULL);
// format connection string
wchar_t *bstrConnect= L"Provider=Microsoft.ACE.OLEDB.12.0; \
Data Source = .\\test.accdb";
try
{
ADODB::_ConnectionPtr pConn("ADODB.Connection");
hr = pConn->Open(bstrConnect, L"admin", L"", ADODB::adConnectUnspecified);
if ( SUCCEEDED(hr) )
{
wchar_t text[10], number[5];
memset( &text, L'\0', sizeof(text) );
memset( &number, L'\0', sizeof(number) );
GetDlgItemText( hWnd, 8002, text, 10 ); // text
GetDlgItemText( hWnd, 8003, number, 5 ); // double
ADODB::_CommandPtr pCmd("ADODB.Command");
pCmd->ActiveConnection = pConn;
pCmd->CommandText = L" insert into MyTable ( field1, field2 ) values ( ?, ? );";
pCmd->Parameters->Append( pCmd->CreateParameter( "?", ADODB::adDouble,
ADODB::adParamInput, sizeof(double),
_wtof_l( number, _get_current_locale() ) ) );
pCmd->Parameters->Append( pCmd->CreateParameter( "?",
ADODB::adVarWChar, ADODB::adParamInput,
wcslen(text), text ) );
pCmd->Execute( NULL, NULL, ADODB::adCmdText );
pConn->Close(); // close connection
CoUninitialize(); // uninitialize COM
}
else
throw _com_error(hr); //something failed-report it
}
catch(_com_error& e)
{
MessageBox(hWnd, (LPWSTR)(e.Description()), L"Error", MB_OK |
MB_ICONERROR );
CoUninitialize();
}
}
break;
5.Create MS Access 2007 database in the project folder.
6.Create table MyTable and add 2 fields field1 as TEXT field, and add field2 which is double.
The problem was in improper setting of the current locale.
I was using the wrong function to do that.
I do not wish to steal other people's credits so here is the link where I got the solution from.
This way, any time user changes local settings in Control Panel, my application will be able to properly convert string to double and my INSERT queries will not have errors!
Hopefully this will help others with the same problem.

c++ win32 edit box cursor not flashing

I'm a newbie in windows programming and am continuously running into different kinds of problems, most of which I have been able to solve by myself.
My problem at hand is the caret (or cursor) shown in text areas. The thing that indicates where you are typing your text? Well it is shown, at least, but it doesn't blink like it should.
I have an EDIT box created in WM_CREATE like so:
case WM_CREATE:
{
if(!logged) {
HWND userField = CreateWindow(
"EDIT", // Predefined class; Unicode assumed
NULL, // Button text
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, // Styles
C_WIDTH/2 - 80, // x position
C_HEIGHT - 240, // y position
160, // Button width
25, // Button height
hwnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
// initialize NONCLIENTMETRICS structure
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(ncm);
// obtain non-client metrics
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0);
// create the new font
HFONT hNewFont = CreateFontIndirect(&ncm.lfMessageFont);
// set the new font
SendMessage(userField, WM_SETFONT, (WPARAM)hNewFont, 0);
}
}
break;
That is all code concerning the edit box. I'm sorry if I'm not being clear enough or my supply of code is lacking; I'm unsure of what parts of code is relevant here and what are irrelevant. I don't think I should paste my whole code here, either.
The problem, again, is that the caret in the textbox (userField) does not blink.
Please ask for more details if you need them.
Using your code, I didn't get a flashing caret. But then i added:
SetFocus( userField );
and voilà, a flashing caret :-)
This may not be the problem the OP was experiencing, but I was experiencing the same symptom, and I'm posting my solution here in case someone else experiences this problem...
In short, if you subclass an edit control, and handle the WM_SETFOCUS event, you need to call DefSubclassProc() or your caret won't show up. Presumably, you can call ShowCaret() yourself, but you're probably safer just calling DefSubclassProc() in case there's other processing that needs to happen.
After playing around - making my code a bit tidier and stuff - I accidentally solved this on my own
I changed
HWND userField = CreateWindow(
"EDIT", // Predefined class; Unicode assumed
NULL, // Button text
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, // Styles
C_WIDTH/2 - 80, // x position
C_HEIGHT - 240, // y position
160, // Button width
25, // Button height
hwnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
Into
HWND userField = CreateWindow("EDIT", NULL, WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
C_WIDTH/2 - 80, C_HEIGHT - 240, 160, 25, hwnd, NULL, g_hInstance, NULL);
The only difference there is the hInstance: in the first code it was apparently wrong. I changed it into my global reference of hInstance.

How to print bold string in C++?

I got an old application which was written in a C++. I have 0 experience with it but I am suppose to make some changes in app. One of them is to change some text. Problem is that part of updated text needs to be bold, but i have no idea how to do that. I googled but with no much success. Only think I now is to go to new line with \nand new tab with \t.
Any clever advise?
EDIT:
Example of code:
BEGIN
STRING1 "First Example"
STRING2 "Second Example"
And place where STRING1 is used:
// WelcomeTip ---------------------------------------------//
LPSTR idsWelcomeTip = (LPSTR)GlobalAlloc(GPTR, sizeof(CHAR) * 4098 );
LoadString( waveInDlg->hInstance, STRING1, idsWelcomeTip, 4098 );
waveInDlg->hwndWelcomeTip = CreateWindow(
"STATIC",
idsWelcomeTip,
WS_CHILD | WS_VISIBLE | SS_LEFT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
waveInDlg->hwnd,
NULL,
waveInDlg->hInstance,
NULL
);
SetWindowLongPtr(waveInDlg->hwndWelcomeTip, GWLP_USERDATA ,(LONG)waveInDlg );
SendMessage(waveInDlg->hwndWelcomeTip, WM_SETFONT , (WPARAM)waveInDlg->hFontDefault , TRUE );
ShowWindow(waveInDlg->hwndWelcomeTip, SW_HIDE);
GlobalFree( (HGLOBAL)idsWelcomeTip );
Thanks,
Ile
There is no concept of bold text in C++, there may be in a particular device that displays character text, for example rich-text-format or HTML tagging or a terminal screen. The latter usually involves sending some "escape sequence" relevant to that particular terminal.
OK, I've knocked up some code that should give an overview of what you're after, I've not managed to compile it as I'd need to write a lot more to test, but it should point you in the right direction:
// Create the font you need
LOGFONT lf;
zeromemory(&lf, sizeof(LOGFONT))
lf.lfHeight = 20; // 20 pixel high font
lf.lfWeight = FW_BOLD;
strcpy(lf.lfFaceName, "Arial");
HFONT hFont = ::CreateFondIndirect(&lf);
// Set the control to use this font
SendMessage(waveInDlg->hwndWelcomeTip, WM_SETFONT, (WPARAM)hFont, NULL);
I hope this helps.
Please go through the below link for help
http://msdn.microsoft.com/en-us/library/dd162499(VS.85).aspx
Yes, you have to override WM_PAINT in your dialog class and call drawtext function.
Use DrwaText API in WM_PAINT message handler.dc.DrawText (_T ("Hello, MFC"), -1, &rect,
DT_SINGLELINE ¦ DT_CENTER ¦ DT_VCENTER);
use DrawTextEx method.
For more inforamtion go through the follwoing link
ms-help://MS.MSDNQTR.v90.en/gdi/fontext_4pbs.htm

Windows API dialogs without using resource files

I'm trying to create a dialog box using C++ and the windows API, but I don't want the dialog defined in a resource file. I can't find anything good on this on the web, and none of the examples I've read seem to define the dialog programmatically.
How can I do this?
A simple example is fine. I'm not doing anything complicated with it yet.
Raymond Chen wrote a few posts about the dialog manager:
The dialog manager, part 1: Warm-ups
The dialog manager, part 2: Creating the frame window
The dialog manager, part 3: Creating the controls
The dialog manager, part 4: The dialog loop
The dialog manager, part 5: Converting a non-modal dialog box to modal
The dialog manager, part 6: Subtleties in message loops
The dialog manager, part 7: More subtleties in message loops
The dialog manager, part 8: Custom navigation in dialog boxes
The dialog manager, part 9: Custom accelerators in dialog boxes
If all you want to do is show a window with controls, it's possible to create a window without using resource (.rc) files / scripts.
This isn't the same as a dialog, but it might be easier than creating a dialog programmatically.
First, a few notes about how this is done:
Instead of designing the dialog in the rc file, you could manually use CreateWindow (or CreateWindowEx) to create child windows of a main window. (for .NET Windows Forms programmers, these windows are like Controls).
This process will not be graphical at all (you will need to manually type in the location and size of each window), but I think this can be a great way to understand how dialogs are created under the hood.
There are some disadvantages to not using a real dialog, namely that tab will not work when switching between controls.
About the example:
This example features a dialog box with two buttons, an edit box (.NET Windows Forms programmers would think of it as a TextBox), and a check box.
It has been tested under the following conditions:
x86 build
x64 build
Unicode build (UNICODE and _UNICODE defined)
Non-Unicode build (UNICODE and _UNICODE not defined)
Built with Visual Studio's C compiler
Built with Visual Studio's C++ compiler
OS: Windows 10 64 bit
Note: UNICODE
As of the time of writing, UTF-8 is still in beta for Windows 10
If you have not enabled this setting, you should assume that any char* is ACP, not UTF-8, this applies to standard library functions too
Even though in Linux, that same standard library function would be UTF-8.
Sadly, some C++ standard library features only work with char* (e.g., exception messages).
You can still use UTF-8 in Windows without the option set, you will just have to encode it back to UTF-16 before calling winapi functions.
Here is a reddit thread with a reply from somebody who claims to have worked on UTF-8 on Windows, it has some good information.
UNICODE in Windows means "UTF-16", not "UTF-8".
Using Unicode of some kind is strongly recommended for any version of Windows that is not very old.
Be aware that if you don't use Unicode, your program may be utterly unable to open file names containing Unicode characters, handle directories (e.g., usernames) with non-ACP characters, etc.
Using ACP functions (SendMessageA,etc) without somehow verifying that UTF-8 is enabled (it's disabled by default) is probably a bug.
For max portability/flexibility, I would recommend using UTF-16 and the W version of all API functions, translating from UTF-8 to UTF-16 at the last minute. Read this page very carefully.
Now for the code:
Note that a large amount of comments have been added to try to document the windows functions, I recommend copy/pasting this into a text editor, for best results.
// This sample will work either with or without UNICODE, it looks like
// it's recommended now to use UNICODE for all new code, but I left
// the ANSI option in there just to get the absolute maximum amount
// of compatibility.
//
// Note that UNICODE and _UNICODE go together, unfortunately part
// of the Windows API uses _UNICODE, and part of it uses UNICODE.
//
// tchar.h, for example, makes heavy use of _UNICODE, and windows.h
// makes heavy use of UNICODE.
#define UNICODE
#define _UNICODE
//#undef UNICODE
//#undef _UNICODE
#include <windows.h>
#include <tchar.h>
// I made this struct to more conveniently store the
// positions / size of each window in the dialog
typedef struct SizeAndPos_s
{
int x, y, width, height;
} SizeAndPos_t;
// Typically these would be #defines, but there
// is no reason to not make them constants
const WORD ID_btnHELLO = 1;
const WORD ID_btnQUIT = 2;
const WORD ID_CheckBox = 3;
const WORD ID_txtEdit = 4;
const WORD ID_btnShow = 5;
// x, y, width, height
const SizeAndPos_t mainWindow = { 150, 150, 300, 300 };
const SizeAndPos_t btnHello = { 20, 50, 80, 25 };
const SizeAndPos_t btnQuit = { 120, 50, 80, 25 };
const SizeAndPos_t chkCheck = { 20, 90, 185, 35 };
const SizeAndPos_t txtEdit = { 20, 150, 150, 20 };
const SizeAndPos_t btnShow = { 180, 150, 80, 25 };
HWND txtEditHandle = NULL;
// hwnd: All window processes are passed the handle of the window
// that they belong to in hwnd.
// msg: Current message (e.g., WM_*) from the OS.
// wParam: First message parameter, note that these are more or less
// integers, but they are really just "data chunks" that
// you are expected to memcpy as raw data to float, etc.
// lParam: Second message parameter, same deal as above.
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
// Create the buttons
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Note that the "parent window" is the dialog itself. Since we are
// in the dialog's WndProc, the dialog's handle is passed into hwnd.
//
//CreateWindow( lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam
//CreateWindow( windowClassName, initial text, style (flags), xPos, yPos, width, height, parentHandle, menuHandle, instanceHandle, param);
CreateWindow( TEXT("Button"), TEXT("Hello"), WS_VISIBLE | WS_CHILD, btnHello.x, btnHello.y, btnHello.width, btnHello.height, hwnd, (HMENU)ID_btnHELLO, NULL, NULL);
CreateWindow( TEXT("Button"), TEXT("Quit"), WS_VISIBLE | WS_CHILD, btnQuit.x, btnQuit.y, btnQuit.width, btnQuit.height, hwnd, (HMENU)ID_btnQUIT, NULL, NULL);
// Create a checkbox
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CreateWindow( TEXT("button"), TEXT("CheckBox"), WS_VISIBLE | WS_CHILD | BS_CHECKBOX, chkCheck.x, chkCheck.y, chkCheck.width, chkCheck.height, hwnd, (HMENU)ID_CheckBox, NULL, NULL);
// Create an edit box (single line text editing), and a button to show the text
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Handle = CreateWindow(windowClassName, windowName, style, xPos, yPos, width, height, parentHandle, menuHandle, instanceHandle, param);
txtEditHandle = CreateWindow(TEXT("Edit"), TEXT("Initial Text"), WS_CHILD | WS_VISIBLE | WS_BORDER, txtEdit.x, txtEdit.y, txtEdit.width, txtEdit.height, hwnd, (HMENU)ID_txtEdit, NULL, NULL);
//CreateWindow( windowClassName, windowName, style, xPos, yPos, width, height, parentHandle, menuHandle, instanceHandle, param);
CreateWindow( TEXT("Button"), TEXT("Show"), WS_VISIBLE | WS_CHILD, btnShow.x, btnShow.y, btnShow.width, btnShow.height, hwnd, (HMENU)ID_btnShow, NULL, NULL);
// Create an Updown control. Note that this control will allow you to type in non-number characters, but it will not affect the state of the control
break;
// For more information about WM_COMMAND, see
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms647591(v=vs.85).aspx
case WM_COMMAND:
// The LOWORD of wParam identifies which control sent
// the WM_COMMAND message. The WM_COMMAND message is
// sent when the button has been clicked.
if (LOWORD(wParam) == ID_btnHELLO)
{
MessageBox(hwnd, TEXT("Hello!"), TEXT("Hello"), MB_OK);
}
else if (LOWORD(wParam) == ID_btnQUIT)
{
PostQuitMessage(0);
}
else if (LOWORD(wParam) == ID_CheckBox)
{
UINT checked = IsDlgButtonChecked(hwnd, ID_CheckBox);
if (checked)
{
CheckDlgButton(hwnd, ID_CheckBox, BST_UNCHECKED);
MessageBox(hwnd, TEXT("The checkbox has been unchecked."), TEXT("CheckBox Event"), MB_OK);
}
else
{
CheckDlgButton(hwnd, ID_CheckBox, BST_CHECKED);
MessageBox(hwnd, TEXT("The checkbox has been checked."), TEXT("CheckBox Event"), MB_OK);
}
}
else if (LOWORD(wParam) == ID_btnShow)
{
int textLength_WithNUL = GetWindowTextLength(txtEditHandle) + 1;
// WARNING: If you are compiling this for C, please remember to remove the (TCHAR*) cast.
TCHAR* textBoxText = (TCHAR*) malloc(sizeof(TCHAR) * textLength_WithNUL);
GetWindowText(txtEditHandle, textBoxText, textLength_WithNUL);
MessageBox(hwnd, textBoxText, TEXT("Here's what you typed"), MB_OK);
free(textBoxText);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
// hInstance: This handle refers to the running executable
// hPrevInstance: Not used. See https://blogs.msdn.microsoft.com/oldnewthing/20040615-00/?p=38873
// lpCmdLine: Command line arguments.
// nCmdShow: a flag that says whether the main application window
// will be minimized, maximized, or shown normally.
//
// Note that it's necessary to use _tWinMain to make it
// so that command line arguments will work, both
// with and without UNICODE / _UNICODE defined.
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS mainWindowClass = { 0 };
// You can set the main window name to anything, but
// typically you should prefix custom window classes
// with something that makes it unique.
mainWindowClass.lpszClassName = TEXT("JRH.MainWindow");
mainWindowClass.hInstance = hInstance;
mainWindowClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
mainWindowClass.lpfnWndProc = WndProc;
mainWindowClass.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClass(&mainWindowClass);
// Notes:
// - The classname identifies the TYPE of the window. Not a C type.
// This is a (TCHAR*) ID that Windows uses internally.
// - The window name is really just the window text, this is
// commonly used for captions, including the title
// bar of the window itself.
// - parentHandle is considered the "owner" of this
// window. MessageBoxes can use HWND_MESSAGE to
// free them of any window.
// - menuHandle: hMenu specifies the child-window identifier,
// an integer value used by a dialog box
// control to notify its parent about events.
// The application determines the child-window
// identifier; it must be unique for all
// child windows with the same parent window.
//CreateWindow( windowClassName, windowName, style, xPos, yPos, width, height, parentHandle, menuHandle, instanceHandle, param);
CreateWindow( mainWindowClass.lpszClassName, TEXT("Main Window"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, mainWindow.x, mainWindow.y, mainWindow.width, mainWindow.height, NULL, 0, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
// This code is based roughly on tutorial code present at http://zetcode.com/gui/winapi/
Further reading
The builtin set of window classes are rather limited, so you might be curious as to how you can define your own window classes ("Controls") using the Windows API, see the articles below:
Custom Controls in Win32 API: The Basics (Code Project)
The WINE emulator source serves as a good example of how the Windows API could be implemented, and how you can make your own window classes that imitate the behavior of builtin classes.
Zetcode.com's tutorials
NOTE: I originally intended this post to cover the creation of dialogs programmatically. Due to a mistake on my part I didn't realize that you can't just "show" a window as a dialog. Unfortunately I wasn't able to get the setup mentioned by Raymond Chen working. Even looking at WINE's source, it's not super clear.
Take a look at this toolkit that describes how to create dialogs without resource files.
It's in WTL. However, I'm sure you can pick apart the internals to achieve the same thing using the Win32 API directly.
Here you can find how to use Windows API dialogs without using resource files.
The Windows API (only the C Win32 API, no MFC) tutorial:
Windows API tutorial
Try to search MSDN for "dialog templates in memory".
See this for example: Dialog Boxes