Windows 학습 노트--SYSMETS2

SYSMETS1 프로그램의 토대 위에서 창에 가장 중요한 요소 중 하나인 스크롤 바를 추가합니다.
코드 설명은 다음과 같습니다.

  
  
  
  
  1. //SYSMETS2.cpp  
  2. #include<windows.h>  
  3. #include"sysmets.h"  
  4.  
  5. LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);  
  6.  
  7.  
  8. int APIENTRY WinMain(HINSTANCE hInstance,  
  9.                      HINSTANCE hPrevInstance,  
  10.                      PSTR     szCmdLine,  
  11.                      int       iCmdShow)  
  12. {  
  13.     static TCHAR szAppName[]=TEXT("SysMets2");  
  14.     HWND hWnd;  
  15.     MSG msg;  
  16.     WNDCLASS wndclass;  
  17.  
  18.     // 10  
  19.     wndclass.cbClsExtra=0;  
  20.     wndclass.cbWndExtra=0;  
  21.  
  22.     wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);  
  23.  
  24.     wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);  
  25.     wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);  
  26.  
  27.     wndclass.hInstance=hInstance;  
  28.  
  29.     wndclass.lpfnWndProc=WndProc;                                 //  
  30.     wndclass.lpszClassName=szAppName;                             //  
  31.  
  32.     wndclass.lpszMenuName=NULL;  
  33.  
  34.     wndclass.style=CS_HREDRAW|CS_VREDRAW;  
  35.  
  36.     //  
  37.     int retVal=RegisterClass(&wndclass); // , 0; , 0  
  38.     if(retVal == 0)  
  39.     {  
  40.         MessageBox(NULL,TEXT("This progrom requires Windows NT !"),szAppName,MB_ICONERROR);  
  41.         return 0;  
  42.     }  
  43.  
  44.     //  
  45.     hWnd=CreateWindow(szAppName,  
  46.                       TEXT("Get System Metrics No.2"),  
  47.                       WS_OVERLAPPEDWINDOW | WS_VSCROLL,  
  48.                       CW_USEDEFAULT,CW_USEDEFAULT,  
  49.                       CW_USEDEFAULT,CW_USEDEFAULT,  
  50.                       NULL,NULL,hInstance,NULL);  
  51.  
  52.     ShowWindow(hWnd,iCmdShow);  
  53.     UpdateWindow(hWnd);  
  54.  
  55.     //  
  56.     while(GetMessage(&msg,NULL,0,0))  
  57.     {  
  58.         TranslateMessage(&msg);  
  59.         DispatchMessage(&msg);  
  60.     }  
  61.                          
  62.  
  63.     return msg.wParam;  
  64. }  
  65.  
  66. LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)  
  67. {  
  68.     static int cxChar,cxCaps,cyChar,cyClient,iVscrollPos;  
  69.     HDC hdc;  
  70.     int i;  
  71.     PAINTSTRUCT ps;  
  72.     TCHAR szBuffer[10];  
  73.     TEXTMETRIC tm;  
  74.  
  75.     //  
  76.     switch(message){  
  77.       
  78.     case WM_CREATE: //CreateWindows , ( API , )  
  79.         hdc=GetDC(hWnd);  
  80.  
  81.         GetTextMetrics(hdc,&tm);  
  82.         cxChar=tm.tmAveCharWidth;  
  83.         cxCaps=(tm.tmPitchAndFamily & 1? 3:2)* cxChar/2;  
  84.         cyChar=tm.tmHeight+tm.tmExternalLeading;  
  85.  
  86.         ReleaseDC(hWnd,hdc);  
  87.  
  88.         //  
  89.         SetScrollRange(hWnd,SB_VERT,0,NUMLINES-1,FALSE);  
  90.         SetScrollPos(hWnd,SB_VERT,iVscrollPos,TRUE);  
  91.         return 0;  
  92.  
  93.     case WM_SIZE: //( , ; WM_PAINT )  
  94.         cyClient=HIWORD(lParam); //lParam: , ; , .  
  95.         return 0;  
  96.  
  97.     case WM_VSCROLL:  
  98.         switch(LOWORD(wParam)) //wParam: ,  
  99.         {  
  100.         case SB_LINEUP:  
  101.             iVscrollPos-=1;  
  102.             break;  
  103.         case SB_LINEDOWN:  
  104.             iVscrollPos+=1;  
  105.             break;  
  106.         case SB_PAGEUP:  
  107.             iVscrollPos-=cyClient/cyChar;  
  108.             break;  
  109.         case SB_PAGEDOWN:  
  110.             iVscrollPos+=cyClient/cyChar;  
  111.             break;  
  112.  
  113.         case SB_THUMBPOSITION:  
  114.             iVscrollPos=HIWORD(wParam); // , ( )  
  115.             break;  
  116.         default:  
  117.             break;  
  118.         }  
  119.  
  120.         // , iVscrollPos  
  121.         iVscrollPos=max(0,min(iVscrollPos,NUMLINES-1));  
  122.         if(GetScrollPos(hWnd,SB_VERT) != iVscrollPos)  
  123.         {  
  124.             SetScrollPos(hWnd,SB_VERT,iVscrollPos,TRUE);  
  125.             InvalidateRect(hWnd,NULL,TRUE);  
  126.         }  
  127.         return 0;  
  128.  
  129.  
  130.     case WM_PAINT:  
  131.         hdc=BeginPaint(hWnd,&ps);  
  132.           
  133.           
  134.         for(i=0;i<NUMLINES;i++)  
  135.         {  
  136.             TextOut(hdc,0,cyChar*(i-iVscrollPos),sysmetrics[i].szLabel,lstrlen(sysmetrics[i].szLabel));  
  137.             TextOut(hdc,22*cxCaps,cyChar*(i-iVscrollPos),sysmetrics[i].szDesc,lstrlen(sysmetrics[i].szDesc));  
  138.  
  139.             SetTextAlign(hdc,TA_LEFT|TA_TOP);  
  140.             TextOut(hdc,  
  141.                     22*cxCaps+40*cxChar,cyChar*(i-iVscrollPos),  
  142.                     szBuffer,wsprintf(szBuffer,TEXT("%5d"),GetSystemMetrics(sysmetrics[i].iIndex)));  
  143.             SetTextAlign(hdc,TA_LEFT|TA_TOP);  
  144.         }  
  145.  
  146.         EndPaint(hWnd,&ps);  
  147.         return 0;  
  148.  
  149.     case WM_DESTROY:  
  150.         PostQuitMessage(0);  
  151.         return 0;  
  152.     }  
  153.  
  154.     return DefWindowProc(hWnd,message,wParam,lParam);  
  155. }  

 
 
함수 프로토타입:
1.
ATOM RegisterClass(
  const WNDCLASS* lpWndClass
); 

typedef  WORD                ATOM;
typedef  unsigned short      WORD;
2.
BOOL InvalidateRect( 
  HWND hWnd, 
  const RECT* lpRect, 
  BOOL bErase
); 

const RECT* lpRect,//는 NULL이며, 전체 클라이언트 영역이 업데이트 영역임을 나타냅니다.
BOOL bErase//TRUE인 경우 배경색을 지웁니다.그렇지 않으면 지우지 않습니다.
3.
BOOL SetScrollRange( 
  HWND hWnd, 
  int nBar, 
  int nMinPos, 
  int nMaxPos, 
  BOOL bRedraw 
); 

int nBar, //SB_VERT | SB_HORZ BOOL bRedraw//일반적으로 스크롤 막대의 범위는 다시 그릴 필요가 없습니다.
int SetScrollPos( 
  HWND hWnd, 
  int nBar, 
  int nPos, 
  BOOL bRedraw 
); 

업데이트할 때마다 BOOL bRedraw//슬라이더의 위치를 다시 그려야 합니다.

좋은 웹페이지 즐겨찾기