Tech Support > Microsoft Windows > Development Resources > Scroll in child window
Scroll in child window
Posted by Wojtek on May 2nd, 2004


Hi,

I want to have a window which contains an edit control, two buttons
below. It is a messanger, i want to write the conversation above the
edit control and have the ability of scrolling it. Can you tell me why
the following code doesn't work? The scroll scrolls the window, but in a
strange manner, so the content is unreadable after even one scrolling.


Thank you for suport

Wojtek


//code (it compiled with DevC++


#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WndProc2 (HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam);
LRESULT CALLBACK SubRozmowa (HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam);
WNDPROC rozmowa;

HINSTANCE hInst;

HBRUSH tlo=CreateSolidBrush(RGB(0,0,0));
HBRUSH act=CreateSolidBrush(RGB(116,49,251));

#define NUMLINES 5

extern HBRUSH act,tlo;

int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("programik") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass2 ;
HMENU menu;
hInst=hInstance ;

wndclass2.style = CS_HREDRAW | CS_VREDRAW;
wndclass2.lpfnWndProc = WndProc2 ;
wndclass2.cbClsExtra = 0 ;
wndclass2.cbWndExtra = 0 ;
wndclass2.hInstance = hInst ;
wndclass2.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass2.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass2.hbrBackground = (HBRUSH) tlo ;
wndclass2.lpszMenuName = NULL ;
wndclass2.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass2)) {MessageBox (NULL, TEXT
("Internal Error"),szAppName, MB_ICONERROR);return 0;}

hwnd= CreateWindow (szAppName,
TEXT("Chat"),
WS_OVERLAPPED|WS_CAPTION|WS_THICKFRAME|WS_SYSMENU,
200,100,
500,350,
NULL,NULL,
hInst,NULL);


ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}

return msg.wParam ;
}


LRESULT CALLBACK WndProc2 (HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
static int cxChar, cxCaps, bsize=20, cxClient, cyClient;
HDC hdc ;
int i, x, y, iVertPos, iHorzPos;
PAINTSTRUCT ps ;
SCROLLINFO si ;
TCHAR szBuffer[10] ;
TEXTMETRIC tm ;
static HWND hwndButton[5];
RECT rect;
TCHAR buforek[50];

switch (message)
{
case WM_CREATE:

hdc = GetDC(hwnd) ;
GetTextMetrics(hdc, &tm) ;
cxChar=tm.tmAveCharWidth ; //7
cxCaps=(tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ; //10
//cyChar=tm.tmHeight + tm.tmExternalLeading ; //16
ReleaseDC (hwnd, hdc) ;
GetClientRect (hwnd, &rect);

hwndButton[0]=CreateWindow (TEXT("edit"),NULL,
WS_CHILD|WS_VISIBLE|WS_BORDER|
WS_VSCROLL|ES_MULTILINE|
ES_AUTOVSCROLL,

0,rect.bottom-150,rect.right,120,hwnd,(HMENU)0,hInst, NULL);

hwndButton[1]=CreateWindow (TEXT("button"),"Send",
WS_CHILD|WS_VISIBLE|BS_CENTER,
2,rect.bottom-28,60,25, hwnd,
(HMENU)1,hInst,NULL);

hwndButton[2]=CreateWindow (TEXT("button"),"enter send",

WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,

rect.right-101,rect.bottom-28,100,25,hwnd,(HMENU)2,hInst,NULL);

hwndButton[3] = CreateWindow (TEXT("edit"),NULL,
WS_CHILD | WS_VISIBLE | BS_LEFT
|WS_VSCROLL |BS_FLAT |BS_OWNERDRAW,
0,0,
rect.right, rect.bottom-154,
hwnd, (HMENU)3,hInst, NULL);





rozmowa=(WNDPROC)GetWindowLong(hwndButton[3],GWL_WNDPROC);

SetWindowLong(hwndButton[3],GWL_WNDPROC,(LONG)SubRozmowa);

return 0 ;

case WM_SIZE:
return 0 ;

case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;

EndPaint (hwnd, &ps) ;
return 0 ;

case WM_COMMAND:
switch(LOWORD(wParam))
{
case 1:
SendMessage (GetDlgItem (hwnd, 3), WM_HSCROLL, 5,
0);
break;
}
return 0;

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

LRESULT CALLBACK SubRozmowa (HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
static int ile;
static bool first=true;
HDC hdc ;
int i, y, iVertPos,x = GetWindowLong(hwnd, GWL_ID);
PAINTSTRUCT ps ;
SCROLLINFO si ;
TCHAR szBuffer[10],buforek[50];
static HBRUSH hBrush, hBrush2, hTempBrush;
static HANDLE hwndButton[4];
RECT rect;

switch (message)
{
case WM_PAINT :
if(first)
{
GetClientRect (hwnd, &rect);
si.cbSize = sizeof (si) ;
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 1;
si.nMax = 10;
si.nPage = 10;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
first=false;
}
hdc=BeginPaint(hwnd,&ps);
RoundRect(hdc,10,10,50,50,0,0);
TextOut(hdc,10,10,"lalala",7);
EndPaint(hwnd,&ps);
return 0;

case WM_SIZE:
return 0;

case WM_LBUTTONDOWN:
return 0;

case WM_VSCROLL:
GetClientRect (hwnd, &rect);
si.cbSize = sizeof (si) ;
si.fMask = SIF_ALL ;
GetScrollInfo (hwnd, SB_VERT, &si) ;
iVertPos = si.nPos ;

switch (LOWORD (wParam))
{
case SB_TOP:
si.nPos = si.nMin ;
break ;
case SB_BOTTOM:
si.nPos = si.nMax ;
break ;
case SB_LINEUP:
si.nPos -= 1 ;
break ;
case SB_LINEDOWN:
si.nPos += 1 ;
break ;
case SB_PAGEUP:
si.nPos -= si.nPage ;
break ;
case SB_PAGEDOWN:
si.nPos += si.nPage ;
break ;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos ;
break ;
}

si.fMask = SIF_POS ;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
GetScrollInfo (hwnd, SB_VERT, &si) ;

if (si.nPos != iVertPos)
{
ScrollWindow(hwnd,0,4*(iVertPos-si.nPos),NULL,&rect);
UpdateWindow(hwnd);
}
return 0;

case WM_HSCROLL:
itoa(ile,buforek,10);
hdc=GetDC(hwnd);
//CreateWindow
("static",buforek,WS_CHILD|WS_VISIBLE|BS_LEFT,0,40 *ile,500,40,hwnd,(HMENU)ile,hInst,NULL);
//DrawText(hdc,);
RoundRect(hdc,10,10,50,50,0,0);
si.cbSize = sizeof (si) ;
si.fMask = SIF_ALL ;
GetScrollInfo (hwnd, SB_VERT, &si);
si.nMax+=10;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
SendMessage (hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
SendMessage (hwnd, WM_VSCROLL, SB_LINEUP, 0);
ile++;
return 0;
}
return CallWindowProc (rozmowa, hwnd, message, wParam, lParam);
}




Posted by odah on May 3rd, 2004


Which edit control doesn't work hwndButton[0] or hwndButton[3]?
hwndButton[0] should be OK.
hwndButton[3] looks strange.

Why does wndButton[3] have BS_OWNERDRAW style ?
Does it have any effect on edit control?
Even if it does, it would send paint message to its parent window.
In that case you would have to handle that message.

You subclassed wndButton[3] and handled WM_PAINT message,
but you didn't include any code to draw text,
beside "lalala", but even that won't scroll since it is painted every time
at the same location (10,10)

If you want to draw text by yourself, you have know by how many pixels the
window is scrolled and than use it in you draw function
DrawText(hdc, pixels scrolled horizontally, pixels scrolled
vertically,.....);

Mariusz



P.S. Goraco polecam "Peogramming Windows" 5th ed by Charles Petzol
THE BEST book ever




Similar Posts