C/C++ Windows API

Moderators: Lundin
Number of threads: 450
Number of posts: 1225

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Passing String to TextOut Posted by waggy55 on 25 Dec 2010 at 3:37 PM
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:
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;


ReadTxt():
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;
}


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

return greeting;
}


Many thanks again!
Report
Re: Passing String to TextOut Posted by AsmGuru62 on 26 Dec 2010 at 6:30 AM
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);

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.
Report
Re: Passing String to TextOut Posted by waggy55 on 27 Dec 2010 at 1:06 PM
Many thanks for your help and advice, superb detail! Thanks a lot for setting me straight on BeginPaint/EndPaint too! Works perfectly now!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.