>_<:there in the MyPaint(...) function respectively use Ellipse(...) draw ellipse, use RoundRect(...) draw rectangle whose angle is round, use Pie(...) draw sector also named fan shap, use Chord(...) draw arch also named bow shaped.
>_<:the same with previous one, there I don't use message and just put the function of MyPaint(...) in the last of InitInstance(...)
resourse.h
StdAfx.h
main.cpp
1 #include "stdafx.h" 2 #include "resourse.h" 3 4 #define MAX_LOADSTRING 100 5 6 // Global Variables: 7 HINSTANCE hInst; // current instance 8 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text 9 TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text 10 HPEN hPen; 11 HBRUSH hBru[4]; 12 int sBru[7]={HS_VERTICAL,HS_HORIZONTAL,HS_CROSS,HS_DIAGCROSS,HS_FDIAGONAL,HS_BDIAGONAL}; 13 // Foward declarations of functions included in this code module: 14 ATOM MyRegisterClass(HINSTANCE hInstance); 15 BOOL InitInstance(HINSTANCE, int); 16 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 17 LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); 18 void MyPaint(HDC hdc); 19 //======================================================================================== 20 int APIENTRY WinMain(HINSTANCE hInstance, 21 HINSTANCE hPrevInstance, 22 LPSTR lpCmdLine, 23 int nCmdShow) 24 { 25 // TODO: Place code here. 26 MSG msg; 27 28 MyRegisterClass(hInstance);//调用函数向系统注册窗口类别,输入参数hInstance是目前运行程序的对象代码; 29 30 // 调用InitInstance函数,进行初始化操作; 31 if (!InitInstance (hInstance, nCmdShow)) 32 { 33 return FALSE; 34 } 35 36 // 消息循环(通过消息循环来获取信息, 37 //进行必要的键盘信息转换而后将控制权交给操作系统, 38 //有操作系统决定哪个程序的消息处理函数处理消息 39 while (GetMessage(&msg, NULL, 0, 0)) //获取程序消息 40 { 41 TranslateMessage(&msg);//转换伪码及字符 42 DispatchMessage(&msg);//将控制权交给系统,再有系统决定负责处理消息的程序; 43 } 44 45 return msg.wParam; 46 } 47 //===================================================================================== 48 49 50 51 //============================================================================================= 52 //在建立程序窗口实体之前,必须先定义一个窗口类别,其中包含所要建立窗口的信息, 53 //并向系统注册,这里的MyRegisterClass函数就是进行定义及注册窗口类别的函数。 54 //============================================================================================== 55 ATOM MyRegisterClass(HINSTANCE hInstance) 56 { 57 WNDCLASSEX wcex; //申请一个窗口类别“WNDCLASSEX”和结构”wcex“ 58 //-------------------------------------------------------------- 59 //定义vcex结构的各项信息,其中设定信息处理函数(lpfnWndProc) 60 //为WNDPROC,类别名称为(lpszClassName)为”fe"; 61 //-------------------------------------------------------------- 62 wcex.cbSize = sizeof(WNDCLASSEX); 63 64 wcex.style = CS_HREDRAW | CS_VREDRAW; 65 wcex.lpfnWndProc = (WNDPROC)WndProc; 66 wcex.cbClsExtra = 0; 67 wcex.cbWndExtra = 0; 68 wcex.hInstance = hInstance; 69 wcex.hIcon = NULL; 70 wcex.hCursor = NULL; 71 wcex.hCursor = LoadCursor(NULL,IDC_ARROW); 72 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 73 wcex.lpszMenuName = NULL; 74 wcex.lpszClassName = "fe"; 75 wcex.hIconSm = NULL; 76 77 return RegisterClassEx(&wcex);//调用RegisterClassEx函数注册类别,返回一个“ATOM"形态的字符串 78 //此字符串即为类别名称”fe"; 79 } 80 //============================================================================================ 81 82 83 //============================================================================================ 84 //按照前面所定义的窗口类别来建立并显示实际的程序窗口 85 //============================================================================================ 86 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 87 { 88 HWND hWnd; 89 HDC hdc; 90 int i; 91 hInst = hInstance; // 把instance handle 储存在全局变量中; 92 93 hWnd = CreateWindow("fe","绘图窗口",WS_OVERLAPPEDWINDOW, 94 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 95 //----------------------------------------------- 96 //调用CreateWindow函数来建立一个窗口对象 97 //第一个参数就是窗口建立依据的类别名称 98 //----------------------------------------------- 99 if (!hWnd) 100 { 101 return FALSE; 102 } 103 //------------------------------------------------ 104 //设定窗口的位置及窗口的大小,然后绘制显示在设备上 105 //------------------------------------------------- 106 MoveWindow(hWnd,10,10,600,450,true);//位置及大小 107 ShowWindow(hWnd, nCmdShow);//改定窗口显示时的状态 108 UpdateWindow(hWnd);//将窗口绘制在显示设备上 109 110 hPen=CreatePen(PS_SOLID,3,RGB(0,255,0)); 111 for(i=0;i<=3;i++) 112 { 113 hBru[i]=CreateHatchBrush(sBru[i],RGB(234,34,125)); 114 } 115 116 hdc=GetDC(hWnd); 117 MyPaint(hdc); 118 ReleaseDC(hWnd,hdc); 119 120 return TRUE; 121 } 122 //============================================================================================ 123 124 125 //============================================================================================ 126 void MyPaint(HDC hdc) 127 { 128 SelectObject(hdc,hPen); 129 130 SelectObject(hdc,hBru[0]); 131 Ellipse(hdc,20,20,270,150); 132 TextOut(hdc,120,160,"椭圆形",strlen("椭圆形")); 133 134 SelectObject(hdc,hBru[1]); 135 RoundRect(hdc,300,20,550,150,30,30); 136 TextOut(hdc,400,160,"圆角矩形",strlen("圆角矩形")); 137 138 SelectObject(hdc,hBru[2]); 139 Pie(hdc,20,210,270,340,50,50,300,300); 140 TextOut(hdc,120,350,"扇形",strlen("扇形")); 141 142 SelectObject(hdc,hBru[3]); 143 Chord(hdc,300,210,550,340,50,50,600,300); 144 TextOut(hdc,400,350,"弓形",strlen("弓形")); 145 } 146 147 148 //============================================================================================ 149 //在前面定义类别的时候把WndProc定义为消息处理函数(当某些外部消息发生时,会按消息的类型 150 //来决定该如何进行处理。此外该函数也是一个回叫函数(CALLBACK)(windows系统函数)每一个 151 //程序都会接收信息,选择性接受、处理; 152 //============================================================================================ 153 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 154 { 155 PAINTSTRUCT ps; 156 HDC hdc; 157 158 switch (message) //判断消息类型 159 { 160 case WM_PAINT: //窗口重绘制 161 hdc = BeginPaint(hWnd, &ps); 162 EndPaint(hWnd, &ps); 163 break; 164 case WM_DESTROY: //处理窗口结束消息 165 PostQuitMessage(0); 166 break; 167 default: 168 return DefWindowProc(hWnd, message, wParam, lParam); 169 } 170 return 0; 171 } 172 //============================================================================================
分类:
软件_C++游戏开发
本文转自beautifulzzzz博客园博客,原文链接:http://www.cnblogs.com/zjutlitao/p/3732666.html
,如需转载请自行联系原作者