Scroll2

#include <Windows.h>
#include <stdio.h>
#include "resource.h"
//#include <afxwin.h>

LRESULT CALLBACK MyWindowProc(
							HWND hwnd, 
							UINT uMsg, 
							WPARAM wParam, 
							LPARAM lParam 
							); 

int WINAPI WinMain(  HINSTANCE hInstance,  HINSTANCE hPrevInstance, LPSTR lpCmdLine,  int nShowCmd )
{
	HWND hwnd;
	WNDCLASS wndclass;
	MSG msg;
    static TCHAR MyAppName[]=TEXT("My first windows app");
	wndclass.style=CS_HREDRAW|CS_VREDRAW;
	wndclass.lpfnWndProc=MyWindowProc;
	wndclass.cbClsExtra=0;//预留的额外空间
	wndclass.cbWndExtra=0;
	wndclass.hInstance=hInstance;
	wndclass.hIcon=::LoadIcon(NULL,IDI_INFORMATION);
	wndclass.hCursor=::LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR1));
	wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName=NULL;
	wndclass.lpszClassName=TEXT("My first windows app");
	if (!RegisterClass(&wndclass))
	{
		int i=GetLastError();
		//char str[25]={0};
		wchar_t chResult[128]={0};
		_itow(i,chResult,10);
		//sprintf(str,TEXT("%d"),i);
	   //sprintf(buffer,L"Key State = 0X%X  ",key_state);
		MessageBox(NULL,chResult,TEXT("Error"),MB_OK);
		//AfxMessageBox(TEXT("Error") );
	}
	hwnd=CreateWindow( MyAppName,
		TEXT("My first windows"),
		WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL,
		CW_USEDEFAULT ,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		NULL,NULL,
		hInstance,
		NULL
		);
	ShowWindow(hwnd,SW_SHOWNORMAL); 
	//ShowWindow(hwnd,iCmdShow);
	UpdateWindow(hwnd);
	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}
LRESULT CALLBACK MyWindowProc(
							  HWND hwnd, 
							  UINT uMsg, 
							  WPARAM wParam, 
							  LPARAM lParam 
							  )
{
	PAINTSTRUCT lp;
	RECT rect;
	HDC hdc;
	TEXTMETRIC tm;
	static int cxChar,cyChar,cxCaps;

	static int cxClient,cyClient,iVscrollPos;

	SCROLLINFO si;
	 int inVertPos,iPaintBeg,iPaintEnd,y;

	switch(uMsg)
	{
	case WM_CREATE:
		hdc=GetDC(hwnd);

		GetTextMetrics(hdc,&tm);
		cxChar=tm.tmAveCharWidth;
		cyChar=tm.tmHeight/*+tm.tmExternalLeading*/;
		cxCaps=(tm.tmPitchAndFamily&1?3:2)*cxChar/2;//平均宽度
		ReleaseDC(hwnd,hdc);

		//SetScrollRange(hwnd,SB_VERT,0,999,FALSE);
		//SetScrollPos(hwnd,SB_VERT,iVscrollPos,TRUE);
		return 0;

	case WM_PAINT:
		/*HDC*/ hdc=BeginPaint(hwnd,&lp);//
		/*
		在case里面不能直接声明变量
		你有两种选择
			1. 将变量j的声明提取到switch的外面
			2. 将case的语句块用大括号(“{” “}”)括起来
		*/
		 
		//GetClientRect(hwnd,&rect);
		//DrawText(dc,TEXT("Hello World"),-1,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
		//TextOut(dc,(rect.right-rect.left)/2,(rect.bottom-rect.top)/2,TEXT("hello world"),wcslen(TEXT("hello world")));
		si.cbSize=sizeof(si);
		si.fMask=SIF_POS;
		GetScrollInfo(hwnd,SB_VERT,&si);
		inVertPos=si.nPos;
		iPaintBeg=max(0,inVertPos+lp.rcPaint.top/cyChar);//
		iPaintEnd=min(999,inVertPos+lp.rcPaint.bottom/cyChar);
		for(int i=iPaintBeg;i<=iPaintEnd;i++)
		{
			y=cyChar*(i-inVertPos);
			TextOut(hdc,0,y,TEXT("Hello World"),wcslen(TEXT("Hello World")));
			TextOut(hdc,22*cxCaps,y,TEXT("Hello World"),wcslen(TEXT("Hello World")));
			SetTextAlign(hdc,TA_RIGHT|TA_TOP);
			TextOut(hdc,22*cxCaps+40*cxChar,y,TEXT("Hello World"),wcslen(TEXT("Hello World")));
			
			SetTextAlign(hdc,TA_LEFT|TA_TOP);
		}
	
		EndPaint(hwnd,&lp);
		
		return 0;
	case WM_SIZE:
		cyClient=HIWORD(lParam);
		si.cbSize=sizeof(si);
		//si.fMask=SIF_PAGE|SIF_POS;
		si.fMask=SIF_PAGE|SIF_RANGE;
		si.nMax=999;
		si.nMin=0;
		si.nPage=cyClient/cyChar;
		//SetScrollInfo(hwnd,SB_VERT,&si,FALSE);
		SetScrollInfo(hwnd,SB_VERT,&si,TRUE);
		return 0;
	case WM_VSCROLL:

		si.cbSize=sizeof(si);
		si.fMask=SIF_ALL;
		GetScrollInfo(hwnd,SB_VERT,&si);

		 inVertPos = si.nPos ;

		switch(LOWORD(wParam))
		{
		case SB_LINEDOWN:
			si.nPos+=1;
			break;
		case SB_LINEUP:
			si.nPos-=1;
			break;
		case SB_PAGEDOWN:
			si.nPos+=si.nPage;
			break;
		case SB_PAGEUP:
			si.nPos-=si.nPage;
			break;
		case SB_THUMBPOSITION:
			si.nPos=HIWORD(wParam);
		default:
			break;
		}
		si.fMask=SIF_POS;
		SetScrollInfo(hwnd,SB_VERT,&si,TRUE);
		GetScrollInfo(hwnd,SB_VERT,&si);

		if (si.nPos!=inVertPos)
		{
			ScrollWindow(hwnd,0,cyChar*(inVertPos-si.nPos),NULL,NULL);
			UpdateWindow(hwnd);
		}
		//iVscrollPos=max(0,min(iVscrollPos,999));
	/*	if (iVscrollPos!=GetScrollPos(hwnd,SB_VERT))
		{
			SetScrollPos(hwnd,SB_VERT,iVscrollPos,TRUE);
			InvalidateRect(hwnd,NULL,TRUE);
		}*/

        return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
 


	}

	return DefWindowProc(hwnd,uMsg, wParam,lParam );

}

  

posted on 2013-05-30 12:47  奔跑吧,蜗牛!  阅读(156)  评论(0编辑  收藏  举报

导航