Having returned to dabbling in programming after some time, I am writing a simple text editor as refresh exercise.
Using Visual Studio 2019 to produce Windows programme
When I enter a character via the keyboard, WM_CHAR picks it up and displays it, but if I resize the window it is erased from the screen. If I put the TextOut function in WM_PAINT as well it works OK, BUT If I put the TextOut only in WM_PAINT the character is not displayed until the window is resized.
Relevant Code below
Code:
static struct NumEditStructure { // structure for input fields
TCHAR StrCha[SLEN]; // characters in string
int StrCou; // total number of characters in string
} EditStru, * PtrES;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
int lmId, lmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR greeting[] = _T("Hello, Windows desktop!");
switch (message){
case WM_PAINT: // message 15
hdc = BeginPaint(hWnd, &ps);
// Here your application is laid out.
// For this introduction, we just print out "Hello, Windows desktop!"
// in the top left corner.
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc,
5, 5,
greeting, _tcslen(greeting));
// Needed when paint called from ReSize etc
TextOut(hdc,
60, 60,
EditStru.StrCha, _tcslen(EditStru.StrCha));
EndPaint(hWnd, &ps);
break;
case WM_CHAR: // message VaLUE 0x0102
hdc = GetDC(hWnd);
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
lmId = LOWORD(lParam);
lmEvent = HIWORD(lParam);
EditStru.StrCha[EditStru.StrCou ] = wmId;
EditStru.StrCha[EditStru.StrCou+1 ] = 0x00;
EditStru.StrCou += 1;
// Needed to display entry at initial entry, before paint called from ReSize etc
TextOut(hdc,
60, 60,
EditStru.StrCha, _tcslen(EditStru.StrCha));
SendMessage(hWnd, WM_PAINT, 0, 0);
break;
case WM_DESTROY: // message
PostQuitMessage(0);
break;
default: // message
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}