Scroll

#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(GRAY_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;

 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")));

 for(int i=0;i<1000;i++)
 {
 TextOut(hdc,0,cyChar*i,TEXT("Hello World"),wcslen(TEXT("Hello World")));
 TextOut(hdc,22*cxCaps,cyChar*i,TEXT("Hello World"),wcslen(TEXT("Hello World")));
 SetTextAlign(hdc,TA_RIGHT|TA_TOP);
 TextOut(hdc,22*cxCaps+40*cxChar,cyChar*i,TEXT("Hello World"),wcslen(TEXT("Hello World")));

 SetTextAlign(hdc,TA_LEFT|TA_TOP);

 }
 
EndPaint(hwnd,&lp);

 return 0;
 case WM_SIZE:
 cyClient=HIWORD(lParam);
 return 0;
 case WM_VSCROLL:// wParam LOW 鼠标在滚动条上的动作 
switch(LOWORD(wParam))
 {
 case SB_LINEDOWN:
 iVscrollPos+=1;
 break;
 case SB_LINEUP:
 iVscrollPos-=1;
 break;
 case SB_PAGEDOWN:
 iVscrollPos+=cyClient/cyChar;
 break;
 case SB_PAGEUP:
 iVscrollPos-=cyClient/cyChar;
 break;
 case SB_THUMBPOSITION:
 iVscrollPos=HIWORD(wParam);
 default:
 break;
 }
 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-29 14:45  奔跑吧,蜗牛!  阅读(189)  评论(0编辑  收藏  举报

导航