Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have written a small C program using win32 functions.
I want to basically enable the line buffering. For ex: when the text reaches to the end of the window, i want to scroll the text to one line above then then print the next line to the end of the window.
Problem: I am able to scroll the text up when i reach to the end of the window but i m unable to see the next lines being printed to the end of the window.
Can you please point out the flaw in my code.
C++
// code for your reference //
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	static      cxChar, cyChar, cxClient, cyClient, iVertPos = 0, iCnt = 0;
	HDC			hdc;
	PAINTSTRUCT ps;
	TEXTMETRIC	tm;
	static		RECT rect;
	static      TCHAR cCh, cScrollFlag = 'N';
	TCHAR		szBuffer[30];
	
	switch( message )
	{
		case WM_CREATE:
			hdc = GetDC( hwnd );
			GetTextMetrics( hdc, &tm );
			cxChar = tm.tmAveCharWidth;
			cyChar = tm.tmHeight;
			ReleaseDC( hwnd, hdc );
			break;

		case WM_SIZE:
			cxClient = LOWORD( lParam );
			cyClient = HIWORD( lParam );
			
			rect.left   = cxChar;
			rect.right  = cxClient;
			rect.top    = cyChar;
			rect.bottom = cyClient;
			
			InvalidateRect( hwnd, &rect, TRUE );
			
			break;

		case WM_CHAR:
			cCh = wParam;
			iVertPos = ++iCnt * cyChar;

			if( iVertPos >= cyClient )  // reached to the end of the window //
			{
				cScrollFlag = 'Y';
				ScrollWindow( hwnd, 0, -cyChar, &rect, &rect );
			}
			else                        // did not reach to the end of the window //
			{
				InvalidateRect( hwnd, &rect, FALSE );
				cScrollFlag = 'N';
			}
			break;

		case WM_PAINT:
			hdc = BeginPaint( hwnd, &ps );
			wsprintf( szBuffer, TEXT("[%2d] Key pressed: %c, current pos: %d"), iCnt, cCh, iVertPos );
			if( iVertPos )
			{
				if( cScrollFlag == 'Y' )  // if scrolling required //
				{
					TextOut( hdc, cxChar, iVertPos, szBuffer, lstrlen( szBuffer ) );
				}
				else                     // scroll not required //
				{
					TextOut( hdc, cxChar, iVertPos, szBuffer, lstrlen( szBuffer ) );
				}
			}
			EndPaint( hwnd, &ps );
			break;

		case WM_DESTROY:
			PostQuitMessage(0);
			break;
	}
	return DefWindowProc( hwnd, message, wParam, lParam );
}
//


Note: I wanted to achieve this using raw Win32 functions but not re-implementing the same using MFC or some other advanced concepts.



Thanks
Posted
Updated 8-Sep-11 22:52pm
v2
Comments
Prerak Patel 9-Sep-11 4:53am    
Use code block for code segments.
Eugen Podsypalnikov 9-Sep-11 7:58am    
Have you already tried to scroll without the clipping (last two parameters =NULL) ? :)
[no name] 9-Sep-11 8:14am    
but still it behaves the same way...like whenever the window is full, i dont see the next text being printed out at the last line.
Eugen Podsypalnikov 9-Sep-11 8:33am    
Please, try to invalidate the bottom by resizing, overlapping, or minimizing/reparing :)
Is it then visible ?
[no name] 9-Sep-11 8:36am    
like Richard suggested, i need to print vertically at cyClient - cyChar has worked for me. Thanks for your inputs aswell. :)

1 solution

You need to implement handlers for the WM_VSCROLL and, if necessary, WM_HSCROLL messages, then in your WM_PAINT handler you need to display every line that is currently part of the visual portion of your data. For example if you have scrolled down one line then you need to print lines 2-N, where N is the last line that will fit in the visible portion of the window. There are various samples of code to achieve this around the internet, such as this one[^].
 
Share this answer
 
Comments
[no name] 9-Sep-11 7:27am    
but i dont want the scroll bar to be displayed nor i want to perform automation on scroll bars through mouse. All I am doing here is scrolling the last line and then printing on the same line with a next text. So in this scenario where does the topic of Scroll bars comes.
Richard MacCutchan 9-Sep-11 7:39am    
Sorry I guess I misunderstood.
However, from what I understand above you are scrolling the window when you detect overflow, but you do not invalidate the window so your program does not receive a WM_PAINT message.
[no name] 9-Sep-11 7:42am    
When i use ScrollWindow in my program, it automatically calls WM_PAINT message.
Is there any way to provide screenshots of the output here ? It would be really help full to explain the situation
enhzflep 9-Sep-11 7:48am    
Just post to a public image sharing site then post the link here.
ImgShack being one such place that comes to mind.
[no name] 9-Sep-11 7:51am    
but i guess you have understood the problem now. Can you please suggest me in fixing the bug ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900