小海豚学NOIP,老师说要用DEV C++。
小海豚喜欢画图,记得以前用C#编些程序给她看。可前一阵打开看,我的免费Visual Studio过期了。可恶的Microsoft ,不想用盗版难道就要每个月就下载一次?
于是就用DEV C++的Windows调用吧。这里还有一个插曲:我搜DEV C++如何作图,结果找到了CSDN,花4个点下了winbgim,然后才想起直接用Windows调用岂不更正宗些?如图:
这样就建立了名为tstdraw的工程。
1 #include <windows.h> 2 3 LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { 4 static int cxClient, cyClient ; 5 static HPEN hPen1, hPen2, hPen3; 6 HDC hdc ; 7 int i ; 8 PAINTSTRUCT ps ; 9 switch(Message) { 10 case WM_SIZE: { 11 cxClient = LOWORD (lParam) ; 12 cyClient = HIWORD (lParam) ; 13 break; 14 } 15 case WM_PAINT: { 16 InvalidateRect(hwnd, NULL, TRUE); 17 hdc = BeginPaint (hwnd, &ps) ; 18 hPen1 = CreatePen(PS_SOLID, 2, 0x0000ff); 19 hPen2 = CreatePen(PS_SOLID, 4, 0xa0ff00); 20 hPen3 = CreatePen(PS_DOT, 0, 0xff00ff); 21 SelectObject(hdc, hPen1); 22 MoveToEx (hdc, 0, cyClient/2, NULL) ; 23 LineTo (hdc, cxClient, cyClient/2) ; 24 SelectObject(hdc, hPen2); 25 Rectangle(hdc, 300, 200, 70, 80); 26 SelectObject(hdc, hPen3); 27 Ellipse(hdc, cxClient/4, cyClient/3, cxClient/4+cxClient/3, cyClient/3+cxClient/3); 28 SelectObject(hdc, hPen1); 29 RoundRect(hdc, 150, 160, 350, 250, 40, 20); 30 DeleteObject(hPen1); 31 DeleteObject(hPen2); 32 DeleteObject(hPen3); 33 34 break; 35 } 36 case WM_CLOSE: { 37 DestroyWindow(hwnd); 38 break; 39 } 40 case WM_DESTROY: { 41 PostQuitMessage(0); 42 break; 43 } 44 default: 45 return DefWindowProc(hwnd, Message, wParam, lParam); 46 } 47 return 0; 48 } 49 50 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { 51 WNDCLASSEX wc; 52 HWND hwnd; 53 MSG Msg; 54 memset(&wc,0,sizeof(wc)); 55 wc.cbSize = sizeof(WNDCLASSEX); 56 wc.lpfnWndProc = WndProc; 57 wc.hInstance = hInstance; 58 wc.hCursor = LoadCursor(NULL, IDC_ARROW); 59 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 60 wc.lpszClassName = "WindowClass"; 61 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 62 wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 63 64 if(!RegisterClassEx(&wc)) { 65 MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK); 66 return 0; 67 } 68 69 hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Try graphics",WS_VISIBLE|WS_OVERLAPPEDWINDOW, 70 CW_USEDEFAULT, 71 CW_USEDEFAULT, 72 640, 73 480, 74 NULL,NULL,hInstance,NULL); 75 if(hwnd == NULL) { 76 MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK); 77 return 0; 78 } 79 while(GetMessage(&Msg, NULL, 0, 0) > 0) { 80 TranslateMessage(&Msg); 81 DispatchMessage(&Msg); 82 } 83 return Msg.wParam; 84 }
其中只有16-32行的是自己写的,其他都是工程自动生成的。
运行结果如下(拉动了窗口,图形的位置大小根据相对或绝对坐标改变或不变):