08 Windows编程——画图

2021-03-29 02:24

阅读:644

标签:bsp   rect   sizeof   window   instance   rac   sla   pst   nbsp   

源码

技术分享图片技术分享图片
  1 #include  2 #include  3 #include  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 )
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编程——画图

标签:bsp   rect   sizeof   window   instance   rac   sla   pst   nbsp   

原文地址:https://www.cnblogs.com/kelamoyujuzhen/p/9315159.html


评论


亲,登录后才可以留言!