標籤:bsp rect sizeof window instance rac sla pst nbsp
源碼
1 #include<Windows.h> 2 #include<tchar.h> 3 #include<stdio.h> 4 #define NUM 1000 5 6 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); 7 8 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow) 9 { 10 WNDCLASS WndClass; 11 TCHAR* ClassName = TEXT("MyClass"); 12 HWND hwnd; 13 MSG msg; 14 HBRUSH hBrush; 15 16 hBrush = CreateSolidBrush(RGB(0x20, 0x85, 0x41)); 17 WndClass.cbClsExtra = 0; 18 WndClass.cbWndExtra = 0; 19 WndClass.hbrBackground = hBrush; 20 WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); 21 WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 22 WndClass.hInstance = hInst; 23 WndClass.lpfnWndProc = WindProc; 24 WndClass.lpszClassName = ClassName; 25 WndClass.lpszMenuName = NULL; 26 WndClass.style = CS_VREDRAW | CS_HREDRAW; 27 28 if (!RegisterClass(&WndClass)) 29 { 30 MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK); 31 return 0; 32 } 33 34 //WS_VSCROLL視窗捲軸 35 hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW | WS_VSCROLL, 0, 0, 1000, 800, NULL, NULL, hInst, NULL); 36 if (hwnd == NULL) 37 { 38 MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK); 39 return 0; 40 } 41 ShowWindow(hwnd, nShow); 42 UpdateWindow(hwnd); 43 44 while (GetMessage(&msg, NULL, 0, 0)) 45 { 46 TranslateMessage(&msg); 47 DispatchMessage(&msg); 48 } 49 50 return 0; 51 } 52 53 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 54 { 55 HDC hdc; 56 static HPEN hPen1,hPen2; 57 static HBRUSH hBrush; 58 PAINTSTRUCT pt; 59 TEXTMETRIC ts; 60 TCHAR buf[1024]; 61 int i; //行數 62 static int cyChar; //字型大小 63 static int cy; //客戶區高度 64 static SCROLLINFO si; 65 static int position = 0; 66 switch (message) 67 { 68 case WM_CREATE: 69 hdc = GetDC(hwnd); 70 hBrush = CreateSolidBrush(RGB(249, 98, 241)); 71 hPen1 = CreatePen(PS_SOLID,100, RGB(0, 0, 255)); 72 GetTextMetrics(hdc, &ts); 73 ReleaseDC(hwnd, hdc); 74 cyChar = ts.tmHeight; 75 si.cbSize = sizeof(si); 76 return 0; 77 case WM_SIZE: 78 //視窗高度 79 cy = HIWORD(lParam); 80 si.fMask = SIF_ALL; 81 si.nMax = NUM - 1; 82 si.nMin = 0; 83 si.nPage = cy / cyChar; 84 si.nPos = position; 85 si.nTrackPos = 0; 86 SetScrollInfo(hwnd, SB_VERT, &si, TRUE); 87 case WM_VSCROLL: 88 //當捲軸是視窗的一部分時,可以忽略IParam參數 89 switch (LOWORD(wParam)) 90 { 91 case SB_BOTTOM: 92 si.nPos = si.nMax; 93 break; 94 case SB_LINEDOWN: 95 si.nPos++; 96 break; 97 case SB_LINEUP: 98 si.nPos--; 99 break;100 case SB_PAGEDOWN:101 si.nPos = si.nPos + cy / cyChar;102 break;103 case SB_PAGEUP:104 si.nPos = si.nPos - cy / cyChar;105 break;106 case SB_THUMBPOSITION:107 si.nPos = HIWORD(wParam);108 break;109 default:110 break;111 }112 si.fMask = SIF_POS;113 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);114 GetScrollBarInfo(hwnd, SB_VERT, &si);115 position = si.nPos;116 //UpdateWindow(hwnd);//視窗必須有無效區,否則重新整理也是白刷。替換為InvalidateRect強制重新整理117 //InvalidateRect函數會產生WM_PAINT訊息118 InvalidateRect(hwnd, NULL, FALSE);119 return 0;120 case WM_PAINT:121 GetScrollBarInfo(hwnd, SB_VERT, &si);122 //調用BeginPaint後整個客戶去都是有效。ValidateRect使客戶區中任意矩形地區變成有效123 hdc = BeginPaint(hwnd, &pt);124 hPen2=(HPEN)SelectObject(hdc, hPen1);125 SelectObject(hdc, hBrush);126 MoveToEx(hdc, 500, 100, NULL);127 LineTo(hdc, 100, 100);128 Rectangle(hdc, 500, 50, 200, 200);129 SelectObject(hdc, hPen2);130 131 for (i = 0; i < NUM; i++)132 {133 _stprintf(buf, TEXT("---------------%d--------------"), si.nPos + i);134 TextOut(hdc, 0, i * cyChar, buf, _tcslen(buf));135 }136 EndPaint(hwnd, &pt);137 return 0;138 case WM_DESTROY:139 PostQuitMessage(0);//發送WM_QUIT訊息140 return 0;141 default:142 break;143 }144 145 return DefWindowProc(hwnd, message, wParam, lParam);146 }
View Code
08 Windows編程——畫圖