滚动后无法打印文本. [英] Unable to print text after scrolling....
问题描述
我已经使用win32函数编写了一个小型C程序.
我想基本启用行缓冲.例如:当文本到达窗口末尾时,我想将文本滚动到上方一行,然后将下一行打印到窗口末尾.
问题:到达窗口末尾时,我可以向上滚动文本,但是看不到要打印到窗口末尾的下一行.
您能指出我代码中的缺陷吗?
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.
// 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 );
}
//
注意:我想使用原始Win32函数来实现此目的,但不想使用MFC或其他一些高级概念重新实现该功能.
Note: I wanted to achieve this using raw Win32 functions but not re-implementing the same using MFC or some other advanced concepts.
Thanks
推荐答案
您需要实现WM_VSCROLL
的处理程序,并在必要时实现WM_HSCROLL
消息,然后在WM_PAINT
处理程序中显示所有当前是数据可视部分的一部分的行.例如,如果您向下滚动了一行,则需要打印2-N行,其中N是适合窗口可见部分的最后一行.在Internet上有许多实现此目的的代码示例,例如此代码 [ ^ ].
You need to implement handlers for theWM_VSCROLL
and, if necessary,WM_HSCROLL
messages, then in yourWM_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[^].
这篇关于滚动后无法打印文本.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!