Windows程序设计教程 期末试卷及答案 (郭暤岩)
期末试卷配套教材:
书名:Windows程序设计教程
作者:郭暤岩 等
出版社:人民邮电出版社
期末试卷概述:
//头文件 #include "windows.h" //定义全局变量和函数 HINSTANCE hInst; HINSTANCE hInstance; MSG msg; char lpszClassName[]="hehe"; char* ShowText; //声明消息响应函数 LRESULT CALLBACK WndProc(HWND hWnd,UINT message, WPARAM wParam,LPARAM lParam); void OnLButtonDown(HWND hWnd,UINT message, WPARAM wParam,LPARAM lParam); void OnPaint(HWND hWnd,UINT message, WPARAM wParam,LPARAM lParam); void OnDestory(HWND hWnd,UINT message, WPARAM wParam,LPARAM lParam); //------------------------------------------------------ //声明窗口类 class CFrameWnd { public: HWND hWnd; public: int RegisterWindow(); void Create(LPCTSTR lpClassName,LPCTSTR lpWindowName); void ShowWindow(int nCmdShow); void UpdateWindow(); }; //类中的函数 //RegisterWindow()设计窗口 int CFrameWnd::RegisterWindow() { WNDCLASS wndclass; wndclass.style=0; wndclass.lpfnWndProc=WndProc; wndclass.cbClsExtra=0; wndclass.cbWndExtra=0; wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndclass.hCursor=LoadCursor(NULL,IDC_ARROW); wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName=NULL; wndclass.lpszClassName=lpszClassName; //注册窗口 return RegisterClass(&wndclass); } //创建窗口 void CFrameWnd::Create(LPCTSTR lpClassName,LPCTSTR lpWindowName) { RegisterWindow(); hInst=hInstance; hWnd=CreateWindow(lpszClassName, lpWindowName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); } //显示窗口 void CFrameWnd::ShowWindow(int nCmdShow) { ::ShowWindow(hWnd,nCmdShow); } //注册窗口 void CFrameWnd::UpdateWindow() { ::UpdateWindow(hWnd); } //-------------------------------------------------------- //声明应用程序类 class CWinApp { public: CFrameWnd* m_pMainWnd; CWinApp* m_pCurrentWnd; public: CWinApp(); ~CWinApp(); virtual BOOL InitInstance(int nCmdShow); int Run(); }; //类中的函数 //构造函数 CWinApp::CWinApp() { m_pCurrentWnd=this; } //初始化函数 BOOL CWinApp::InitInstance(int nCmdShow) { m_pMainWnd=new CFrameWnd; m_pMainWnd->Create(NULL,"Windows程序"); m_pMainWnd->ShowWindow(nCmdShow); m_pMainWnd->UpdateWindow(); return TRUE; } //Run()函数 int CWinApp::Run() { while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } //构造函数 CWinApp::~CWinApp() { delete m_pMainWnd; } //------------------------------------------------------- //声明窗口类CFrameWnd的派生类 class CMyWnd:public CFrameWnd { }; //------------------------------------------------------- //声明应用程序类CWinApp的派生类 class CMyApp:public CWinApp { public: BOOL InitInstance(int nCmdShow); }; //实现类中的函数 CMyApp::InitInstance(int nCmdShow) { CMyWnd* pMainWnd; pMainWnd=new CMyWnd; pMainWnd->Create(NULL,"派生类Windows程序"); pMainWnd->ShowWindow(nCmdShow); pMainWnd->UpdateWindow(); return TRUE; } //------------------------------------------------------- //构造CMyApp类生日 CMyApp myApp; //重写AfxGetApp()函数 CWinApp* AfxGetApp() { return myApp.m_pCurrentWnd; } //------------------------------------------------------- //主函数 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { int rt; /* CWinApp* pApp; pApp=AfxGetApp();*/ CWinApp *pApp = (CWinApp *)AfxGetApp(); pApp->InitInstance(nCmdShow); rt=pApp->Run(); return rt; } //------------------------------------------------------- //窗口函数 LRESULT CALLBACK WndProc(HWND hWnd,UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_LBUTTONDOWN: OnLButtonDown(hWnd,message,wParam,lParam); break; case WM_PAINT: OnPaint(hWnd,message,wParam,lParam); break; case WM_DESTROY: OnDestory(hWnd,message,wParam,lParam); break; default: return DefWindowProc(hWnd,message,wParam,lParam); } return 0; } //-------------------------------------------------------- //消息响应函数 //响应鼠标鼠标单击消息 void OnLButtonDown(HWND hWnd,UINT message, WPARAM wParam,LPARAM lParam) { ShowText="面向对象的封装性"; InvalidateRect(hWnd,NULL,1); } //刷新消息 void OnPaint(HWND hWnd,UINT message, WPARAM wParam,LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; hdc=BeginPaint(hWnd,&ps); TextOut(hdc,150,150,ShowText,16); EndPaint(hWnd,&ps); } //关闭消息 void OnDestory(HWND hWnd,UINT message, WPARAM wParam,LPARAM lParam) { PostQuitMessage(0); }