// definition of data types and data structures windows.h file contains the desired application #include <windows.h> #include <stdlib.h> #include <time.h> #include " menu_2.h " # the include <math.h> LRESULT CALLBACK the WndProc (the HWND, UINT, WPARAM, LPARAM); // window function description // --------- following initialization window class ----------- --- int WINAPI the WinMain (hInstance the hINSTANCE, the hINSTANCE hPrevInst, the lpszCmdLine LPSTR, int the nCmdShow) { the HWND HWND; the MSG Msg; the WNDCLASS WNDCLASS; char , lpszClassName [] = " window " ; // window class name char lpszTitle [] = " My_Windows " ; // window title name // window class definition wndclass.style the CS_HREDRAW = | CS_VREDRAW; // window type is the default type wndclass.lpfnWndProc = the WndProc; // window handler for the WndProc WNDCLASS. = cbClsExtra 0 ; // window class without extension wndclass.cbWndExtra = 0 ; // window instance no extension wndclass.hInstance = hInstance; // this instance handle wndclass.hIcon = the LoadIcon (NULL, IDI_APPLICATION); // minimized window icon is the default icon = the LoadCursor wndclass.hCursor (NULL, IDC_ARROW); // window using the cursor arrow wndclass.hbrBackground = (HBRUSH) the GetStockObject (WHITE_BRUSH); // window background is white wndclass.lpszMenuName = " Menu2 " ; // window no menu wndclass =, lpszClassName .lpszClassName; // window class named "sample window" // ----------- the register window classes IF (! the RegisterClass (& WNDCLASS)) // if the registration fails issuing warnings Gao sound { the MessageBeep ( 0 ); return FALSE; } // create a window HWND = the CreateWindow ( lpszClassName, // window class name lpszTitle, // title name of the window instance WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, CW_USEDEFAULT, // upper left corner of the window coordinates to the default value CW_USEDEFAULT, CW_USEDEFAULT, // Gao and wide to the default value of the window NULL, // the window no parent window NULL, // the window without the main menu hInstance, // create this window applications currently handles NULL // do not use this value ); the ShowWindow (HWND, the nCmdShow); // display window the UpdateWindow (HWND); // draw a user area the while (the GetMessage (& Msg, NULL, 0 , 0 )) // message loop { TranslateMessage ( & Msg); the DispatchMessage ( & Msg); } return msg.wParam; // program termination information back to the system } // window function LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hDC; HBRUSH hBrush; RECT rect; HPEN hPen; PAINTSTRUCT PtStr; POINT p; static bool isTria = false, isCir = false, isTex = false; COLORREF color[7] = {RGB(255,0,0), RGB(255,165,0), RGB(255,255,0), RGB(0,255,0), RGB(0,255,255), RGB(0,0,255), RGB(139,0,255)}; static HMENU hmenu, haddmenu; switch(message) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDM_ADDMENU: hmenu = GetMenu(hWnd); haddmenu = CreateMenu(); AppendMenu(haddmenu, MF_ENABLED, IDM_QIUHE, "求和"); AppendMenu(haddmenu, MF_ENABLED, IDM_FANGCHA, "方差"); AppendMenu(haddmenu, MF_ENABLED, IDM_PINGJUNZHI, "平均值"); AppendMenu(haddmenu, MF_ENABLED, IDM_JUNFANGGEN, "均方根"); InsertMenu(hmenu, 2, MF_POPUP|MF_BYPOSITION, (UINT)haddmenu, "统计计算(&C)"); EnableMenuItem(hmenu, IDM_ADDMENU, MF_GRAYED); EnableMenuItem(hmenu, IDM_DELMENU, MF_ENABLED); DrawMenuBar(hWnd); return 0; case IDM_DELMENU: DeleteMenu(hmenu, 2, MF_BYPOSITION); EnableMenuItem(hmenu, IDM_ADDMENU, MF_ENABLED); EnableMenuItem(hmenu, IDM_DELMENU, MF_GRAYED); DrawMenuBar(hWnd); return 0; case IDM_EXIT: PostQuitMessage(0); return 0; } return 0; case WM_CREATE: return 0; case WM_PAINT: GetClientRect(hWnd, &rect); hDC = BeginPaint(hWnd, &PtStr); GetClientRect(hWnd,&rect); if (isCir) { hPen = CreatePen(PS_SOLID, 5, color[2]); SelectObject(hDC, hPen); Ellipse(hDC, (rect.right+rect.left)/2-100, (rect.bottom+rect.top)/2-100, (rect.right+rect.left)/2+100, (rect.bottom+rect.top)/2+100); DeleteObject(hPen); isCir = false; } else if (isTria) { hPen = CreatePen(PS_SOLID, 5, color[5]); SelectObject(hDC, hPen); MoveToEx(hDC, (rect.right+rect.left)/2, (rect.bottom+rect.top)/3,NULL); LineTo(hDC, (rect.right+rect.left)*2/3, (rect.bottom+rect.top)*2/3); LineTo(hDC, (rect.right+rect.left)/3, (rect.bottom+rect.top)*2/3); LineTo(hDC,(rect.right+rect.left)/2, (rect.bottom+rect.top)/3); DeleteObject(hPen); isTria = false; } else if (isTex) { SetTextColor(hDC, color[0]); DrawText(hDC, "我爱学VC++", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); isTex = false; } EndPaint(hWnd, &PTSTR); return 0 ; Case the WM_DESTROY: PostQuitMessage ( 0 ); // calls PostQuitMessage WM_QUIT message sent return 0 ; default : return the DefWindowProc (the hWnd, Message, the wParam, the lParam); // use the system default message handler default } }