VisualC++课后习题参考答案VisualC++课后习题参考答案
4-5参考答案:
#include
#include
#include
#include
#define Pi 3.1415926
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明.
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINS...
VisualC++课后习
题
快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题
参考答案
4-5参考答案:
#include
#include
#include
#include
#define Pi 3.1415926
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明.
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);//初始化窗口声明. HWND hWndMain;
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message;
if(!InitWindowsClass(hInstance)) //初始化窗口类.
return FALSE;
if(!InitWindows(hInstance,nCmdShow)) //初始化窗口.
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环.
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam) //消息处理函数. {
HDC hDC; //定义设备环境句柄.
HBRUSH hBrush; //定义画刷句柄
HPEN hPen; //定义画笔句柄
PAINTSTRUCT PtStr; //定义包含绘图信息的结构体变量
double dfRadious0=100.0,dfRadious1; //定义外部和内部正五边形外接圆半径.
POINT lpOuterPoints[5],lpInnerPoints[5];//定义外,内正五边形点结构数组.
POINT lpTriangle[3]; //定义三角形点结构数组.
dfRadious1=dfRadious0*sin(0.1*Pi)/sin(126.0/180*Pi);//根据外圆半径计算内园半径.
//计算内外正五边形的点坐标.
for(int i=0;i<5;i++)
{
lpOuterPoints[i].x=(long)(dfRadious0*cos(i*72.0/180*Pi));
lpOuterPoints[i].y=(long)(dfRadious0*sin(i*72.0/180*Pi));
lpInnerPoints[i].x=(long)(dfRadious1*cos(i*72.0/180*Pi+36.0/180*Pi));
lpInnerPoints[i].y=(long)(dfRadious1*sin(i*72.0/180*Pi+36.0/180*Pi));
}
switch(iMessage)
{
case WM_PAINT: //处理绘图消息.
hDC=BeginPaint(hWnd,&PtStr); //得到设备环境句柄
SetMapMode(hDC,MM_ANISOTROPIC); //设置映射模式.
SetWindowOrgEx(hDC,-100,-100,NULL); //设置坐标原点.
hPen=CreatePen(PS_SOLID,1,RGB(255,0,0)); //创新红色画笔.
SelectObject(hDC,hPen); //将画笔选入.
Polygon(hDC,lpOuterPoints,5); //画正五边形.
//填充正五边形的不同区域.
for(i=0;i<5;i++)
{
lpTriangle[0]=lpOuterPoints[i%5]; //生成图形中的三角形区域的坐标.
lpTriangle[1]=lpInnerPoints[i%5];
lpTriangle[2]=lpOuterPoints[(i+1)%5];
//创新新画刷.
hBrush=CreateSolidBrush(RGB(i*10,i*20,i*30));
SelectObject(hDC,hBrush); //选入新画刷.
Polygon(hDC,lpTriangle,3); //画三角形区域.
lpTriangle[2]=lpInnerPoints[(i+4)%5]; //生成图形中的三角形区域的坐标.
hBrush=CreateSolidBrush(RGB(i*40,i*30,i*20));//创新画刷.
SelectObject(hDC,hBrush); //选入画刷.
Polygon(hDC,lpTriangle,3); //画三角形区域.
}
hBrush=CreateSolidBrush(RGB(255,255,255));//创新白画刷.
SelectObject(hDC,hBrush); //选入画刷.
Polygon(hDC,lpInnerPoints,5); //画中心的五边形.
//用不同种颜色的画笔来绘制五角星.
MoveToEx(hDC,lpOuterPoints[0].x,lpOuterPoints[0].y,NULL);
for(i=1;i<=5;i++)
{
hPen=CreatePen(PS_SOLID,1,RGB(0,i*51,0));
SelectObject(hDC,hPen);
LineTo(hDC,lpOuterPoints[(i+2)%5].x,lpOuterPoints[(i+2)%5].y);
}
hPen=(HPEN)(GetStockObject(PS_NULL));
DeleteObject(hPen); //删除画笔.
DeleteObject(hBrush); //删除画刷.
EndPaint(hWnd,&PtStr); //结束绘图.
return 0;
case WM_DESTROY: //处理关闭窗口信息.
PostQuitMessage(0); //向应用程序发送WM_QUIT消息.
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow) //初始化 {
HWND hWnd; //定义窗口句柄.
hWnd=CreateWindow("Polygon", //窗口类名.
"五边形", //窗口实例标题.
WS_OVERLAPPEDWINDOW, //窗口风格.带边框,标题栏,系统菜单和最大和最小按钮.
CW_USEDEFAULT, //窗口左上角坐标.
0, //窗口左上角坐标.
CW_USEDEFAULT, //窗口宽度
0, //窗口高度
NULL, //无父窗口.
NULL, //无主菜单.
hInstance, //创建此窗口的应用程序的当前句柄.
NULL); //指向一个传递给窗口的参数值从指针.
//不使用该值.
if(!hWnd) //如果窗口创建失败,返回false.
return FALSE;
hWndMain=hWnd; //将窗口句柄传递给全局变量.
ShowWindow(hWnd,nCmdShow); //显示窗口.
UpdateWindow(hWnd); //更新并绘制用户区.
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance) //初始化窗口类. {
WNDCLASS WndClass; //定义窗口类.
WndClass.cbClsExtra=0; //窗口类无扩展.
WndClass.cbWndExtra=0; //窗口实例无扩展.
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));//窗口背景为白色.
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //窗口采用箭头光标.
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //窗口的最小化图标为缺省图标.
WndClass.hInstance=hInstance; //当前实例句柄.
WndClass.lpfnWndProc=WndProc; //窗口处理函数.
WndClass.lpszClassName="Polygon"; //窗口类名.
WndClass.lpszMenuName=NULL; //无菜单
WndClass.style=0; //窗口类型为缺省类型.
return RegisterClass(&WndClass); //返回注册窗口的值. }
4-6参考答案:
#include
#include
#include
#include
#define Pi 3.1415926
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明.
double dfTheta=0,dfRange=100.0; //正弦曲线的角度变量. long i=0,j=0;
long lCentreX=0,lCentreY=0,lRadious=(long)(0.2*dfRange);
//定义圆心坐标和圆坐标.
POINT lpSin[100]; //定义正弦曲线的点坐标.
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message; //定义消息变量.
HWND hWnd; //定义窗口句柄.
WNDCLASS WndClass; //定义窗口类.
WndClass.cbClsExtra=0; //无窗口类扩展.
WndClass.cbWndExtra=0; //无窗口实例扩展.
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));//背景为白色.
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //窗口采用箭头光标.
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //采用缺省图标.
WndClass.hInstance=hInstance; //当前应用程序句柄.
WndClass.lpfnWndProc=WndProc; //窗口处理函数.
WndClass.lpszClassName="SIN"; //窗口类名称.
WndClass.lpszMenuName=NULL; //无窗口菜单.
WndClass.style=0; //窗口类型为缺省类型.
if(!RegisterClass(&WndClass)) //注册窗口.
{
MessageBeep(0);
return FALSE;
}
hWnd=CreateWindow("SIN", //窗口类名.
"4_6", //标题名.
WS_OVERLAPPEDWINDOW, //带标题栏,最大和最小按钮的窗口.
CW_USEDEFAULT, //窗口左上角坐标.
0,
CW_USEDEFAULT, //采用缺省的宽度和高度.
0,
NULL, //无父窗口.
NULL, //无主菜单.
hInstance, //当前实例句柄.
NULL); //不要此参数.
ShowWindow(hWnd,nCmdShow); //显示窗口.
UpdateWindow(hWnd); //更新并绘制用户区.
for(int j=0;j<100;j++) //生成正弦曲线的点坐标.
{
lpSin[j].x=(long)(j*2*Pi/100*60);
lpSin[j].y=(long)(dfRange*sin(j*2*Pi/100));
}
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环.
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam) //消息处理函数. {
HDC hDC; //定义设备环境句柄.
HBRUSH hBrush; //定义画刷句柄
HPEN hPen; //定义画笔句柄
PAINTSTRUCT PtStr; //定义包含绘图信息的结构体变量
switch(iMessage)
{
case WM_PAINT: //处理绘图消息.
hDC=BeginPaint(hWnd,&PtStr); //获得设备环境指针.
SetWindowOrgEx(hDC,-200,-200,NULL); //设置原点坐标为(-200,-200).
hPen=CreatePen(PS_DASH,1,RGB(255,0,0)); //创新画笔.
SelectObject(hDC,hPen); //选入画笔.
Polyline(hDC,lpSin,100); //绘制正弦曲线.
if(i<=25) //第一个1/4周期.
{
hPen=CreatePen(PS_DASH,1,RGB(255,0,0));//创新红笔.
hBrush=CreateHatchBrush(HS_BDIAGONAL,RGB(255,0,0));//创新红画刷.
lRadious=(long)(dfRange*0.2+i%25*dfRange*0.4/25);//计算半径.
}
else if(i<=50)//第二个1/4周期.
{
hPen=CreatePen(PS_DASH,1,RGB(0,255,0));//创新绿笔.
hBrush=CreateHatchBrush(HS_DIAGCROSS,RGB(0,255,0));//创新绿刷.
lRadious=(long)(dfRange*0.2+i%25*dfRange*0.4/25);//计算半径.
}
else if(i<=75)//第三个周期.
{
hPen=CreatePen(PS_DASH,1,RGB(0,0,255));//创新蓝笔
hBrush=CreateHatchBrush(HS_CROSS,RGB(0,0,255));//创新蓝刷.
lRadious=(long)(dfRange*0.2+i%25*dfRange*0.4/25);//计算半径.
}
else//第四个周期.
{
hPen=CreatePen(PS_DASH,1,RGB(255,255,0));//创新黄笔
hBrush=CreateHatchBrush(HS_VERTICAL,RGB(255,255,0));//创新黄刷.
lRadious=(long)(dfRange*0.2+i%25*dfRange*0.4/25);//计算半径.
}
SelectObject(hDC,hBrush); //选入画刷.
SelectObject(hDC,hPen); //选入画笔.
lCentreX=lpSin[i].x; //圆心x坐标.
lCentreY=lpSin[i].y; //圆心y坐标.
Ellipse(hDC,lCentreX-lRadious,lCentreY-lRadious,
lCentreX+lRadious,lCentreY+lRadious); //画圆.
i++;
DeleteObject(hPen); //删除画笔.
DeleteObject(hBrush); //删除画刷.
EndPaint(hWnd,&PtStr); //删除设备环境指针.
Sleep(100); //停0.1秒.
if(i<100) InvalidateRect(hWnd,NULL,1); //刷新用户区.
return 0;
case WM_DESTROY: //关闭窗口.
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
}
4-7参考答案:
#include
#include
#include
#include
#define Pi 3.1415926
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明.
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message; //定义消息变量.
HWND hWnd; //定义窗口句柄.
WNDCLASS WndClass; //定义窗口类.
WndClass.cbClsExtra=0; //无窗口类扩展.
WndClass.cbWndExtra=0; //无窗口实例扩展.
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));//背景为白色.
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //窗口采用箭头光标.
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //采用缺省图标.
WndClass.hInstance=hInstance; //当前应用程序句柄.
WndClass.lpfnWndProc=WndProc; //窗口处理函数.
WndClass.lpszClassName="4_7"; //窗口类名称.
WndClass.lpszMenuName=NULL; //无窗口菜单.
WndClass.style=0; //窗口类型为缺省类型.
if(!RegisterClass(&WndClass)) //注册窗口.
{
MessageBeep(0);
return FALSE;
}
hWnd=CreateWindow("4_7", //窗口类名.
"4_7", //标题名.
WS_OVERLAPPEDWINDOW, //带标题栏,最大和最小按钮的窗口.
CW_USEDEFAULT, //窗口左上角坐标.
0,
600, //采用宽度为450,高度为600.
450,
NULL, //无父窗口.
NULL, //无主菜单.
hInstance, //当前实例句柄.
NULL); //不要此参数.
ShowWindow(hWnd,nCmdShow); //显示窗口.
UpdateWindow(hWnd); //更新并绘制用户区.
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环.
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam) //消息处理函数. {
HDC hDC; //定义设备环境句柄.
HBRUSH hBrush; //定义画刷句柄
HPEN hPen; //定义画笔句柄
PAINTSTRUCT PtStr; //定义包含绘图信息的结构体变量
switch(iMessage)
{
case WM_PAINT: //处理绘图消息.
hDC=BeginPaint(hWnd,&PtStr); //获得设备环境指针.
SetMapMode(hDC,MM_ANISOTROPIC); //设置映射模式.
SetWindowExtEx(hDC,300,300,NULL); //设置窗口区域.逻辑单位.
SetViewportExtEx(hDC,600,600,NULL); //设置视口区域.物理单位.
SetViewportOrgEx(hDC,0,0,NULL); //设置视口原点坐标为(0,0).物理单位.
//绘制四个圆形.(笔的样式不同)
hBrush = (HBRUSH)GetStockObject(NULL_BRUSH); //选择空画刷.
SelectObject(hDC,hBrush);
hPen = CreatePen(PS_DASH,0,RGB(255,0,0));//建立虚线样式的红色笔
SelectObject(hDC,hPen); //选入设备环境.
Ellipse(hDC,0,0,70,70); //画圆.
hPen = CreatePen(PS_DASHDOT,0,RGB(0,255,0));//建立点划线样式的绿色笔
SelectObject(hDC,hPen);
Ellipse(hDC,25,0,95,70); //画圆.
hPen = CreatePen(PS_DASHDOTDOT,0,RGB(0,0,255));//建立双点划线样式的蓝色笔
SelectObject(hDC,hPen);
Ellipse(hDC,0,25,70,95);
hPen = CreatePen(PS_DOT,0,RGB(0,255,255));//建立点线样式的天蓝色笔
SelectObject(hDC,hPen);
Ellipse(hDC,25,25,95,95);
//画6个圆角矩形.(实画刷样式不同.)
hPen = (HPEN)GetStockObject(BLACK_PEN);
SelectObject(hDC,hPen);
hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH); //选择黑画刷.
SelectObject(hDC,hBrush); //选入设备环境.
RoundRect(hDC,120,0,165,45,10,10); //画圆角矩形.
hBrush = (HBRUSH)GetStockObject(DKGRAY_BRUSH); //选择深灰画刷.
SelectObject(hDC,hBrush);
RoundRect(hDC,165,0,210,45,10,10);
hBrush = (HBRUSH)GetStockObject(GRAY_BRUSH); //选择灰画刷.
SelectObject(hDC,hBrush);
RoundRect(hDC,210,0,255,45,10,10);
hBrush = (HBRUSH)GetStockObject(HOLLOW_BRUSH); //选择虚画刷.
SelectObject(hDC,hBrush);
RoundRect(hDC,120,45,165,90,10,10);
hBrush = (HBRUSH)GetStockObject(LTGRAY_BRUSH); //选择亮灰画刷.
SelectObject(hDC,hBrush);
RoundRect(hDC,165,45,210,90,10,10);
hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH); //选择白画刷.
SelectObject(hDC,hBrush);
RoundRect(hDC,210,45,255,90,10,10);
//画6个矩形.(画刷阴影样式不同.)
hPen = (HPEN)GetStockObject(BLACK_PEN);
SelectObject(hDC,hPen);
hBrush = CreateHatchBrush(HS_BDIAGONAL,RGB(200,200,200));//创建45度左上右下阴影线
SelectObject(hDC,hBrush); //选入设备环境.
Rectangle(hDC,120,90,165,135); //画矩形.
hBrush = CreateHatchBrush(HS_DIAGCROSS,RGB(200,200,200));//创建45度交叉线
SelectObject(hDC,hBrush);
Rectangle(hDC,165,90,210,135);
hBrush = CreateHatchBrush(HS_FDIAGONAL,RGB(200,200,200));//创建45度左下右上阴影线.
SelectObject(hDC,hBrush);
Rectangle(hDC,210,90,255,135);
hBrush = CreateHatchBrush(HS_CROSS,RGB(200,200,200));//创建垂直相交阴影线.
SelectObject(hDC,hBrush);
Rectangle(hDC,120,135,165,175);
hBrush = CreateHatchBrush(HS_HORIZONTAL,RGB(200,200,200));//创建水平阴影线.
SelectObject(hDC,hBrush);
Rectangle(hDC,165,135,210,175);
hBrush = CreateHatchBrush(HS_VERTICAL,RGB(200,200,200));//创建垂直阴影线.
SelectObject(hDC,hBrush);
Rectangle(hDC,210,135,255,175);
//画饼图。
hPen = (HPEN)GetStockObject(BLACK_PEN);
SelectObject(hDC,hPen);
hBrush = CreateSolidBrush(RGB(255,0,0));//创建红色画刷。
SelectObject(hDC,hBrush); //选入设备环境。
Pie(hDC,10,100, //画饼图。
100,190,
(int)(55+45*cos(2*Pi/3)),(int)(145+45*sin(2*Pi/3)),
(int)(55+45*cos(0)),(int)(145+45*sin(0)));
hBrush = CreateSolidBrush(RGB(0,255,255));//创建天蓝色画刷。
SelectObject(hDC,hBrush); //选入设备环境。
Pie(hDC,10,100,
100,190,
(int)(55+45*cos(4*Pi/3)),(int)(145+45*sin(4*Pi/3)),
(int)(55+45*cos(2*Pi/3)),(int)(145+45*sin(2*Pi/3)));
hBrush = CreateSolidBrush(RGB(255,255,0));//创建黄色画刷。
SelectObject(hDC,hBrush); //选入设备环境。
Pie(hDC,10,100,
100,190,
(int)(55+45*cos(2*Pi)),(int)(145+45*sin(2*Pi)),
(int)(55+45*cos(4*Pi/3)),(int)(145+45*sin(4*Pi/3)));
EndPaint(hWnd,&PtStr); //释放环境指针。
return 0;
case WM_DESTROY: //关闭窗口.
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
}
4-8参考答案:
#include
#include
#include
#include
#define Pi 3.1415926
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明. int nNum = 0,nMaxNum = 20; //nMaxNum记录了叶片循环一周中绘图的次数.
//nNum记录了当前的序数.
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message; //定义消息变量.
HWND hWnd; //定义窗口句柄.
WNDCLASS WndClass; //定义窗口类.
WndClass.cbClsExtra=0; //无窗口类扩展.
WndClass.cbWndExtra=0; //无窗口实例扩展.
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));//背景为白色.
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //窗口采用箭头光标.
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //采用缺省图标.
WndClass.hInstance=hInstance; //当前应用程序句柄.
WndClass.lpfnWndProc=WndProc; //窗口处理函数.
WndClass.lpszClassName="4_8"; //窗口类名称.
WndClass.lpszMenuName=NULL; //无窗口菜单.
WndClass.style=0; //窗口类型为缺省类型.
if(!RegisterClass(&WndClass)) //注册窗口.
{
MessageBeep(0);
return FALSE;
}
hWnd=CreateWindow("4_8", //窗口类名.
"4_8", //标题名.
WS_OVERLAPPEDWINDOW, //带标题栏,最大和最小按钮的窗口.
CW_USEDEFAULT, //窗口左上角坐标.
0,
600, //采用宽度为450,高度为600.
450,
NULL, //无父窗口.
NULL, //无主菜单.
hInstance, //当前实例句柄.
NULL); //不要此参数.
ShowWindow(hWnd,nCmdShow); //显示窗口.
UpdateWindow(hWnd); //更新并绘制用户区.
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环.
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam) //消息处理函数. {
HDC hDC; //定义设备环境句柄.
HBRUSH hBrush; //定义画刷句柄
HPEN hPen; //定义画笔句柄
PAINTSTRUCT PtStr; //定义包含绘图信息的结构体变量
int nCentreX,nCentreY; //定义3个叶片的圆心的坐标.
double fAngle;
switch(iMessage)
{
case WM_PAINT: //处理绘图消息.
hDC=BeginPaint(hWnd,&PtStr); //获得设备环境指针.
SetMapMode(hDC,MM_ANISOTROPIC); //设置映射模式.
SetWindowExtEx(hDC,400,300,NULL); //设置窗口区域.逻辑单位.
SetViewportExtEx(hDC,600,450,NULL); //设置视口区域.物理单位.
SetViewportOrgEx(hDC,300,200,NULL); //设置视口原点坐标为(300,200).物理单位.
//绘制外圆。
hPen = (HPEN)GetStockObject(BLACK_PEN);
SelectObject(hDC,hPen);
Ellipse(hDC,-100,-100,100,100);
//绘制风车的叶片。
hBrush = CreateSolidBrush(RGB(255,0,0)); //画红色的叶片.
SelectObject(hDC,hBrush);
fAngle = 2*Pi/nMaxNum*nNum;
nCentreX = (int)(50*cos(fAngle));
nCentreY = (int)(50*sin(fAngle));
Pie(hDC,nCentreX-50,nCentreY-50,
nCentreX+50,nCentreY+50,
(int)(nCentreX+50*cos(fAngle)),(int)(nCentreY+50*sin(fAngle)),
(int)(nCentreX+50*cos(fAngle+Pi)),(int)(nCentreY+50*sin(fAngle+Pi)));
hBrush = CreateSolidBrush(RGB(255,255,0)); //画天蓝色的叶片.
SelectObject(hDC,hBrush);
nCentreX = (int)(50*cos(fAngle+2*Pi/3));
nCentreY = (int)(50*sin(fAngle+2*Pi/3));
Pie(hDC,nCentreX-50,nCentreY-50,
nCentreX+50,nCentreY+50,
(int)(nCentreX+50*cos(fAngle+2*Pi/3)),(int)(nCentreY+50*sin(fAngle+2*Pi/3)),
(int)(nCentreX+50*cos(fAngle+Pi+2*Pi/3)),(int)(nCentreY+50*sin(fAngle+Pi+2*Pi/3)));
hBrush = CreateSolidBrush(RGB(0,255,255)); //画黄色的叶片.
SelectObject(hDC,hBrush);
nCentreX = (int)(50*cos(fAngle+4*Pi/3));
nCentreY = (int)(50*sin(fAngle+4*Pi/3));
Pie(hDC,nCentreX-50,nCentreY-50,
nCentreX+50,nCentreY+50,
(int)(nCentreX+50*cos(fAngle+4*Pi/3)),(int)(nCentreY+50*sin(fAngle+4*Pi/3)),
(int)(nCentreX+50*cos(fAngle+Pi+4*Pi/3)),(int)(nCentreY+50*sin(fAngle+Pi+4*Pi/3)));
nNum++; //当前序数加1.
Sleep(100); //等待0.1秒.
InvalidateRect(hWnd,NULL,1); //重绘窗口区域.
EndPaint(hWnd,&PtStr); //释放环境指针。
return 0;
case WM_DESTROY: //关闭窗口.
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
}
4-9参考答案:
#include
#define ID_TIMER 1
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
static char szAppName[] = "Timer1" ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
if (!hPrevInstance)
{
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_ARROW) ;
wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
RegisterClass (&wndclass) ;
}
hwnd = CreateWindow (szAppName, "Timer1 Demo Program",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
while (!SetTimer (hwnd, ID_TIMER, 1000, NULL)) //产生一个特殊的时间值/
if (IDCANCEL == MessageBox (hwnd,
"Too many clocks or timers!", szAppName,
MB_ICONEXCLAMATION | MB_RETRYCANCEL))
//本题中, MB_ICONEXCLAMATION
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
示对话框图标为惊叹号,
//MB_RETRYCANCEL表示对话框包含RETRY和CANCEL两个按钮
return FALSE ;
ShowWindow (hwnd, nCmdShow) ;
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)
{
static int colors = 0 ;
HBRUSH hBrush ;
HDC hdc ;
PAINTSTRUCT ps ;
RECT rc ;
switch (message)
{
case WM_TIMER:
MessageBeep (-1) ; //产生一个声音
InvalidateRect (hwnd, NULL, FALSE) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rc) ;
switch (colors)
{
case 0:
hBrush = CreateSolidBrush (RGB(255,0,0)) ;//得到某种颜色的逻辑刷
colors +=1;
break;
case 1:
hBrush = CreateSolidBrush (RGB(0,255,0)) ;
colors +=1;
break;
case 2:
hBrush = CreateSolidBrush (RGB(0,0,255)) ;
colors =0;
}
FillRect (hdc, &rc, hBrush) ;//用指定的格式刷填充矩形区
//包括矩形区的左边和顶部,但不包括右边和底部
EndPaint (hwnd, &ps) ;
DeleteObject (hBrush) ;
return 0 ;
case WM_DESTROY:
KillTimer (hwnd, ID_TIMER) ;//释放ID_TIMER指定的计时器
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
4-10参考答案:
/*-------------------------------------------------------------
MAPMODE.C -- What's the Size of Window in differend mapmode?
------------------------------------------------------------*/
#include
#include
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
static char szAppName[] = "MapMode" ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
if (!hPrevInstance)
{
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_ARROW) ;
wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
RegisterClass (&wndclass) ;
}
hwnd = CreateWindow (szAppName, "What Size is the Window?",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, nCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
void Show (HWND hwnd, HDC hdc, short xText, short yText, short nMapMode,
char *szMapMode)
{
char szBuffer [60] ;
RECT rect ;
SaveDC (hdc) ;//保存保存设备上下文
SetMapMode (hdc, nMapMode) ;//为设备上下文设置新的图纸单位,并且设置好X-,Y-轴
方向
GetClientRect (hwnd, &rect) ;
DPtoLP (hdc, (LPPOINT) &rect, 2) ;
RestoreDC (hdc, -1) ;//重新保存设备上下文
TextOut (hdc, xText, yText, szBuffer,
sprintf (szBuffer, "%-20s %7d %7d %7d %7d", szMapMode,
rect.left, rect.right, rect.top, rect.bottom)) ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
static char szHeading [] =
"Mapping Mode Left Right Top Bottom" ;
static char szUndLine [] =
"------------ ---- ----- --- ------" ;
static long cxChar, cyChar ;
HDC hdc ;
PAINTSTRUCT ps ;
TEXTMETRIC tm ;
switch (message)
{
case WM_CREATE:
hdc = GetDC (hwnd) ;
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight + tm.tmExternalLeading ;
ReleaseDC (hwnd, hdc) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
SetMapMode (hdc, MM_ANISOTROPIC) ;
SetWindowExtEx (hdc, 1, 1,NULL) ;//用指定设置窗口的水平和竖直长度
SetViewportExtEx (hdc, cxChar, cyChar,NULL) ;
TextOut (hdc, 1, 1, szHeading, sizeof szHeading - 1) ;
TextOut (hdc, 1, 2, szUndLine, sizeof szUndLine - 1) ;
Show (hwnd, hdc, 1, 3, MM_TEXT, "TEXT (pixels)") ;
Show (hwnd, hdc, 1, 4, MM_LOMETRIC, "LOMETRIC (.1 mm)") ;
Show (hwnd, hdc, 1, 5, MM_HIMETRIC, "HIMETRIC (.01 mm)") ;
Show (hwnd, hdc, 1, 6, MM_LOENGLISH, "LOENGLISH (.01 in)") ;
Show (hwnd, hdc, 1, 7, MM_HIENGLISH, "HIENGLISH (.001 in)") ;
Show (hwnd, hdc, 1, 8, MM_TWIPS, "TWIPS (1/1440 in)") ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
4-11参考答案:
#include
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;//Wndproc是窗口过
程函数名,LRESULT说明是32位数据类型 CALLBACK说明是反调函数
int WINAPI WinMain (
HINSTANCE hInstance,//当前实例句柄
HINSTANCE hPrevInstance,//前一个实例句柄
LPSTR lpszCmdParam, //指向字符串的指针,这里指向存放内存运行程序
名的缓冲区
int nCmdShow)//告诉应用程序如何显示初始化窗口,一般为
SW—SHOW—NOMAL
{
static char szAppName[] = "4_11" ;//赋值字符串,在定义窗口特征中你会看到他
HWND hwnd ;//窗口句柄
MSG msg ;//消息结构类
WNDCLASS wndclass ;//窗口类
if (!hPrevInstance)//定义窗口特征
{
wndclass.style = CS_HREDRAW | CS_VREDRAW ;//定义窗口类型为整个
窗口重画
wndclass.lpfnWndProc = WndProc ;//指向当前运行窗口,以处理不同窗口信息
wndclass.cbClsExtra = 0 ;//不留额外类结构空间
wndclass.cbWndExtra = 0 ;//不留额外窗口结构空间
wndclass.hInstance = hInstance ;//设置winmain的第一个参数
wndclass.hIcon = NULL;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;//定义窗口光标为
IDC_ARROW标记的光标,即箭头
wndclass.hbrBackground = (HBRUSH)GetStockObject (LTGRAY_BRUSH) ;//指定窗
口客户区的背景为浅灰色
wndclass.lpszMenuName = szAppName ;//这个应用程序的菜单名
wndclass.lpszClassName = szAppName ;//窗口类的名字,一般应和程序名字一样
RegisterClass (&wndclass) ;//这个函数用来注册窗口类
}
hwnd = CreateWindow (//该函数创建一个窗口实例
szAppName, // window class name
"ROP2 demonstration program", // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position常量
CW_USEDEFAULT可使Windows 为窗口选择适当的大小和位置
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, nCmdShow) ;//把窗口放到显示器上,hwnd标志要显示的窗口句柄, nCmdShow标志显示方式
UpdateWindow (hwnd) ;//显示窗口,发送WM—PAINT消息
while (GetMessage (&msg, NULL, 0, 0))//取得消息,除受到WM-QUIT外,任何消息都返回TRUE
{
TranslateMessage (&msg) ;//对一些键盘消息进行翻译
DispatchMessage (&msg) ;//
要求
对教师党员的评价套管和固井爆破片与爆破装置仓库管理基本要求三甲医院都需要复审吗
WINDOWS将消息传送给 在MSG结构中为窗口所指定的消息处理器
}
return msg.wParam ;//返回一个16位消息参数
}
LRESULT CALLBACK WndProc (//Wndproc是窗口过程函数名,LRESULT说明是32位数据类型 CALLBACK说明是反调函数
HWND hwnd, //窗口句柄
UINT message, //调用的消息
WPARAM wParam,//第一个消息,16位
LPARAM lParam)//第二个消息,32位
{
static LOGPEN lpBlack={PS_SOLID,1,1,RGB(0,0,0)},//定义逻辑笔类型
lpWhite={PS_SOLID,1,1,RGB(255,255,255)};
static short nDrawingMode=R2_COPYPEN;
HDC hdc ;//设备句柄
HMENU hMenu;//菜单句柄
HPEN hPenBlack,hPenWhite,hDefPen;
PAINTSTRUCT ps ;//画刷结构
RECT rect ;//无效区
short i;
switch (message)//消息处理函数
{
case WM_COMMAND:
hMenu=GetMenu(hwnd);//从窗口取得菜单句柄
CheckMenuItem(hMenu,nDrawingMode,MF_UNCHECKED);//检查菜单项
nDrawingMode=LOWORD(wParam);
CheckMenuItem(hMenu,nDrawingMode,MF_CHECKED);
InvalidateRect(hwnd,NULL,FALSE);
return 0;
case WM_PAINT://绘制消息
hdc = BeginPaint (hwnd, &ps) ; //得到设备上下文,这个函数仅用于WM-PAINT消息处理中
hPenBlack=CreatePenIndirect(&lpBlack);//设置黑色笔
hPenWhite=CreatePenIndirect(&lpWhite);//设置白色笔
SetMapMode(hdc,MM_ANISOTROPIC);//设置图纸模式
GetClientRect (hwnd, &rect) ;//得到需要绘制的客户区
SetViewportExtEx(hdc,rect.right,rect.bottom,NULL);
SetWindowExtEx(hdc,10,4,NULL);
for(i=0;i<10;i+=2)
{
SetRect(&rect,i,0,i+2,4);
FillRect(hdc,&rect,(HBRUSH)GetStockObject(i/2));
}
SetROP2(hdc,nDrawingMode);
hDefPen=(HPEN)SelectObject(hdc,hPenWhite);
MoveToEx(hdc,1,1,NULL);//在当前点和前一个点连一根线,返回前一个点的坐标
LineTo(hdc,9,3);
EndPaint(hwnd,&ps);
SelectObject(hdc,hDefPen);
DeleteObject(hPenBlack);
DeleteObject(hPenWhite);
return 0 ;
case WM_DESTROY://用户电击了X按钮等消息
PostQuitMessage (0) ;//告诉系统退出程序
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;//调用系统默认函数处理程序没有处理的消息
}
5-5参考答案:
#include
#include
#include
long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam);
//消息处理函数声明.
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);//初始化窗口声明. int CALLBACK pEnumFontFamProc(
ENUMLOGFONT FAR *lpelf, // pointer to logical-font data
NEWTEXTMETRIC FAR *lpntm, // pointer to physical-font data
int FontType, // type of font
LPARAM lParam // pointer to application-defined data
);
HWND hWndMain; //定义窗口句柄.
int iFontSign=0; //定义字体标志,表示采用何种字体.
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message;
if(!InitWindowsClass(hInstance)) //初始化窗口类.
return FALSE;
if(!InitWindows(hInstance,nCmdShow)) //初始化窗口.
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环.
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam) //
消息处理函数.
{
HDC hDC; //定义设备环境句柄.
HFONT hF; //定义字体句柄.
PAINTSTRUCT ps; //定义包含绘图信息的结构体变量
TEXTMETRIC tm; //定义包含字体信息的结构体变量.
static char lpsz_1[]="只有付出才有收获 "; //定义输出的字符串.
char chFont[20]; //定义字体种类的字符串.
int X=0,Y=0,i;
static int nCharlen=strlen(lpsz_1); //定义字符串长度变量.
switch(iMessage)
{
case WM_PAINT: //处理绘图消息.
hDC=BeginPaint(hWnd,&ps); //获得设备环境指针.
for(i=0;i
#include
#include
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明.
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);//初始化窗口声明. HWND hWndMain; //定义窗口句柄.
int iFontFlag=0; //定义字体标志.
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message;
if(!InitWindowsClass(hInstance)) //初始化窗口类.
return FALSE;
if(!InitWindows(hInstance,nCmdShow)) //初始化窗口.
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环.
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam) //消息处理函数. {
HDC hDC; //定义设备环境句柄.
HFONT hF; //定义字体句柄.
PAINTSTRUCT ps; //定义包含绘图信息的结构体变量
TEXTMETRIC tm; //定义包含字体信息的结构体变量.
char lpsz_1[]="欲穷千里目 更上一层楼"; //定义输出字符串.
char chFont[7]; //定义包含字体的字符串.
int X=0,Y=0,nCharlen=strlen(lpsz_1);
switch(iMessage)
{
case WM_CREATE:
SetTimer(hWnd,1,200,NULL); //设定定时器.每0.2秒发出WM_TIMER消息.
break;
case WM_PAINT: //处理绘图消息.
hDC=BeginPaint(hWnd,&ps); //获取设备环境指针.
if(iFontFlag/nCharlen==0) //设置字体颜色.
{
SetTextColor(hDC,RGB(255,0,0));
strcpy(chFont,"楷体");
}
else if(iFontFlag/nCharlen==1)
{
SetTextColor(hDC,RGB(0,255,0));
strcpy(chFont,"宋体");
}
else if(iFontFlag/nCharlen==2)
{
SetTextColor(hDC,RGB(255,255,0));
strcpy(chFont,"仿宋体");
}
else if(iFontFlag/nCharlen==3)
{
SetTextColor(hDC,RGB(0,0,255));
strcpy(chFont,"黑体");
}
else
{
iFontFlag=0;
SetTextColor(hDC,RGB(255,0,0));
}
//设置字体.
hF=CreateFont( //获得字体句柄.
40, //字体高度
0, //系统自动调整宽度.
0, //文本水平
0, //字体倾斜度为0
400, //字体粗度.400为正常.
0, //字体不倾斜.
0, //无下划线.
0, //无中划线.
GB2312_CHARSET, //字符集
OUT_DEFAULT_PRECIS, //默认输出精度.
CLIP_DEFAULT_PRECIS,//默认裁剪精度
DEFAULT_QUALITY, //默认输出质量.
DEFAULT_PITCH|FF_DONTCARE,//默认间距
chFont); //字体名称.
SelectObject(hDC,hF); //选入字体.
GetTextMetrics(hDC,&tm); //得到字体的信息.
TextOut(hDC,X,Y,&lpsz_1[iFontFlag%nCharlen],nCharlen-iFontFlag%nCharlen);
//输出.
iFontFlag+=2; //字体标志自增.
DeleteObject(hF); //删除字体.
EndPaint(hWnd,&ps); //删除设备环境指针.
return 0;
case WM_DESTROY: //结束.
PostQuitMessage(0);
KillTimer(hWnd,1); //删除定时器.
return 0;
case WM_TIMER:
InvalidateRect(hWnd,NULL,1); //刷新用户区.
break;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
return 0;
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow("5_6", //窗口类名称.
"字体显示实例程序", //标题栏名称.
WS_OVERLAPPEDWINDOW, //窗口样式.有标题栏和最大最小化按钮.
CW_USEDEFAULT, //窗口左上角坐标.
0,
CW_USEDEFAULT, //采用缺省的高度和宽度.
0,
NULL, //无父窗口.
NULL, //无菜单.
hInstance, //当前应用实例句柄.
NULL); //指向传递给窗口的参数的指针.不用.
if(!hWnd) //创建失败.
return FALSE;
hWndMain=hWnd; //给全局窗口句柄付值.
ShowWindow(hWnd,nCmdShow); //显示窗口.
UpdateWindow(hWnd); //刷新用户区.
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS WndClass;
WndClass.cbClsExtra=0; //无窗口类扩展.
WndClass.cbWndExtra=0; //无窗口实例扩展.
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));//背景为白色.
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //光标为为箭头.
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //采用缺省图标.
WndClass.hInstance=hInstance; //当前实例.
WndClass.lpfnWndProc=WndProc; //消息处理函数.
WndClass.lpszClassName="5_6"; //窗口类名称.
WndClass.lpszMenuName=NULL; //无菜单.
WndClass.style=0; //缺省窗口风格.
return RegisterClass(&WndClass); //返回窗口注册值.
}
5-7参考答案:
#include
#include
#include
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明.
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);//初始化窗口声明. HFONT CreateFont(HDC hDC,int nCharHeight,BOOL bItalic);
HWND hWndMain; //定义窗口句柄.
int nChar=0;
BOOL bRight=TRUE,bLeft=FALSE,bItalic=FALSE;
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message;
if(!InitWindowsClass(hInstance)) //初始化窗口类.
return FALSE;
if(!InitWindows(hInstance,nCmdShow)) //初始化窗口.
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环.
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam) //消息处理函数. {
HDC hDC; //定义设备环境句柄.
HFONT hF; //定义字体句柄.
PAINTSTRUCT ps; //定义包含绘图信息的结构体变量
TEXTMETRIC tm; //定义包含字体信息的结构体变量.
char lpsz_1[]="abcdefghijklmnopqrstuvwxyz"; //定义输出的字符串.
int nCharlen=strlen(lpsz_1); //定义字符串长度变量.
int X=0,Y=0,i;
switch(iMessage)
{
case WM_CREATE:
SetTimer(hWnd,1,300,NULL); //建立计时器,每0.1秒发出WM_TIMER消息.
break;
case WM_PAINT: //处理绘图消息.
hDC=BeginPaint(hWnd,&ps); //获得设备环境指针.
//输出第1行字体.
hF = CreateFont(hDC,40,0); //创建字体.
SelectObject(hDC,hF); //选入字体.
GetTextMetrics(hDC,&tm); //得到包含字体信息的结构体.
Y=tm.tmExternalLeading+10; //设置输出字符的Y坐标.
for(i=0;i
#include
#include
#include
#define Pi 3.1415926
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明.
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);//初始化窗口声明. HFONT CreateFont(HDC hDC,int nCharHeight,int nCharWidth);//创建字体函数声明. HWND hWndMain; //定义窗口句柄.
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message;
if(!InitWindowsClass(hInstance)) //初始化窗口类.
return FALSE;
if(!InitWindows(hInstance,nCmdShow)) //初始化窗口.
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环.
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam) //消息处理函数. {
HDC hDC; //定义设备环境句柄.
HFONT hF; //定义字体句柄.
PAINTSTRUCT ps; //定义包含绘图信息的结构体变量
char lpsz_1[]="欲穷千里目更上一层楼"; //定义输出的字符串.(白日依山尽黄河入海流)
int nCharlen=strlen(lpsz_1)/2; //定义字符串长度变量.
int X=0,Y=0,i;
int nCharHeight;
switch(iMessage)
{
case WM_PAINT: //处理绘图消息.
hDC=BeginPaint(hWnd,&ps); //获得设备环境指针.
SetMapMode(hDC,MM_ANISOTROPIC); //设置映射模式.
SetWindowExtEx(hDC,640,480,NULL); //设置窗口范围.
SetViewportExtEx(hDC,640,480,NULL); //设置视口范围.
//输出字体大小线形变化的艺术字.
for(i=0;i//包括了windows函数和数据类型,结构,常量,宏
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM) ;//Wndproc是窗口过程函数名
int WINAPI WinMain( //windows希望winmain使用通用的API函数调用约定
HINSTANCE hInstance,//当前实例句柄
HINSTANCE hPrevInstance,//前一个实例句柄
LPSTR lpszCmdParam, //指向字符串的指针,这里指向存放内存运行程序名的缓冲区
int nCmdShow) //应用程序开始执行时窗口的显示方式,常用SW_SHOWNORMAL {
static char szAppName[] = "Hello60" ; //窗口类的名字,经常为程序名
HWND hwnd ; //窗口句柄
MSG msg ; //消息结构类
WNDCLASS wndclass ; //窗口类
if(!hPrevInstance)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW ;//定义窗口类型为整个窗口重画
wndclass.lpfnWndProc= WndProc ; //窗口处理函数
wndclass.cbClsExtra = 0 ; //不留额外类结构空间(窗口类无扩展)
wndclass.cbWndExtra = 0 ; //不留额外窗口结构空间(窗口实例无扩展)
wndclass.hInstance = hInstance ; //设置winmain的第一个参数
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);//IDI_APPLICATION为窗口最小化确省图标
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;//定义窗口采用IDC_ARROW(箭头)光标
wndclass.hbrBackground = (HBRUSH)GetStockObject (LTGRAY_BRUSH) ;//窗口客户区的背景为浅灰色
wndclass.lpszMenuName = NULL ; //应用程序窗口中没有菜单,所以菜单名为NULL
wndclass.lpszClassName = szAppName ;//窗口类的名字,一般应和程序名字一样
RegisterClass (&wndclass) ;//这个函数用来注册窗口类
}
hwnd = CreateWindow ( //该函数创建一个窗口实例
szAppName, // window class name
"The Hello Program", // 窗口实例标题名
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT,CW_USEDEFAULT, // 窗口左上角坐标
CW_USEDEFAULT,CW_USEDEFAULT, // 窗口的高和宽
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle of current window
NULL); // creation parameters
ShowWindow (hwnd, nCmdShow) ; //nCmdShow标志显示方式
UpdateWindow (hwnd) ; //绘制用户区,发送WM_PAINT消息
while (GetMessage (&msg, NULL, 0, 0))//取得消息,除受到WM_QUIT外,任何消息都返回TRUE
{
TranslateMessage (&msg) ; //对一些键盘消息进行翻译
DispatchMessage (&msg) ; //将消息传送给在MSG结构中为窗口所指定的消息处理器
}
return msg.wParam ; //程序终止时告知系统
}
LRESULT CALLBACK WndProc( HWND hwnd, //接收消息的窗口句柄
UINT message, //调用的消息号
WPARAM wParam,//消息参数
LPARAM lParam)//消息参数
{
HDC hdc ; //设备句柄
PAINTSTRUCT ps ; //画刷结构
RECT rect ; //无效矩形区
static LOGFONT lf;
HFONT holdFont,hnewFont;
switch (message) //消息处理函数
{
case WM_CREATE: //窗口创建,返回0
return 0;
case WM_PAINT: //绘制消息
lf.lfHeight=-64;
lf.lfWeight=500;
lf.lfPitchAndFamily=DEFAULT_PITCH & FF_DONTCARE;
lf.lfCharSet=GB2312_CHARSET; //选择字符集
strcpy(lf.lfFaceName,"黑体"); //字符串赋值,设置字体
hnewFont=CreateFontIndirect(&lf); //创建一种新的逻辑字体
hdc = BeginPaint (hwnd, &ps); //得到设备环境句柄
GetClientRect (hwnd, &rect); //得到需要绘制的客户区
GetClientRect (hwnd, &rect) ; //得到需要绘制的客户区
holdFont=(HFONT)SelectObject(hdc,hnewFont); //选择新的字体格式,并且保存老的画笔
SetTextColor(hdc,RGB(255,0,0)); //设置文本颜色
SetBkColor(hdc,RGB(255,255,0)); //设置背景颜色
DrawText(hdc, //输出文本
"VC中显示字体与背景", //输出的字符串
-1, //自动计算输出字符的个数
&rect, //输出文本区区域
DT_SINGLELINE|DT_CENTER|DT_VCENTER); //字体输出在矩形客户区的中央
SelectObject(hdc,holdFont); //保存原来的字体格式
DeleteObject(hnewFont); //删除新的字体格式
EndPaint (hwnd, &ps) ; //结束绘制,与BeiginPaint函数配套
return 0 ;
case WM_DESTROY: //退出消息
PostQuitMessage (0) ; //告诉系统退出程序
return 0 ;
}
return DefWindowProc(hwnd,message,wParam,lParam) ;//调用默认函数处理没有处理的消息 }
6-3参考答案:
#include
#include
#include
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明.
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);//初始化窗口声明. HWND hWndMain;
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message;
if(!InitWindowsClass(hInstance)) //初始化窗口类.
return FALSE;
if(!InitWindows(hInstance,nCmdShow)) //初始化窗口.
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环.
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam) //消息处理函数. {
HDC hDC; //定义设备环境句柄.
PAINTSTRUCT ps; //定义包含绘图信息的结构体变量
HPEN hPen; //定义画笔句柄。
HBRUSH hBrush; //定义画刷句柄
//定义输出的字符串。
static char cUp[]="You had hitted the UP key";
static char cCtrl[]="You had hitted the Ctrl key";
static char cShift[]="You had hitted the SHIFT key";
static char cCtrl_A[]="You had hitted the CTRL A key";
static char cShift_B[]="You had hitted the SHIFT B key";
//定义并初始化按键标志变量。
static BOOL nUpKeyDown=FALSE, nShiftKeyDown=FALSE,
nCtrlKeyDown=FALSE, nCtrlAKeyDown=FALSE, nShiftBKeyDown=FALSE;
switch(iMessage)
{
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_UP: //当按上箭头键时,变量置为真。
nUpKeyDown = TRUE;
break;
case VK_SHIFT: //当按shift键时,变量置为真。
nShiftKeyDown = TRUE;
break;
case VK_CONTROL: //当按control键时,变量置为真。
nCtrlKeyDown = TRUE;
break;
default:
break;
}
}
break;
case WM_KEYUP:
InvalidateRect(hWnd,NULL,FALSE); //刷新用户区。
break;
case WM_CHAR:
if(wParam==(65&VK_CONTROL))
{
if(nCtrlKeyDown == TRUE)
{
nCtrlAKeyDown = TRUE;
nCtrlKeyDown = FALSE;
}
}
else if(wParam==98||wParam==66) //当按下b键时
{
if(nShiftKeyDown == TRUE) //检查shift键是否处于按下状态。
{
nShiftBKeyDown = TRUE; //当SHIFT键按下时,变量置为真。
nShiftKeyDown = FALSE;
}
}
break;
case WM_PAINT: //处理绘图消息.
hDC=BeginPaint(hWnd,&ps);
hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH); //创建 白画刷。
hPen = (HPEN)GetStockObject(WHITE_PEN); //创建白画笔。
SelectObject(hDC,hPen); //选入白画刷
SelectObject(hDC,hBrush); //选入白画笔
SetTextColor(hDC,RGB(255,0,0)); //设置字体颜色为红色。
//输出信息。
if(nUpKeyDown == TRUE)
{
Rectangle(hDC,0,0,300,200);
TextOut(hDC,0,0,cUp,strlen(cUp));
nUpKeyDown = FALSE;
}
else if(nCtrlAKeyDown == TRUE)
{
Rectangle(hDC,0,0,300,200);
TextOut(hDC,0,100,cCtrl_A,strlen(cCtrl_A));
nCtrlAKeyDown = FALSE;
nCtrlKeyDown = FALSE;
}
else if(nCtrlKeyDown == TRUE&&nCtrlAKeyDown == FALSE)
{
Rectangle(hDC,0,0,300,200);
TextOut(hDC,0,60,cCtrl,strlen(cCtrl));
nCtrlKeyDown = FALSE;
}
else if(nShiftBKeyDown == TRUE)
{
Rectangle(hDC,0,0,300,200);
TextOut(hDC,0,0,cShift_B,strlen(cShift_B));
nShiftBKeyDown = FALSE;
nShiftKeyDown = FALSE;
}
else if(nShiftBKeyDown == FALSE&&nShiftKeyDown == TRUE)
{
Rectangle(hDC,0,0,300,200);
TextOut(hDC,0,0,cShift,strlen(cShift));
nShiftKeyDown = FALSE;
}
else
{
}
//删除画笔和画刷
DeleteObject(hPen);
DeleteObject(hBrush);
EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
return 0;
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow("6_3",
"键盘实例程序",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)
return FALSE;
hWndMain=hWnd;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS WndClass;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,"END");
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=WndProc;
WndClass.lpszClassName="6_3";
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW|CS_VREDRAW;
return RegisterClass(&WndClass);
WndClass.style=0;
}
6-4参考答案:
#include
#include
#include
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明.
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);//初始化窗口声明. HWND hWndMain; //定义全局窗口句柄.
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message;
if(!InitWindowsClass(hInstance)) //初始化窗口类.
return FALSE;
if(!InitWindows(hInstance,nCmdShow)) //初始化窗口.
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环.
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam) //消息处理函数. {
HDC hDC; //定义设备环境句柄.
WORD x,y; //定义鼠标的位置坐标.
HCURSOR hCursor; //定义光标句柄.
HBRUSH hBrush;
LPSIZE lpSize; //窗口尺寸结构.
static char cLeftBtn[]="LEFT BUTTON"; //定义单击左键时的输出字符.
static char cRightBtn[]="RIGHT BUTTON"; //定义单击右键时的输出字符.
RECT ClientRect;
switch(iMessage)
{
case WM_MOUSEMOVE: //处理鼠标移动信息.
x=LOWORD(lParam); //获得鼠标的X坐标.
y=HIWORD(lParam); //获得鼠标的Y坐标.
GetClientRect(hWnd,&ClientRect); //得到窗口大小
if(y>=0&&y<=ClientRect.bottom)
{
是箭头. if(y>=0&&y<=ClientRect.bottom/5) //在此区域,
{
hCursor=LoadCursor(NULL,IDC_ARROW);
SetCursor(hCursor);
}
else if(y>=ClientRect.bottom/5&&y<=ClientRect.bottom/5*2) //在此区域为十字图标.
{
hCursor=LoadCursor(NULL,IDC_CROSS);
SetCursor(hCursor);
}
else if(y>=ClientRect.bottom/5*2&&y<=ClientRect.bottom/5*3) //在此区域为东北西南方向的箭头光标.
{
hCursor=LoadCursor(NULL,IDC_SIZENESW);
SetCursor(hCursor);
}
else if(y>=ClientRect.bottom/5*3&&y<=ClientRect.bottom/5*4) //在此区域为西北东南方向的箭头光标.
{
hCursor=LoadCursor(NULL,IDC_SIZENS);
SetCursor(hCursor);
}
else //在此区域为等待光标.
{
hCursor=LoadCursor(NULL,IDC_WAIT);
SetCursor(hCursor);
}
}
return 0;
case WM_LBUTTONDOWN: //单击左建.
hDC=GetDC(hWnd); //得到设备环境指针.
SetBkColor(hDC,RGB(255,255,0)); //设置背景色.
TextOut(hDC,0,0,cLeftBtn,strlen(cLeftBtn)); //输出.
ReleaseDC(hWnd,hDC); //释放设备环境指针.
break;
case WM_RBUTTONDOWN: //单击右建.
hDC=GetDC(hWnd); //得到设备环境指针.
SetBkColor(hDC,RGB(255,255,0)); //设置背景色.
TextOut(hDC,0,0,cRightBtn,strlen(cRightBtn)); //输出.
ReleaseDC(hWnd,hDC); //释放设备环境指针.
break;
case WM_SIZE:
hDC=GetDC(hWnd); //得到设备环境指针.
SetMapMode(hDC,MM_ANISOTROPIC); //设置映象模式.
SetViewportExtEx(hDC,LOWORD(lParam),HIWORD(lParam),lpSize); //设置试口区域.
SetWindowExtEx(hDC,100,50,lpSize); //设置窗口区域.
hBrush=CreateSolidBrush(RGB(255,255,255)); //定义白画刷.
SelectObject(hDC,hBrush); //选入白画刷.
Rectangle(hDC,0,0,100,10); //填充矩形
DeleteObject(hBrush); //删除画刷.
hBrush=CreateSolidBrush(RGB(0,255,0)); //定义绿画刷.
SelectObject(hDC,hBrush); //选入绿画刷.
Rectangle(hDC,0,10,100,20); //填充矩形.
DeleteObject(hBrush); //删除画刷.
hBrush=CreateSolidBrush(RGB(0,0,255)); //定义蓝画刷.
SelectObject(hDC,hBrush); //选入蓝画刷.
Rectangle(hDC,0,20,100,30); //填充矩形.
DeleteObject(hBrush); //删除画刷.
hBrush=CreateSolidBrush(RGB(255,255,0)); //定义黄画刷.
SelectObject(hDC,hBrush); //选入黄画刷.
Rectangle(hDC,0,30,100,40); //填充矩形.
DeleteObject(hBrush); //删除画刷.
hBrush=CreateSolidBrush(RGB(255,0,0)); //定义红画刷.
SelectObject(hDC,hBrush); //选入红画刷.
Rectangle(hDC,0,40,100,50); //填充矩形.
DeleteObject(hBrush); //删除画刷.
ReleaseDC(hWnd,hDC); //删除设备环境指针.
break;
case WM_DESTROY: //处理退出窗口信息.
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
return 0;
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow("6_4", //窗口类名称.
"鼠标显示实例程序", //标题栏名称.
WS_OVERLAPPEDWINDOW, //窗口样式.有标题栏和最大最小化按钮.
CW_USEDEFAULT, //窗口左上角坐标.
0,
600, //采用缺省的高度和宽度.
400,
NULL, //无父窗口.
NULL, //无菜单.
hInstance, //当前应用实例句柄.
NULL); //指向传递给窗口的参数的指针.不用.
if(!hWnd) //创建失败.
return FALSE;
hWndMain=hWnd; //给全局窗口句柄付值.
ShowWindow(hWnd,nCmdShow); //显示窗口.
UpdateWindow(hWnd); //刷新用户区.
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance) {
WNDCLASS WndClass;
WndClass.cbClsExtra=0; //无窗口类扩展.
WndClass.cbWndExtra=0; //无窗口实例扩展.
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));//背景为白色.
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //光标为为箭头.
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //采用缺省图标.
WndClass.hInstance=hInstance; //当前实例.
WndClass.lpfnWndProc=WndProc; //消息处理函数.
WndClass.lpszClassName="6_4"; //窗口类名称.
WndClass.lpszMenuName=NULL; //无菜单.
WndClass.style=0; //缺省窗口风格.
return RegisterClass(&WndClass); //返回窗口注册值. }
6-5参考答案:
#include
#include
#include
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明.
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);//初始化窗口声明. HWND hWndMain; //定义全局窗口句柄. RECT rect1; //定义矩形结构体.记录了图形的信息.
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message;
if(!InitWindowsClass(hInstance)) //初始化窗口类.
return FALSE;
if(!InitWindows(hInstance,nCmdShow)) //初始化窗口.
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环.
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam) //消息处理函数. {
HDC hDC; //定义设备环境句柄.
WORD x,y; //定义鼠标的位置坐标.
static BOOL bCircle=FALSE,bRect=FALSE; //设置绘制圆和矩形的标志变量.
PAINTSTRUCT ps; //定义绘图结构体变量.
x = LOWORD(lParam); //得到鼠标的位置.
y = HIWORD(lParam);
switch(iMessage)
{
case WM_LBUTTONDOWN: //处理按下鼠标左键消息.
if(wParam&MK_CONTROL) //同时按下Ctrl键时.
{
bCircle = TRUE; //画圆.
bRect = FALSE;
rect1.left = x; //圆的左上角坐标为当前鼠标位置.
rect1.top = y;
}
else if(wParam&MK_SHIFT) //同时按下shift键时.
{
bRect = TRUE; //画矩形.
bCircle = FALSE;
rect1.left = x; //矩形的左上角坐标为当前鼠标位置.
rect1.top = y;
}
else
{
bRect = FALSE;
bCircle = FALSE;
}
break;
case WM_LBUTTONUP: //当松开左键时.绘图标志为false.
bRect = FALSE;
bCircle = FALSE;
break;
case WM_MOUSEMOVE: //处理鼠标移动信息.
rect1.right = x; //图形的右下角坐标为当前鼠标位置.
rect1.bottom = y;
if(bRect == TRUE || bCircle == TRUE)
{
InvalidateRect(hWnd,NULL,TRUE); //发出重绘信息.
}
break;
case WM_PAINT:
hDC = BeginPaint(hWnd,&ps);
if(bCircle == TRUE)
{ //绘制圆形.
Ellipse(hDC,rect1.left,rect1.top,rect1.right,rect1.bottom);
}
if(bRect == TRUE)
{ //绘制矩形.
Rectangle(hDC,rect1.left,rect1.top,rect1.right,rect1.bottom);
}
EndPaint(hWnd,&ps);
break;
case WM_DESTROY: //处理退出窗口信息.
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
return 0;
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow("6_4", //窗口类名称.
"鼠标显示实例程序", //标题栏名称.
WS_OVERLAPPEDWINDOW, //窗口样式.有标题栏和最大最小化按钮.
CW_USEDEFAULT, //窗口左上角坐标.
0,
CW_USEDEFAULT, //采用缺省的高度和宽度.
0,
NULL, //无父窗口.
NULL, //无菜单.
hInstance, //当前应用实例句柄.
NULL); //指向传递给窗口的参数的指针.不用.
if(!hWnd) //创建失败.
return FALSE;
hWndMain=hWnd; //给全局窗口句柄付值.
ShowWindow(hWnd,nCmdShow); //显示窗口.
UpdateWindow(hWnd); //刷新用户区.
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS WndClass;
WndClass.cbClsExtra=0; //无窗口类扩展.
WndClass.cbWndExtra=0; //无窗口实例扩展.
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));//背景为白色.
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //光标为为箭头.
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //采用缺省图标.
WndClass.hInstance=hInstance; //当前实例.
WndClass.lpfnWndProc=WndProc; //消息处理函数.
WndClass.lpszClassName="6_4"; //窗口类名称.
WndClass.lpszMenuName=NULL; //无菜单.
WndClass.style=0; //缺省窗口风格.
return RegisterClass(&WndClass); //返回窗口注册值.
}
6-6参考答案:
#include
#include
#include
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明.
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);//初始化窗口声明. HWND hWndMain; //定义全局窗口句柄. RECT rect1; //定义矩形结构体.记录了图形的信息.
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message;
if(!InitWindowsClass(hInstance)) //初始化窗口类.
return FALSE;
if(!InitWindows(hInstance,nCmdShow)) //初始化窗口.
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环.
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam) //消息处理函数. {
HDC hDC; //定义设备环境句柄.
static BOOL bCircle=FALSE,bRect=FALSE; //设置绘制圆和矩形的标志变量.
PAINTSTRUCT ps; //定义绘图结构体变量.
switch(iMessage)
{
case WM_KEYDOWN:
if(wParam == VK_CONTROL) //按下Ctrl键时.
{
bCircle = TRUE; //画圆.
bRect = FALSE;
rect1.left = 0; //初始化矩形结构体.
rect1.right = 0;
rect1.top = 0;
rect1.bottom = 0;
}
else if(wParam == VK_SHIFT) //按下shift键时.
{
bRect = TRUE; //画矩形.
bCircle = FALSE;
rect1.left = 0; //初始化矩形结构体.
rect1.right = 0;
rect1.top = 0;
rect1.bottom = 0;
}
else if(wParam == VK_RIGHT)
{
rect1.right +=10; //按下右箭头时矩形框长度加10.
if(bRect == TRUE || bCircle == TRUE)
{
InvalidateRect(hWnd,NULL,TRUE); //发出重绘信息.
}
}
else if(wParam == VK_DOWN)
{
rect1.bottom +=10; //按下下箭头矩形框高度加10.
if(bRect == TRUE || bCircle == TRUE)
{
InvalidateRect(hWnd,NULL,TRUE); //发出重绘信息.
}
}
else if(wParam == VK_PRIOR)
{ //按pageup键时,向上移动.
rect1.top -=10;
rect1.bottom -=10;
if(bRect == TRUE || bCircle == TRUE)
{
InvalidateRect(hWnd,NULL,TRUE); //发出重绘信息.
}
}
else if(wParam == VK_NEXT)
{ //按pagedown键,向下移动.
rect1.top +=10;
rect1.bottom +=10;
if(bRect == TRUE || bCircle == TRUE)
{
InvalidateRect(hWnd,NULL,TRUE); //发出重绘信息.
}
}
else if(wParam == VK_HOME)
{ //按home键,向左移动.
rect1.left -=10;
rect1.right -=10;
if(bRect == TRUE || bCircle == TRUE)
{
InvalidateRect(hWnd,NULL,TRUE); //发出重绘信息.
}
}
else if(wParam == VK_END)
{ //按end键,向右移动.
rect1.left +=10;
rect1.right +=10;
if(bRect == TRUE || bCircle == TRUE)
{
InvalidateRect(hWnd,NULL,TRUE); //发出重绘信息.
}
}
break;
case WM_PAINT:
hDC = BeginPaint(hWnd,&ps);
if(bCircle == TRUE)
{ //绘制圆形.
Ellipse(hDC,rect1.left,rect1.top,rect1.right,rect1.bottom);
}
if(bRect == TRUE)
{ //绘制矩形.
Rectangle(hDC,rect1.left,rect1.top,rect1.right,rect1.bottom);
}
EndPaint(hWnd,&ps);
break;
case WM_DESTROY: //处理退出窗口信息.
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
return 0;
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow("6_4", //窗口类名称.
"鼠标显示实例程序", //标题栏名称.
WS_OVERLAPPEDWINDOW, //窗口样式.有标题栏和最大最小化按钮.
CW_USEDEFAULT, //窗口左上角坐标.
0,
CW_USEDEFAULT, //采用缺省的高度和宽度.
0,
NULL, //无父窗口.
NULL, //无菜单.
hInstance, //当前应用实例句柄.
NULL); //指向传递给窗口的参数的指针.不用.
if(!hWnd) //创建失败.
return FALSE;
hWndMain=hWnd; //给全局窗口句柄付值.
ShowWindow(hWnd,nCmdShow); //显示窗口.
UpdateWindow(hWnd); //刷新用户区.
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS WndClass;
WndClass.cbClsExtra=0; //无窗口类扩展.
WndClass.cbWndExtra=0; //无窗口实例扩展.
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));//背景为白色.
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //光标为为箭头.
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //采用缺省图标.
WndClass.hInstance=hInstance; //当前实例.
WndClass.lpfnWndProc=WndProc; //消息处理函数.
WndClass.lpszClassName="6_4"; //窗口类名称.
WndClass.lpszMenuName=NULL; //无菜单.
WndClass.style=0; //缺省窗口风格.
return RegisterClass(&WndClass); //返回窗口注册值.
}
6-7参考答案:
#include
#include
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
RECT rect ;
long cxChar, cyChar ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
static char szAppName[] = "KMessage" ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
if (!hPrevInstance)
{
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_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
RegisterClass (&wndclass) ;
}
hwnd = CreateWindow (szAppName, "Keyboard Message Looker",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, nCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
void ShowKey (HWND hwnd, int iType, char *szMessage, UINT wParam, LONG lParam)
{
static char *szFormat[2] = { "%-14s %3d %c %6u %4d %3s %3s %4s %4s",
"%-14s %3d %c %6u %4d %3s %3s %4s %4s" } ;
char szBuffer[80] ;
HDC hdc ;
ScrollWindow (hwnd, 0, -cyChar, &rect, &rect) ;
hdc = GetDC (hwnd) ;
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
TextOut (hdc, cxChar, rect.bottom - cyChar, szBuffer,
wsprintf (szBuffer, szFormat [iType],
(LPSTR) szMessage, wParam,
(BYTE) (iType ? wParam : ' '),
LOWORD (lParam),
HIWORD (lParam) & 0xFF,
(LPSTR) (0x01000000 & lParam ? "Yes" : "No"),
(LPSTR) (0x20000000 & lParam ? "Yes" : "No"),
(LPSTR) (0x40000000 & lParam ? "Down" : "Up"),
(LPSTR) (0x80000000 & lParam ? "Up" : "Down"))) ;
ReleaseDC (hwnd, hdc) ;
ValidateRect (hwnd, NULL) ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
static char szTop[] =
"Message Key Char Repeat Scan Ext ALT Prev Tran";
static char szUnd[]=
"_______ ___ ____ ______ ____ ___ ___ ____ ____";
HDC hdc ;
PAINTSTRUCT ps ;
TEXTMETRIC tm ;
switch (message)
{
case WM_CREATE:
hdc = GetDC (hwnd) ;
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight ;
ReleaseDC (hwnd, hdc) ;
rect.top = 3 * cyChar / 2 ;
return 0 ;
case WM_SIZE:
rect.right = LOWORD (lParam) ;
rect.bottom = HIWORD (lParam) ;
UpdateWindow (hwnd) ;
return 0 ;
case WM_PAINT:
InvalidateRect (hwnd, NULL, TRUE) ;
hdc = BeginPaint (hwnd, &ps) ;
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
SetBkMode (hdc, TRANSPARENT) ;
TextOut (hdc, cxChar, cyChar / 2, szTop, (sizeof szTop) - 1) ;
TextOut (hdc, cxChar, cyChar / 2, szUnd, (sizeof szUnd) - 1) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_KEYDOWN:
ShowKey (hwnd, 0, "WM_KEYDOWN", wParam, lParam) ;
return 0 ;
case WM_KEYUP:
ShowKey (hwnd, 0, "WM_KEYUP", wParam, lParam) ;
return 0 ;
case WM_CHAR:
ShowKey (hwnd, 1, "WM_CHAR", wParam, lParam) ;
return 0 ;
case WM_DEADCHAR:
ShowKey (hwnd, 1, "WM_DEADCHAR", wParam, lParam) ;
return 0 ;
case WM_SYSKEYDOWN:
ShowKey (hwnd, 0, "WM_SYSKEYDOWN", wParam, lParam) ;
break ; // ie, call DefWindowProc
case WM_SYSKEYUP:
ShowKey (hwnd, 0, "WM_SYSKEYUP", wParam, lParam) ;
break ; // ie, call DefWindowProc
case WM_SYSCHAR:
ShowKey (hwnd, 1, "WM_SYSCHAR", wParam, lParam) ;
break ;
case WM_SYSDEADCHAR:
ShowKey (hwnd, 1, "WM_SYSDEADCHAR", wParam, lParam) ;
break ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
6-8参考答案:
#include
#define MAXPOINTS 1000
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
static char szAppName[] = "MMsg1" ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
if (!hPrevInstance)
{
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_ARROW) ;
wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
RegisterClass (&wndclass) ;
}
hwnd = CreateWindow (szAppName, "Connect-the-Points Mouse Demo",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, nCmdShow) ;
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)
{
static POINTS points[MAXPOINTS] ;
static short nCount ;
HDC hdc ;
PAINTSTRUCT ps ;
short i, j ;
switch (message)
{
case WM_LBUTTONDOWN://单击鼠标左键
nCount = 0 ;
InvalidateRect (hwnd, NULL, TRUE) ;//整个客户区重画,产生wparam消息
return 0 ;
case WM_MOUSEMOVE://鼠标移动
if (wParam & MK_LBUTTON && nCount < 1000)
{
points [nCount++] = MAKEPOINTS (lParam) ;//此宏返回POINTS类,这里包含有鼠标的X-,Y-坐标信息
hdc = GetDC (hwnd) ;
SetPixel (hdc, LOWORD (lParam), HIWORD (lParam), 0L) ;
ReleaseDC (hwnd, hdc) ;
}
return 0 ;
case WM_LBUTTONUP:
InvalidateRect (hwnd, NULL, FALSE) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
for (i = 0 ; i < nCount - 1 ; i++)
for (j = i ; j < nCount ; j++)
{
MoveToEx (hdc, points[i].x, points[i].y,NULL) ;//移动当前的位置,并且返回前一个位置的值
LineTo (hdc, points[j].x, points[j].y) ;//在两点之间画线
}
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
6-9参考答案:
#include
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
static char szAppName[] = "6_9" ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
if (!hPrevInstance)
{
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_ARROW) ;
wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
RegisterClass (&wndclass) ;
}
hwnd = CreateWindow (szAppName, "Blow-Up Mouse Demo",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, nCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
void InvertBlock (HWND hwnd, POINT ptBeg, POINT ptEnd)
{
HDC hdc ;
hdc = CreateDC ("DISPLAY", NULL, NULL, NULL) ;
ClientToScreen (hwnd, &ptBeg) ;//转换指定窗口的客户区内的点(PIONT结构)的坐标
ClientToScreen (hwnd, &ptEnd) ;
PatBlt (hdc, ptBeg.x, ptBeg.y, ptEnd.x - ptBeg.x, ptEnd.y - ptBeg.y,
DSTINVERT) ;
//这个函数在矩形区内用当前的画笔重画背景和前景*/
DeleteDC (hdc) ;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam,LPARAM
lParam)
{
static BOOL fCapturing=FALSE, fBlocking=FALSE;
static POINT ptBeg, ptEnd ;
HDC hdc ;
RECT rect ;
switch (message)
{
case WM_LBUTTONDOWN:
if (!fCapturing)
{
fCapturing = TRUE ;//左键击活俘获
SetCapture (hwnd) ;//把所有的鼠标信息输入到被左键击活的窗口,无论鼠标是否在窗口内活动,
//直到鼠标被再次点下为止
SetCursor (LoadCursor (NULL, IDC_CROSS)) ;//载入光标
}
if (!fBlocking)
{
fBlocking = TRUE ;//左键击活俘获
ptBeg.x=LOWORD(lParam);
ptBeg.y=HIWORD(lParam);
}
return 0 ;
case WM_MOUSEMOVE:
if (fBlocking)
{
ptEnd.x=LOWORD(lParam);
ptEnd.y=HIWORD(lParam);
InvertBlock (hwnd, ptBeg, ptEnd) ;
// InvertBlock (hwnd, ptBeg, ptEnd) ;
}
return 0 ;
case WM_LBUTTONUP:
if (fBlocking)
{
fCapturing = fBlocking = FALSE ;
ptEnd.x=LOWORD(lParam);
ptEnd.y=HIWORD(lParam);
SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
hdc = GetDC (hwnd) ;
GetClientRect (hwnd, &rect) ;
StretchBlt (hdc, 0, 0, rect.right, rect.bottom,
hdc, ptBeg.x, ptBeg.y,
ptEnd.x - ptBeg.x, ptEnd.y - ptBeg.y,
SRCCOPY) ;//把位图载入指定的矩形客户区,并且拉伸或压缩位图
//恰好适合矩形区
ReleaseDC (hwnd, hdc) ;
SetCursor (LoadCursor (NULL, IDC_ARROW)) ;
ReleaseCapture () ;//把鼠标从当前窗口中释放出来,您应该不难找出前面与之配套的函数
}
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
7-7参考答案:
#include
#include "7_7.h"
#include
#include
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明.
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);//初始化窗口声明.
HWND hWndMain; //定义全局窗口句柄.
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message;
if(!InitWindowsClass(hInstance)) //初始化窗口类.
return FALSE;
if(!InitWindows(hInstance,nCmdShow)) //初始化窗口.
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环.
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam) //消息处理函数. {
switch(iMessage)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
return 0;
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd=CreateWindow("7_7", //窗口类名称.
"菜单显示实例程序", //标题栏名称.
WS_OVERLAPPEDWINDOW, //窗口样式.有标题栏和最大最小化按钮.
CW_USEDEFAULT, //窗口左上角坐标.
0,
CW_USEDEFAULT, //采用缺省的高度和宽度.
0,
NULL, //无父窗口.
NULL, //无菜单.
hInstance, //当前应用实例句柄.
NULL); //指向传递给窗口的参数的指针.不用.
if(!hWnd) //创建失败.
return FALSE;
hWndMain=hWnd; //给全局窗口句柄付值.
ShowWindow(hWnd,nCmdShow); //显示窗口.
UpdateWindow(hWnd); //刷新用户区.
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS WndClass;
WndClass.cbClsExtra=0; //无窗口类扩展.
WndClass.cbWndExtra=0; //无窗口实例扩展.
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));//背景为白色.
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //光标为为箭头.
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //采用缺省图标.
WndClass.hInstance=hInstance; //当前实例.
WndClass.lpfnWndProc=WndProc; //消息处理函数.
WndClass.lpszClassName="7_7"; //窗口类名称.
WndClass.lpszMenuName="Menu"; //定义菜单为Menu.
WndClass.style=0; //缺省窗口风格.
return RegisterClass(&WndClass); //返回窗口注册值.
}
7-8参考答案:
#include
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam); //消息处理函数声明.
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);//初始化窗口声明
HWND hWndMain; //定义窗口句柄
HDC hDC; //定义设备环境句柄
HDC hdcmem; //定义内存句柄
HBITMAP hBm; //定义位图句柄
BITMAP bm; //定义位图结构变量
int iY=80; //位图左上角初始y坐标
int iWindowWidth,iWindowHeight; //窗口的宽度和高度 char cUpWarn[]="不能再向上移动了"; //向上警告字符串 char cDownWarn[]="不能再向下移动了"; //向下警告字符串
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) //主函数
{
MSG Message; //定义消息变量
hBm=LoadBitmap(hInstance,"forest"); //加载位图
GetObject(hBm,sizeof(BITMAP),(LPVOID)&bm); //获得位图信息
iWindowWidth=2*bm.bmWidth; //得到窗口的宽度
iWindowHeight=2*bm.bmHeight; //得到窗口的高度
if(!InitWindowsClass(hInstance)) //初始化窗口类
return FALSE;
if(!InitWindows(hInstance,nCmdShow)) //初始化窗口
return FALSE;
while(GetMessage(&Message,0,0,0))
{
TranslateMessage(&Message); //消息循环
DispatchMessage(&Message);
}
return Message.wParam; }
long WINAPI WndProc(HWND hWnd,
UINT iMessage,
UINT wParam,
LONG lParam) //消息处理函数 {
PAINTSTRUCT ps; //定义包含绘图信息的结构变量
switch(iMessage)
{
case WM_LBUTTONDOWN: //点击鼠标左键
iY=iY-10; //位图高度-10
InvalidateRect(hWnd,NULL,1);//刷新用户区
break;
case WM_RBUTTONDOWN: //点击鼠标右键
iY=iY+10; //位图高度+10;
InvalidateRect(hWnd,NULL,1);//刷新用户区
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_UP: //按上箭头时,位置-10
iY=iY-10;
break;
case VK_DOWN: //按下箭头时,位置+10
iY=iY+10;
break;
}
InvalidateRect(hWnd,NULL,1);//刷新用户区
break;
case WM_CREATE: //初始化窗口消息
hDC=GetDC(hWnd); //得到设备环境指针
hdcmem=CreateCompatibleDC(hDC); //得到内存指针
ReleaseDC(hWnd,hDC); //删除设备环境指针
case WM_PAINT: //处理绘图消息
hDC=BeginPaint(hWnd,&ps); //得到设备环境指针
if(iY>0&&iY
#include "7_9.h"
long WINAPI WndProc(HWND hWnd,UINT iMessage,
WPARAM wParam,LPARAM lParam); BOOL CALLBACK DlgProc(HWND,UINT,WPARAM,LPARAM); //对话框处理函数
HWND hDlg; //定义对话框句柄
HINSTANCE hInst; //定义应用程序实例句柄
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow) {
MSG Message; //定义消息变量
HWND hWnd; //定义窗口句柄
WNDCLASS WndClass; //定义窗口类
WndClass.cbClsExtra=0; //无窗口类扩展
WndClass.cbWndExtra=0; //无窗口实例扩展
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));//背景为白色
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //光标为为箭头
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //采用缺省图标
WndClass.hInstance=hInstance; //当前实例
WndClass.lpfnWndProc=WndProc; //消息处理函数
WndClass.lpszClassName="DlgInstance";//窗口类名称
WndClass.lpszMenuName="Menu"; //定义菜单为Menu
WndClass.style=0; //缺省窗口风格
if(!RegisterClass(&WndClass)) //注册窗口.
{
MessageBeep(0); //注册失败发出警告声.
return FALSE;
}
hWnd=CreateWindow("DlgInstance", //窗口类名称
"非模式对话框", //标题栏名称
WS_OVERLAPPEDWINDOW, //窗口样式.有标题栏和最大最小化按钮
CW_USEDEFAULT, //窗口左上角坐标
0,
CW_USEDEFAULT, //采用自定义的高度和宽度
0,
NULL, //无父窗口
NULL, //无菜单
hInstance, //当前应用实例句柄
NULL); //指向传递给窗口的参数的指针
ShowWindow(hWnd,nCmdShow); //显示窗口
UpdateWindow(hWnd); //刷新用户区
hInst=hInstance; //将应用程序句柄传递给全局变量
while(GetMessage(&Message,0,0,0))
{
if(!IsDialogMessage(hDlg,&Message)) //若是对话框消息,发往对话框处理函数
{
TranslateMessage(&Message); //消息循环
DispatchMessage(&Message);
}
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
switch(iMessage)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_SHOW: //点击Show菜单
hDlg=CreateDialog(hInst,"Show",hWnd,(DLGPROC)DlgProc); //显示对话框
break;
case IDM_HIDE: //点击Hide菜单
DestroyWindow(hDlg); //删除对话框
break;
case IDM_EXIT: //点击Exit菜单
SendMessage(hWnd,WM_DESTROY,0,0); //发送退出消息
break;
}
break;
case WM_DESTROY: //退出
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
return 0;
}
//对话框处理函数
BOOL CALLBACK DlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG: //初始化对话框
return 1;
}
return 0;
}
7-10参考答案:
#include
#include
#include
#include "7_10.h"
#include
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam);
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);//初始化窗口声明 HWND hWndMain; //定义全局窗口句柄 HINSTANCE hInst; //定义实例句柄
RECT rect1={100,100,100,100}; //定义矩形结构体.记录了图形的信息
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow) {
MSG Message;
HACCEL hAccel;
hInst = hInstance;
if(!InitWindowsClass(hInstance)) //初始化窗口类
return FALSE;
if(!InitWindows(hInstance,nCmdShow)) //初始化窗口
return FALSE;
hAccel = LoadAccelerators(hInstance,"Menu"); //加载加速键资源
while(GetMessage(&Message,0,0,0))
{
if(!TranslateAccelerator(hWndMain,hAccel,&Message))//截获加速键消息
{
TranslateMessage(&Message); //消息循环
DispatchMessage(&Message);
}
}
return Message.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,
UINT wParam,LONG lParam)
{
HMENU hMenu1,hMenu2; //定义菜单句柄
HDC hDC; //定义设备环境句柄
static BOOL bCircle = FALSE,bRect = FALSE; //设置绘制圆和矩形的标志变量
static BOOL bMove = FALSE; //设置移动标志
PAINTSTRUCT ps; //定义绘图结构体变量
switch(iMessage)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_CIRCLE: //单击圆形菜单
hMenu1 = GetMenu(hWnd); //得到菜单句柄
CheckMenuItem(hMenu1,IDM_CIRCLE,MF_CHECKED); //设置选中标志
CheckMenuItem(hMenu1,IDM_RECTANGLE,MF_UNCHECKED);//删除标志
DeleteMenu(hMenu1,1,MF_BYPOSITION); //删除“矩形”弹出菜单
hMenu2 = CreateMenu(); //动态创建菜单
//添加菜单项
AppendMenu(hMenu2,MF_ENABLED,IDM_DRAWCIRCLE,"绘制圆形(&d)");
AppendMenu(hMenu2,MF_ENABLED,IDM_MOVECIRCLE,"移动圆形(&m)");
AppendMenu(hMenu2,MF_ENABLED,IDM_ZOOMIN,"放大(&i)");
AppendMenu(hMenu2,MF_ENABLED,IDM_ZOOMOUT,"缩小(&o)");
AppendMenu(hMenu2,MF_ENABLED,IDM_REDRAW,"重绘(&r)");
//插入菜单。
InsertMenu(hMenu1,1,MF_POPUP|MF_BYPOSITION,(UINT)hMenu2,"圆形(&c)");
DrawMenuBar(hWndMain); //刷新菜单
bCircle = FALSE; //绘圆标志为假
bRect = FALSE; //绘矩形标志为假
bMove = FALSE; //移动标志为假
rect1.left = 100; //恢复矩形的初始状态
rect1.right = 100;
rect1.bottom = 100;
rect1.top = 100;
InvalidateRect(hWnd,NULL,TRUE); //发出刷新信息
break;
case IDM_RECTANGLE: //单击矩形菜单
hMenu1 = GetMenu(hWnd); //得到菜单句柄
CheckMenuItem(hMenu1,IDM_CIRCLE,MF_UNCHECKED); //删除选中标志
CheckMenuItem(hMenu1,IDM_RECTANGLE,MF_CHECKED); //设置选中标志
DeleteMenu(hMenu1,1,MF_BYPOSITION); //删除“圆形”弹出菜单
hMenu2 = LoadMenu(hInst,"menuRect"); //得到菜单句柄
//插入菜单
InsertMenu(hMenu1,1,MF_POPUP|MF_BYPOSITION,(UINT)hMenu2,"矩形(&r)");
DrawMenuBar(hWndMain); //刷新菜单
bCircle = FALSE; //绘圆标志为假
bRect = FALSE; //绘矩形标志为假
bMove = FALSE; //移动标志为假
rect1.left = 100; //恢复矩形的初始状态
rect1.right = 100;
rect1.bottom = 100;
rect1.top = 100;
InvalidateRect(hWnd,NULL,TRUE); //发出刷新信息
break;
case IDM_EXIT:
SendMessage(hWnd,WM_DESTROY,0,0); //发出关闭窗口信息
break;
case IDM_DRAWCIRCLE: //绘制圆形
bCircle = TRUE; //绘圆标志为真
bRect = FALSE; //绘矩形标志为假
bMove = FALSE; //移动标志为假
break;
case IDM_DRAWRECT: //绘制矩形
bCircle = FALSE; //绘圆标志为假
bRect = TRUE; //绘矩形标志为真
bMove = FALSE; //移动标志为假
break;
case IDM_MOVECIRCLE: //移动圆形
bMove = TRUE; //移动标志为真
break;
case IDM_MOVERECT: //移动矩形
bMove = TRUE; //移动标志为真
break;
case IDM_ZOOMIN: //放大
if(bCircle ==TRUE || bRect ==TRUE)
{
int nLength,nWidth;
nLength = abs(rect1.right-rect1.left); //计算长度
nWidth = abs(rect1.top-rect1.bottom); //计算宽度
rect1.right = (int)(rect1.left+nLength*1.2); //放大
rect1.bottom = (int)(rect1.top+nWidth*1.2);
InvalidateRect(hWnd,NULL,TRUE); //发出刷新信息
}
break;
case IDM_ZOOMOUT: //缩小
if(bCircle ==TRUE || bRect ==TRUE)
{
int nLength,nWidth;
nLength = abs(rect1.right-rect1.left); //计算长度
nWidth = abs(rect1.top-rect1.bottom); //计算宽度
rect1.right = (int)(rect1.left+nLength/1.2); //缩小
rect1.bottom = (int)(rect1.top+nWidth/1.2);
InvalidateRect(hWnd,NULL,TRUE); //发出刷新信息
}
break;
case IDM_REDRAW: //重新绘图
bMove = FALSE; //移动标志为假
rect1.left = 100; //恢复矩形的初始状态
rect1.right = 100;
rect1.top = 100;
rect1.bottom = 100;
InvalidateRect(hWnd,NULL,TRUE); //发出刷新信息
break;
}
break;
case WM_KEYDOWN:
if(bMove ==TRUE)
{
if(wParam == VK_LEFT)
{ //按left键,向左移动
rect1.left -=10;
rect1.right -=10;
InvalidateRect(hWnd,NULL,TRUE); //发出刷新信息
}
else if(wParam == VK_RIGHT)
{ //按right键,向右移动
rect1.left +=10;
rect1.right +=10;
InvalidateRect(hWnd,NULL,TRUE); //发出刷新信息
}
else if(wParam == VK_UP)
{ //按up键时,向上移动
rect1.top -=10;
rect1.bottom -=10;
InvalidateRect(hWnd,NULL,TRUE); //发出刷新信息
}
else if(wParam == VK_DOWN)
{ //按down键,向下移动
rect1.top +=10;
rect1.bottom +=10;
InvalidateRect(hWnd,NULL,TRUE); //发出刷新信息
}
}
else if(bCircle == TRUE || bRect ==TRUE)
{
if(wParam == VK_RIGHT)
{
rect1.right +=10; //按下右箭头时矩形框长度加10
InvalidateRect(hWnd,NULL,TRUE); //发出刷新信息
}
else if(wParam == VK_DOWN)
{
rect1.bottom +=10; //按下下箭头矩形框高度加10
InvalidateRect(hWnd,NULL,TRUE); //发出刷新信息
}
else if(wParam == VK_UP)
{
rect1.bottom -=10; //按下上箭头矩形框高度-10
InvalidateRect(hWnd,NULL,TRUE); //发出刷新信息
}
else if(wParam == VK_LEFT)
{
rect1.right -=10; //按下左箭头矩形框高度-10
InvalidateRect(hWnd,NULL,TRUE); //发出刷新信息
}
}
break;
case WM_PAINT:
hDC = BeginPaint(hWnd,&ps);
if(bCircle == TRUE)
{ //绘制圆形
Ellipse(hDC,rect1.left,rect1.top,rect1.right,rect1.bottom);
}
if(bRect == TRUE)
{ //绘制矩形
Rectangle(hDC,rect1.left,rect1.top,rect1.right,rect1.bottom);
}
EndPaint(hWnd,&ps);
break;
case WM_DESTROY: //处理退出窗口信息
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
return 0;
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
HMENU hMenu;
hMenu = LoadMenu(hInstance,"Menu");
hWnd=CreateWindow("7_10", //窗口类名称
"菜单显示实例程序", //标题栏名称
WS_OVERLAPPEDWINDOW, //窗口样式
CW_USEDEFAULT, //窗口左上角坐标
0,
CW_USEDEFAULT, //采用缺省的高度和宽度
0,
NULL, //无父窗口
hMenu, //菜单
hInstance, //当前应用实例句柄
NULL); //指向传递给窗口的参数的指针
if(!hWnd) //创建失败
return FALSE;
hWndMain=hWnd; //给全局窗口句柄付值
ShowWindow(hWnd,nCmdShow); //显示窗口
UpdateWindow(hWnd); //刷新用户区
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS WndClass;
WndClass.cbClsExtra=0; //无窗口类扩展
WndClass.cbWndExtra=0; //无窗口实例扩展
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));//背景为白色
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //光标为为箭头
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //采用缺省图标
WndClass.hInstance=hInstance; //当前实例
WndClass.lpfnWndProc=WndProc; //消息处理函数
WndClass.lpszClassName="7_10"; //窗口类名称
WndClass.lpszMenuName=NULL; //无菜单
WndClass.style=0; //缺省窗口风格
return RegisterClass(&WndClass); //返回窗口注册值
}
7-11参考答案:
#include
#include
#include
#include "7_11.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage,
UINT wParam,LONG lParam);
BOOL InitWindowsClass(HINSTANCE hInstance); //初始化窗口类声明 BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);//初始化窗口声明 HWND hWndMain; //定义全局窗口句柄 HINSTANCE hInst; //定义实例句柄
OPENFILENAME ofn;
CHOOSECOLOR chc;
CHOOSEFONT chf;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow) {
MSG Message;
HACCEL hAccel;
hInst = hInstance;
if(!InitWindowsClass(hInstance)) //初始化窗口类
return FALSE;
if(!InitWindows(hInstance,nCmdShow)) //初始化窗口
return FALSE;
hAccel = LoadAccelerators(hInstance,"Menu"); //加载加速键资源
while(GetMessage(&Message,0,0,0))
{
if(!TranslateAccelerator(hWndMain,hAccel,&Message))//截获加速键消息
{
TranslateMessage(&Message); //消息循环
DispatchMessage(&Message);
}
}
return Message.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam)
{
static char lpstrFileName[] = "";
static COLORREF clref[16]={0x00ff0000};
switch(iMessage)
{
case WM_CREATE:
//注意:此处只标注了一些常用的OPENFILENAME结构的成员
ofn.lStructSize = sizeof(OPENFILENAME); //数据结构长度
ofn.hwndOwner = hWnd; //拥有该结构的窗口句柄
ofn.hInstance = hInst; //应用程序实例句柄
ofn.lpstrFilter = "Text Files(*.TXT)\0*.txt\0"\
"All Files (*.*)\0*.*\0"; //文件过滤器
ofn.lpstrCustomFilter = NULL; //自定义的文件过滤器
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 1; //文件过滤器正文串索引值
ofn.lpstrFile = NULL; //指向列表框文件的指针
ofn.nMaxFile = 0;
ofn.lpstrFileTitle = NULL; //指向选定文件名的指针
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = 0; //对话框标志
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lpstrDefExt = NULL;
ofn.lCustData = 0;
ofn.lpfnHook = NULL; //钩子函数指针.同对话框处理函数功能一样
ofn.lpTemplateName = NULL;
//选择颜色通用对话框
chc.lStructSize = sizeof(CHOOSECOLOR); //结构大小
chc.hwndOwner = hWnd; //父窗口句柄
chc.rgbResult = 0; //设定默认颜色
chc.lpCustColors = clref; //指向用户自定义颜色数组的指针
chc.Flags = 0; //标志
chc.lCustData = 0;
chc.lpfnHook = NULL; //钩子函数指针.同对话框处理函数功能一样
chc.lpTemplateName = NULL;
//选择字通用体对话框
chf.lStructSize = sizeof(CHOOSEFONT); //结构大小
chf.hwndOwner = hWnd; //父窗口句柄
chf.Flags = CF_BOTH | CF_TTONLY | CF_EFFECTS;//通用对话框属性
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_NEW:
break;
case IDM_OPEN:
//设置通用对话框样式
ofn.Flags =
OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY;
ofn.lpstrTitle = "打开"; //通用对话框标题
GetOpenFileName(&ofn); //显示对话框
break;
case IDM_SAVEAS:
//设置通用对话框样式.
ofn.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST
|OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT;
ofn.lpstrTitle = "另存为..."; //通用对话框标题
GetOpenFileName(&ofn); //显示对话框
break;
case IDM_FONT:
ChooseFont(&chf);
break;
case IDM_COLOR:
ChooseColor(&chc);
break;
case IDM_ABOUT:
MessageBox(hWnd,"通用对话框程序示例","关于",MB_OK);
break;
case IDM_EXIT:
SendMessage(hWnd,WM_DESTROY,0,0);
break;
}
break;
case WM_DESTROY: //处理退出窗口信息.
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
return 0;
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
HMENU hMenu;
hMenu = LoadMenu(hInstance,"Menu");
hWnd=CreateWindow("7_11", //窗口类名称.
"通用对话框显示实例程序",//标题栏名称.
WS_OVERLAPPEDWINDOW, //窗口样式.有标题栏和最大最小化按钮.
CW_USEDEFAULT, //窗口左上角坐标.
0,
CW_USEDEFAULT, //采用缺省的高度和宽度.
0,
NULL, //无父窗口.
hMenu, //菜单.
hInstance, //当前应用实例句柄.
NULL); //指向传递给窗口的参数的指针.不用.
if(!hWnd) //创建失败.
return FALSE;
hWndMain=hWnd; //给全局窗口句柄付值.
ShowWindow(hWnd,nCmdShow); //显示窗口.
UpdateWindow(hWnd); //刷新用户区.
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance) {
WNDCLASS WndClass;
WndClass.cbClsExtra=0; //无窗口类扩展
WndClass.cbWndExtra=0; //无窗口实例扩展
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));//背景为白色
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //光标为为箭头
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); //采用缺省图标
WndClass.hInstance=hInstance; //当前实例
WndClass.lpfnWndProc=WndProc; //消息处理函数
WndClass.lpszClassName="7_11"; //窗口类名称
WndClass.lpszMenuName=NULL; //无菜单
WndClass.style=0; //缺省窗口风格
return RegisterClass(&WndClass); //返回窗口注册值
}
7-12参考答案:
/*----------------------------------------------------
BALL.C--Bouncing Ball Program
----------------------------------------------------*/
#include //包括了windows函数和数据类型,结构,常量,宏
#define min(a,b) (((a) < (b)) ? (a) : (b))//宏定义函数,
#define max(a,b) (((a) > (b)) ? (a) : (b)) LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
//Wndproc是窗口过程函数名,LRESULT说明是32位数据类型 CALLBACK说明是反调函数
int WINAPI WinMain (//WinMain在vc中相当于tc 的main函数,
//WINAPI声明表示windows希望winmain使用通用的API函数调用约定
HINSTANCE hInstance,//当前实例句柄
HINSTANCE hPrevInstance,//前一个实例句柄
LPSTR lpszCmdParam, //指向字符串的指针,这里指向存放内存运行程序名的缓冲区
int nCmdShow) //告诉应用程序如何显示初始化窗口,一般为SW—SHOW—NOMAL
{
static char szAppName[] = "Ball" ;//赋值字符串,在定义窗口特征中你会看到他
HWND hwnd ;//窗口句柄
MSG msg ;//消息结构类
WNDCLASS wndclass ;//窗口类
if (!hPrevInstance)//定义窗口特征
{
wndclass.style = CS_HREDRAW | CS_VREDRAW ;//定义窗口类型为整个窗口重画
wndclass.lpfnWndProc = WndProc ;//指向当前运行窗口,以处理不同窗口信息
wndclass.cbClsExtra = 0 ;//不留额外类结构空间
wndclass.cbWndExtra = 0 ;//不留额外窗口结构空间
wndclass.hInstance = hInstance ;//设置winmain的第一个参数
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
//定义窗口图标为IDI_APPLICATION标记的图标
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
//定义窗口光标为IDC_ARROW标记的光标,即箭头
wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH) ;//指定窗口客户区的背景为白色
wndclass.lpszMenuName = NULL ;//这个应用程序没有菜单,所以菜单名为NULL
wndclass.lpszClassName = szAppName ;//窗口类的名字,一般应和程序名字一样
RegisterClass (&wndclass) ;//这个函数用来注册窗口类
}
hwnd = CreateWindow (//该函数创建一个窗口实例
szAppName, // window class nameszAppName,
"Bouncing ball", // window caption
WS_OVERLAPPEDWINDOW|WS_VSCROLL, // window style
CW_USEDEFAULT, //初始化 x位置,用确省值
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
if(!SetTimer(hwnd,1,50,NULL))
{
MessageBox(hwnd,"Too many clocks or timers!",
szAppName,MB_ICONEXCLAMATION|MB_OK);
return FALSE;
}
ShowWindow (hwnd, nCmdShow) ;//显示hwnd标志的窗口, nCmdShow标志显示方式
UpdateWindow (hwnd) ;//显示窗口,发送WM—PAINT消息
while (GetMessage (&msg, NULL, 0, 0))//取得消息,除WM-QUIT外都返回TRUE
{
TranslateMessage (&msg) ;//对一些键盘消息进行翻译
DispatchMessage (&msg) ;//要求将消息传送给 在MSG结构中为窗口所指定的消息处理器
}
return msg.wParam ;//返回一个16位消息参数
}
LRESULT CALLBACK WndProc ( HWND hwnd, //窗口句柄
UINT message, //调用的消息
WPARAM wParam,//第一个消息,16位
LPARAM lParam)//第二个消息,32位
{
static HANDLE hBitmap;
static short cxClient ,cyClient,xCenter,yCenter,cxTotal,cyTotal,
cxRadius,cyRadius,cxMove,cyMove,xPixel,yPixel;
HBRUSH hBrush;
HDC hdc ,hdcMem;////设备句柄
short nScale ;
switch (message)//消息处理函数
{
case WM_CREATE:
hdc = GetDC (hwnd) ;//得到设备上下文,与下面的 ReleaseDC ()配套
xPixel=GetDeviceCaps(hdc,ASPECTX);/*此函数返回设备信息
//这里取回划一条线设备用的X—向象素单位
int GetDeviceCaps( HDC hdc, // handle to the device context
int nIndex // index of capability to query);
*/
yPixel=GetDeviceCaps(hdc,ASPECTY);
ReleaseDC (hwnd, hdc) ;//释放由hdc = GetDC (hwnd)得到的设备上下文
return 0 ;
case WM_SIZE://受到改变窗口尺寸的命令时,
xCenter=(cxClient = LOWORD (lParam))/2 ;
yCenter=(cyClient = HIWORD (lParam))/2 ;//指定客户区的高度,HIWORD是一个宏,
//检查lparam的高字节,返回32位系统中的客户区高度
nScale=min(cxClient*xPixel,cyClient*yPixel)/16;
cxRadius=nScale/xPixel;
cyRadius=nScale/yPixel;
cxMove=max(1,cxRadius/4);
cyMove=max(1,cyRadius/4);
cxTotal=2*(cxRadius+cxMove);
cyTotal=2*(cyRadius+cyMove);
if(hBitmap)
DeleteObject(hBitmap);
hdc=GetDC(hwnd);
hdcMem=CreateCompatibleDC(hdc);
hBitmap=CreateCompatibleBitmap(hdc,cxTotal,cyTotal);
/*根据设备上下文设置象素单位;原形:
HBITMAP CreateCompatibleBitmap( HDC hdc, // handle to device context
int nWidth, // width of bitmap, in pixels
int nHeight // height of bitmap, in pixels);
*/
ReleaseDC(hwnd,hdc);
SelectObject(hdcMem,hBitmap);
Rectangle(hdcMem,-1,-1,cxTotal+1,cyTotal+1);
hBrush=CreateHatchBrush(HS_DIAGCROSS,0L);
SelectObject(hdcMem,hBrush);
SetBkColor(hdcMem,RGB(255,0,0));
Ellipse(hdcMem,cxMove,cyMove,cxTotal-cxMove,
cyTotal-cyMove);
DeleteDC(hdcMem);
DeleteObject(hBrush);
return 0 ;
case WM_TIMER:
if(!hBitmap)
break;
hdc=GetDC(hwnd);
hdcMem=CreateCompatibleDC(hdc);//设置设备句柄
SelectObject(hdcMem,hBitmap);
BitBlt(hdc,xCenter-cxTotal/2,
yCenter-cyTotal/2,cxTotal,cyTotal,
hdcMem,0,0,SRCCOPY); //从源设备句柄向目标设备句柄转化矩形象素块
ReleaseDC(hwnd,hdc);
DeleteDC(hdcMem);
xCenter+=cxMove;
yCenter+=cyMove;
if((xCenter+cxRadius>=cxClient)||(xCenter-cxRadius<=0))
cxMove=-cxMove;
if((yCenter+cyRadius>=cyClient)||(yCenter-cyRadius<=0))
cyMove=-cyMove;
return 0 ;
case WM_DESTROY://用户点击了X按钮等消息
if(hBitmap)
DeleteObject(hBitmap);
KillTimer(hwnd,1);
PostQuitMessage (0) ;//告诉系统退出程序
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
12-3惨好答案:
// 12_3.cpp : Defines the class behaviors for the application. //
#include "stdafx.h"
#include "12_3.h"
#include "MainFrm.h"
#include "12_3Doc.h"
#include "12_3View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMy12_3App
BEGIN_MESSAGE_MAP(CMy12_3App, CWinApp)
//{{AFX_MSG_MAP(CMy12_3App)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup) END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy12_3App construction
CMy12_3App::CMy12_3App()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance }
/////////////////////////////////////////////////////////////////////////////
// The one and only CMy12_3App object
CMy12_3App theApp;
///////////////////////////////////////////////////////////////////////////// // CMy12_3App initialization
BOOL CMy12_3App::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL #else
Enable3dControlsStatic(); // Call this when linking to MFC statically #endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMy12_3Doc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CMy12_3View));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog {
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App command to run the dialog void CMy12_3App::OnAppAbout() {
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
///////////////////////////////////////////////////////////////////////////// // CMy12_3App message handlers
12-4参考答案:
// 12_4.cpp : Defines the class behaviors for the application. //
#include "stdafx.h"
#include "12_4.h"
#include "MainFrm.h"
#include "12_4Doc.h"
#include "12_4View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////////////// // CMy12_4App
BEGIN_MESSAGE_MAP(CMy12_4App, CWinApp)
//{{AFX_MSG_MAP(CMy12_4App)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup) END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CMy12_4App construction
CMy12_4App::CMy12_4App()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance }
///////////////////////////////////////////////////////////////////////////// // The one and only CMy12_4App object
CMy12_4App theApp;
///////////////////////////////////////////////////////////////////////////// // CMy12_4App initialization
BOOL CMy12_4App::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL #else
Enable3dControlsStatic(); // Call this when linking to MFC statically #endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMy12_4Doc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CMy12_4View));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About
class CAboutDlg : public CDialog {
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App command to run the dialog void CMy12_4App::OnAppAbout() {
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CMy12_4App message handlers
13-2参考答案:
// 13_2.cpp : Defines the class behaviors for the application. //
#include "stdafx.h"
#include "13_2.h"
#include "13_2Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////////////// // CMy13_2App
BEGIN_MESSAGE_MAP(CMy13_2App, CWinApp)
//{{AFX_MSG_MAP(CMy13_2App)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CMy13_2App construction
CMy13_2App::CMy13_2App()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance }
///////////////////////////////////////////////////////////////////////////// // The one and only CMy13_2App object
CMy13_2App theApp;
///////////////////////////////////////////////////////////////////////////// // CMy13_2App initialization
BOOL CMy13_2App::InitInstance()
{
// 初始化COM环境
if(FAILED(::CoInitialize(NULL)))
return FALSE;
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL #else
Enable3dControlsStatic(); // Call this when linking to MFC statically #endif
CMy13_2Dlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
int CMy13_2App::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
// 释放COM环境
::CoUninitialize();
return CWinApp::ExitInstance();
}
14-2参考答案:
// Ex1402.cpp : Defines the class behaviors for the application. //
#include "stdafx.h"
#include "Ex1402.h"
#include "Ex1402Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEx1402App
BEGIN_MESSAGE_MAP(CEx1402App, CWinApp)
//{{AFX_MSG_MAP(CEx1402App)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CEx1402App construction
CEx1402App::CEx1402App()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance }
///////////////////////////////////////////////////////////////////////////// // The one and only CEx1402App object
CEx1402App theApp;
///////////////////////////////////////////////////////////////////////////// // CEx1402App initialization
BOOL CEx1402App::InitInstance()
{
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL #else
Enable3dControlsStatic(); // Call this when linking to MFC statically #endif
CEx1402Dlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
14-3参考答案:
// 14_3.cpp : Defines the class behaviors for the application. //
#include "stdafx.h"
#include "14_3.h"
#include "14_3Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////////////// // CMy14_3App
BEGIN_MESSAGE_MAP(CMy14_3App, CWinApp)
//{{AFX_MSG_MAP(CMy14_3App)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CMy14_3App construction
CMy14_3App::CMy14_3App()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance }
///////////////////////////////////////////////////////////////////////////// // The one and only CMy14_3App object
CMy14_3App theApp;
///////////////////////////////////////////////////////////////////////////// // CMy14_3App initialization
BOOL CMy14_3App::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL #else
Enable3dControlsStatic(); // Call this when linking to MFC statically #endif
CMy14_3Dlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
14-4参考答案:
// Ex1404.cpp : Defines the class behaviors for the application. //
#include "stdafx.h"
#include "Ex1404.h"
#include "Ex1404Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////////////// // CEx1404App
BEGIN_MESSAGE_MAP(CEx1404App, CWinApp)
//{{AFX_MSG_MAP(CEx1404App)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CEx1404App construction
CEx1404App::CEx1404App()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance }
///////////////////////////////////////////////////////////////////////////// // The one and only CEx1404App object
CEx1404App theApp;
///////////////////////////////////////////////////////////////////////////// // CEx1404App initialization
BOOL CEx1404App::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL #else
Enable3dControlsStatic(); // Call this when linking to MFC statically #endif
CEx1404Dlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
本文档为【VisualC++课后习题参考答案】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。