Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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!

WinMain:
[code]
case WM_LBUTTONDOWN:
{
hdc = BeginPaint(hWnd, &ps);

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

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

EndPaint(hWnd, &ps);
}
break;
[/code]

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

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

FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpBuffer,
0,
NULL );

MessageBox( NULL, (LPCTSTR)lpBuffer, __T("Last Error"), MB_OK );
LocalFree( lpBuffer );
}
else
{
strFB = FileBuffer;
return _T(strFB);
}
return 0;
}
[/code]

readfile():
[code]
LPSTR readfile (void)
{
LPSTR greeting = _T("Hello World!");

return greeting;
}
[/code]

Many thanks again!

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [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:
    [/color]
    [code]
    InvalidateRect (hwnd, NULL, TRUE);
    UpdateWindow (hwnd);
    [/code]
    [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]
  • waggy55waggy55 Posts: 4Member
    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.