Windows API窗口程序设计

发布时间 : 星期三 文章Windows API窗口程序设计更新完毕开始阅读

集美大学计算机工程学院实验报告

课程名称:TCP/IP协议分析与编程

实验项目编号:01 一、 实验目的

运用Windows API进行编程 二、实验内容与设计思想

在桌面显示Windows窗口。窗口内居中显示“大家好,这是我的第一个Windows API程序!”同时播放背景音乐,并可通过程序改变窗口显示风格为只有标题栏,以及鼠标指针形状为手型。 三、实验使用环境

操作系统: Microsoft Windows XP SP2 编程环境: Visual C++ 6.0简体中文企业版

四、实验步骤和调试过程(要求:给出源码及实验结果截图)

源码:

#include //定义手型鼠标指针 #ifndef IDC_HAND #define IDC_HAND MAKEINTRESOURCE(32649) #endif LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;//声明WndProc回调函数 //主函数,程序调用的入口 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT (\ HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION) ; wndclass.hCursor= LoadCursor (NULL,IDC_HAND) ; wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH) ; 班级: 组号:1

实验成绩:

上机实践日期:2016-03-24 上机实践时间: 2学时

实验项目名称:Windows API窗口程序设计 学号:

wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass)) { MessageBox (NULL, TEXT (\ szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szAppName, // window class name TEXT (\对话框\ WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT, // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL) ; // creation parameters ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; } //返回长整型的消息处理回调函数 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc ; PAINTSTRUCT ps ; RECT rect ; switch (message) { case WM_CREATE: PlaySound (TEXT (\ return 0 ; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rect); DrawText (hdc,TEXT(\大家好,这是我的第一个Windows API 程序!\ DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ;}

实验结果:

五、实验小结(必写)

1.执行时出现错误

由于程序引用了PlaySound函数,应该在库文件加上相应的库 winmm.lib,在工程—设置—连接—工程选项里加上winmm.lib,并将/subsystem:console改成/subsystem:windows

2.手型在windows7不能直接添加,需要在头加上

#ifndef IDC_HAND

#define IDC_HAND MAKEINTRESOURCE(32649) #endif 七、附录

《网络编程技术与应用》

联系合同范文客服:xxxxx#qq.com(#替换为@)