Passing String to TextOut

Hi there,
I have just started programming in the win32 API and I've had a small problem that's been bothering me for a couple of days - I'm sure I'm missing out something fundamental, so apologies if I'm doing something foolish.

The windows procedure for left mouse button press calls LPSTR from ReadText() and displays the correct text. However, when the window is resized and LM button is pressed again "/0" is displayed - and I'm not sure why, I was expecting the correct text to be displayed. If I call readfile() there is no problem and the string displays correctly.

I have copied my code below, followed by the functions - any help would be really appreciated. Many thanks!

hdc = BeginPaint(hWnd, &ps);

//Application-specific layout
str = ReadTxt();

5, 5,
str, strlen(str));
// End application-specific layout section.

EndPaint(hWnd, &ps);

LPSTR ReadTxt (void)
char FileBuffer[128] = {"/0"};
DWORD dwBytesRead = 0;
LPSTR strFB = "0" ;

if(!ReadFile(hFile, FileBuffer, 128, &dwBytesRead, NULL))
void* lpBuffer;

(LPTSTR) &lpBuffer,

MessageBox( NULL, (LPCTSTR)lpBuffer, __T("Last Error"), MB_OK );
LocalFree( lpBuffer );
strFB = FileBuffer;
return _T(strFB);
return 0;

LPSTR readfile (void)
LPSTR greeting = _T("Hello World!");

return greeting;

Many thanks again!


  • [color=Blue]BeginPaint/EndPaint are only to be used inside a response to WM_PAINT message. They cannot be used in WM_LBUTTONDOWN.

    Basics of windows programming:
    1. HWND has a client area attached
    2. If anything changes in that client area, then HWND must be given a signal to redraw itself. It is done with following two lines:
    InvalidateRect (hwnd, NULL, TRUE);
    UpdateWindow (hwnd);
    [color=Blue]So, in your case - you need a text buffer, which are you going to paint (with TextOut) INSIDE WM_PAINT message case. At the beginning the text is empty and nothing painted in your window - you still call TextOut and paint whatever in buffer, but if it's empty then you see nothing, obviously. Then you click a mouse button and windows calls your window procedure and code jumps into WM_LBUTTONDOWN case - where you: 1) load the text file and 2) GIVE THAT SIGNAL to redraw. The window will automatically redraw the text buffer.

    Any time anything changes - say, you insert a character into your text or insert a whole line - you do the change in data buffer and GIVE THAT SIGNAL to redraw again. Take a look at the InvalidateRect() description - it allows to redraw only a portion of the client area, so you can reduce flicker. If you insert the character - you only need to redraw the changed line - not the whole text.[/color]
  • Many thanks for your help and advice, superb detail! Thanks a lot for setting me straight on BeginPaint/EndPaint too! Works perfectly now!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!