C++面向对象简易图形编辑器.docx
《C++面向对象简易图形编辑器.docx》由会员分享,可在线阅读,更多相关《C++面向对象简易图形编辑器.docx(45页珍藏版)》请在冰点文库上搜索。
C++面向对象简易图形编辑器
面向对象编程技术
实验五实验报告
班级:
学号:
姓名:
时间:
2013-12-01
1实验要求1
A、基本功能要求1
B、高级要求1
2实验过程及结果1
2.1交互式绘制图形1
22绘制直线1
2.3绘制矩形2
2.4绘制椭圆3
2.5输入文本4
2.6文本保存6
2.7文本读取7
3实验总结8
附录关键代码8
一、主CPP8
二、附CPP22
三、头文件28
1实验要求
A、基本功能要求
1.交互式绘制图形(至少包括线、矩形和文本),绘制完成的图形能够正确地重现
2.绘制结果可以正确持久化(存储到文件、从文件中读出)//
3.最终交付的工程中至少包含两个源文件(.cpp文件,即除主程序外,将部分函数分离到单独的源文件卜一个rc文件
B、高级要求用“椭圆”
1.线、矩形、文本的颜色(线色和填充色)、文本字体可调。
绘制
2.能够通过鼠标点击选中图形,并进行属性的修改,修改的内容可包括:
大小
颜色
线型
字体(对应文本图元而言)
3.能够删除选中的图形
2实验过程及结果
2.1交互式绘制图形
此次绘制图形的程序和例程中EasyDraw不同,相比后者,本程序的绘制是建立在结构
体数组的基础上的。
绘制结果完全可以满足实验要求。
效果图如下:
wiEirawarrT亘笑⑺酊
请在空门畦单五取麻£犍首入文応风标
2.2绘制直线
首先定义一个直线的结构体数组
structMySaveLine//直线保存
{
POINTptS;
POINTptE;
COLORREFcolor;
intpenWidth;
intpenStyle;
};
绘制直线的函数如下:
voidDrawMyLine(HDChdc)//绘制直线
HPENhPen;
HPENhPenOld;
intj;
for(j=0;j{hPen=CreatePen(SaveLine[j].penStyle,SaveLine[j].penWidth,SaveLine[j].color);hPenOld=(HPEN)SelectObject(hdc,hPen);MoveToEx(hdc,SaveLine[j].ptS.x,SaveLine[j].ptS.y,NULL);LineTo(hdc,SaveLine[j].ptE.x,SaveLine[j].ptE.y);SelectObject(hdc,hPenOld);DeleteObject(hPen);}}2.3绘制矩形首先定义一个矩形的结构体数组structMySaveRectangle//矩形保存{POINTptS;POINTptE;COLORREFcolor;HBRUSHhNewBrush;intbrushColor;intpenWidth;intpenStyle;};绘制矩形的函数如下:voidDrawMyRectangle(HDChdc)//绘制矩形{intj;HBRUSHhOldBrush;HPENhPen;HPENhPenOld;for(j=0;j{hPen=CreatePen(SaveRectangle[j].penStyle,SaveRectangle[j].penWidth,SaveRectangle[j].color);hPenOld=(HPEN)SelectObject(hdc,hPen);switch(pDrawData->iSelectionBrush){caseIDM_BRUSH_WHITE:caseIDM_BRUSH_LTGRAY:hOldBrush=(HBRUSH)SelectObject(hdc,GetStockObject(SaveRectangle[j].brushColor));break;caseIDM_BRUSH_COLORSOLID:caseIDM_BRUSH_CROSS:hOldBrush=(HBRUSH)SelectObject(hdc,SaveRectangle[j].hNewBrush);break;}Rectangle(hdc,SaveRectangle[j].ptS.x,SaveRectangle[j].ptS.y,SaveRectangle[j].ptE.x,SaveRectangle[j].ptE.y);SelectObject(hdc,hPenOld);DeleteObject(hPen);SelectObject(hdc,hOldBrush);}}2.4绘制椭圆首先定义一个椭圆的结构体数组structMySaveEllipse//椭圆保存{POINTptS;POINTptE;COLORREFcolor;HBRUSHhNewBrush;intbrushColor;intpenWidth;intpenStyle;};绘制椭圆的函数如下:voidDrawMyEllipse(HDChdc)//绘制椭圆{intj;HBRUSHhOldBrush;HPENhPen;HPENhPenOld;for(j=0;j{hPen=CreatePen(SaveEllipse[j].penStyle,SaveEllipse[j].penWidth,SaveEllipse[j].color);hPenOld=(HPEN)SelectObject(hdc,hPen);switch(pDrawData->iSelectionBrush){caseIDM_BRUSH_WHITE:caseIDM_BRUSH_LTGRAY:hOldBrush=(HBRUSH)SelectObject(hdc,GetStockObject(SaveEllipse[j].brushColor));break;caseIDM_BRUSH_COLORSOLID:caseIDM_BRUSH_CROSS:hOldBrush=(HBRUSH)SelectObject(hdc,SaveEllipse[j].hNewBrush);break;}Ellipse(hdc,SaveEllipse[j].ptS.x,SaveEllipse[j].ptS.y,SaveEllipse[j].ptE.x,SaveEllipse[j].ptE.y);SelectObject(hdc,hPenOld);DeleteObject(hPen);SelectObject(hdc,hOldBrush);}}2.5输入文本输入文本前可以选择文本的字体、颜色,然后点击鼠标左键在该处输入文本。先定义一个文本的结构体数组structMySaveText//文本保存{POINTpt;charg_EditInput[1024];COLORREFcrText;HFONThFont;};选择字体的函数如下:voidChooseFont(HWNDhwnd){staticLOGFONTlogfont;staticCHOOSEFONTcf;staticHFONThFont;GetObject(GetStockObject(SYSTEM_FONT),sizeof(LOGFONT),SaveText[q].hFont=CreateFontlndirect(&logfont);InvalidateRect(hwnd,NULL,TRUE);}cc.lpTemplateName=NULL;if(ChooseColor(&cc)){SaveText[q].crText=crCustomColors[0];InvalidateRect(hwnd,NULL,TRUE);}}输出函数的文本如下:voidDrawMyText(HDChdc){intj;for(j=0;j{if(SaveText[j].hFont)SelectObject(hdc,SaveText[j].hFont);//选用逻辑字体SetTextColor(hdc,SaveText[j].crText);TextOut(hdc,SaveText[j].pt.x,SaveText[j].pt.y,SaveText[j].g_EditInput,strlen(SaveText[j].g_Editlnput));SelectObject(hdc,GetStockObject(SYSTEM_FONT));〃还原逻辑字体}}.2.6文本保存代码如下voidSaveMyData(structMySaveLine*SaveLine,intLineCount,structMySaveRectangle*SaveRectangle,intRectangleCount,structMySaveEllipse*SaveEllipse,intEllipseCount,structMySaveText*SaveText,intTextCount){staticHANDLEhFile;hFile=CreateFile("e:\\abc.f",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL);if(hFile!=INVALID_HANDLE_VALUE){DWORDdwByte仁0;DWORDdwByte2=0;DWORDdwByte3=0;DWORDdwByte4=0;WriteFile(hFile,&LineCount,sizeof(int),&dwBytel,NULL);WriteFile(hFile,&RectangleCount,sizeof(int),&dwByte2,NULL);WriteFile(hFile,&EllipseCount,sizeof(int),&dwByte3,NULL);WriteFile(hFile,&TextCount,sizeof(int),&dwByte4,NULL);WriteFile(hFile,SaveLine,LineCount*sizeof(MySaveLine),&dwByte1,NULL);WriteFile(hFile,SaveRectangle,RectangleCount*sizeof(MySaveRectangle),&dwByte2,NULL);WriteFile(hFile,SaveEllipse,EllipseCount*sizeof(MySaveEllipse),&dwByte3,NULL);WriteFile(hFile,SaveText,TextCount*sizeof(MySaveText),&dwByte4,NULL);CloseHandle(hFile);}}2.7文本读取代码如下:voidLoadMyData(structMySaveLine*SaveLine,intLineCount,structMySaveRectangle*SaveRectangle,intRectangleCount,structMySaveEllipse*SaveEllipse,intEllipseCount,structMySaveText*SaveText,intTextCount){staticHANDLEhFile;hFile=CreateFile("e:\\abc.f",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);if(hFile!=INVALID_HANDLE_VALUE){DWORDdwByte仁0;DWORDdwByte2=0;DWORDdwByte3=0;DWORDdwByte4=0;ReadFile(hFile,&LineCount,sizeof(int),&dwByte1,NULL);ReadFile(hFile,&RectangleCount,sizeof(int),&dwByte2,NULL);ReadFile(hFile,&EllipseCount,sizeof(int),&dwByte3,NULL);ReadFile(hFile,&TextCount,sizeof(int),&dwByte4,NULL);ReadFile(hFile,SaveLine,LineCount*sizeof(MySaveLine),&dwByte1,NULL);ReadFile(hFile,SaveRectangle,RectangleCount*sizeof(MySaveRectangle),&dwByte2,NULL);ReadFile(hFile,SaveEllipse,EllipseCount*sizeof(MySaveEllipse),&dwByte3,NULL);ReadFile(hFile,SaveText,TextCount*sizeof(MySaveText),&dwByte4,NULL);CloseHandle(hFile);}}3实验总结本实验最大的特点是使用结构体数组,这也是我第一次使用结构体数组,从不使用结构体到使用结构体指针最后升级到结构体数组是一个循序渐进的过程,在这个过程中我越来越感觉到结构体在管理数据方面的优势。我的感觉是在主CPP文件中代码应该是越少越好,很多地方只要调用函数就可以了,然后在另外的CPP中写函数。这样的程序给人的感觉是分层做的比较好,调理比较清晰。附录关键代码一、主CPP/*******************************************************************程序:Paint文件:Paint.cpp功能:简易的图像编辑器********************************************************************/#include"draw.h"#defineMAX100TCHARszAppName[];structMySaveLineSaveLine[MAX];structMySaveRectangleSaveRectangle[MAX];structMySaveEllipseSaveEllipse[MAX];structMySaveTextSaveText[MAX];intlineCount;intrectangleCount;intellipseCount;inttextCount;DRAWDATA*pDrawData;HACCELhAccel; HDChdc;inti,m,n,p,q;BOOLInitWindow(HINSTANCEhlnstanee,intnCmdShow);LRESULTCALLBACKWinProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMIParam);/*******************************************************************函数:WinMain()功能:Win32应用程序入口函数。创建主窗口,处理消息循环********************************************************************/intWINAPIWinMain(HINSTANCEhlnstanee,HINSTANCEhPrevInstanee,PSTRszCmdLine,intiCmdShow){MSGmsg;char*p;//当前实例句柄//前一个实例句柄//命令行字符//窗口显示方式〃创建主窗口if(!InitWindow(hInstanee,iCmdShow))returnFALSE;p=0;//进入消息循环:从该应用程序的消息队列中检取消息,〃送到消息处理过程,当检取到WM_QUIT消息时,退出消息循环。while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&msg);}returnmsg.wParam;}/******************************************************************函数:InitWindow()功能:创建窗口。*******************************************************************/staticBOOLInitWindow(HINSTANCEhlnstanee,intiCmdShow){HWNDhwnd;//窗口句柄 //窗口类结构WNDCLASSwcMainWnd;ATOMa;〃填充窗口类结构wcMainWnd.style=CS_VREDRAW|CS_HREDRAW;wcMainWnd.lpfnWndProc=WinProc;wcMainWnd.cbCIsExtra=0;wcMainWnd.cbWndExtra=0;wcMainWnd.hInstanee=hlnstanee;wcMainWnd.hlcon=Loadlcon(hlnstanee,MAKEINTRESOURCE(IDI_ICON1));wcMainWnd.hCursor=LoadCursor(NULL,IDC_ARROW);wcMainWnd.hbrBackground=(structHBRUSH__*)GetStockObject(WHITE_BRUSH);wcMainWnd.lpszMenuName=(LPSTR)IDR_MENU1;;wcMainWnd.lpszCIassName=szAppName;LoadAcceIerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1));hAccel=LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1));〃注册窗口类a=RegisterClass(&wcMainWnd);if(!a){MessageBox(NULL,TEXT("注册窗口类失败!"),szAppName,MB_ICONERROR);return0;}〃创建主窗口hwnd=CreateWindow(szAppName,TEXT("简易的图形编辑器WS_OVERLAPPEDWINDOW,200,200,800,450,NULL,NULL,hlnstanee,NULL);if(!hwnd)returnFALSE;〃显示并更新窗口ShowWindow(hwnd,iCmdShow);UpdateWindow(hwnd);returnTRUE;}/******************************************************************函数:WinProc()功能:处理主窗口消息*******************************************************************/LRESULTCALLBACKWinProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMIParam){PAINTSTRUCTps;RECTrect;HMENUhMenu;bDrawing=FALSE,Line=FALSE,staticBOOLRectangle=FALSE,Ellipse=FALSE,Text=FALSE;staticHMENUhPopMenu;POINTpt;inthNewPenStyle,hNewPenWidth;intBrushColor;COLORREFhNewPenColor;staticintx,y;staticchara[100];HBRUSHhNewBrush;staticintk=1;staticHWNDhWndEdit;caseWMCREATE://创建消息pDrawData=newDRAWDATA;pDrawData->iSelectionFunc=IDM_FUNC_LINETO;pDrawData->iSelectionPenColor=IDM_PEN_BLACK;pDrawData->iSelectionPenStyle=IDM_PEN_SOLID;pDrawData->iSelectionPenWidth=IDM_PEN_WIDTH1;if(pDrawData){SetWindowLong(hWnd,0,(LONG)pDrawData);}〃初始
hPen=CreatePen(SaveLine[j].penStyle,SaveLine[j].penWidth,SaveLine[j].color);hPenOld=(HPEN)SelectObject(hdc,hPen);
MoveToEx(hdc,SaveLine[j].ptS.x,SaveLine[j].ptS.y,NULL);
LineTo(hdc,SaveLine[j].ptE.x,SaveLine[j].ptE.y);
SelectObject(hdc,hPenOld);
DeleteObject(hPen);
}
2.3绘制矩形
首先定义一个矩形的结构体数组
structMySaveRectangle//矩形保存
HBRUSHhNewBrush;
intbrushColor;
绘制矩形的函数如下:
voidDrawMyRectangle(HDChdc)//绘制矩形
HBRUSHhOldBrush;
for(j=0;j{hPen=CreatePen(SaveRectangle[j].penStyle,SaveRectangle[j].penWidth,SaveRectangle[j].color);hPenOld=(HPEN)SelectObject(hdc,hPen);switch(pDrawData->iSelectionBrush){caseIDM_BRUSH_WHITE:caseIDM_BRUSH_LTGRAY:hOldBrush=(HBRUSH)SelectObject(hdc,GetStockObject(SaveRectangle[j].brushColor));break;caseIDM_BRUSH_COLORSOLID:caseIDM_BRUSH_CROSS:hOldBrush=(HBRUSH)SelectObject(hdc,SaveRectangle[j].hNewBrush);break;}Rectangle(hdc,SaveRectangle[j].ptS.x,SaveRectangle[j].ptS.y,SaveRectangle[j].ptE.x,SaveRectangle[j].ptE.y);SelectObject(hdc,hPenOld);DeleteObject(hPen);SelectObject(hdc,hOldBrush);}}2.4绘制椭圆首先定义一个椭圆的结构体数组structMySaveEllipse//椭圆保存{POINTptS;POINTptE;COLORREFcolor;HBRUSHhNewBrush;intbrushColor;intpenWidth;intpenStyle;};绘制椭圆的函数如下:voidDrawMyEllipse(HDChdc)//绘制椭圆{intj;HBRUSHhOldBrush;HPENhPen;HPENhPenOld;for(j=0;j{hPen=CreatePen(SaveEllipse[j].penStyle,SaveEllipse[j].penWidth,SaveEllipse[j].color);hPenOld=(HPEN)SelectObject(hdc,hPen);switch(pDrawData->iSelectionBrush){caseIDM_BRUSH_WHITE:caseIDM_BRUSH_LTGRAY:hOldBrush=(HBRUSH)SelectObject(hdc,GetStockObject(SaveEllipse[j].brushColor));break;caseIDM_BRUSH_COLORSOLID:caseIDM_BRUSH_CROSS:hOldBrush=(HBRUSH)SelectObject(hdc,SaveEllipse[j].hNewBrush);break;}Ellipse(hdc,SaveEllipse[j].ptS.x,SaveEllipse[j].ptS.y,SaveEllipse[j].ptE.x,SaveEllipse[j].ptE.y);SelectObject(hdc,hPenOld);DeleteObject(hPen);SelectObject(hdc,hOldBrush);}}2.5输入文本输入文本前可以选择文本的字体、颜色,然后点击鼠标左键在该处输入文本。先定义一个文本的结构体数组structMySaveText//文本保存{POINTpt;charg_EditInput[1024];COLORREFcrText;HFONThFont;};选择字体的函数如下:voidChooseFont(HWNDhwnd){staticLOGFONTlogfont;staticCHOOSEFONTcf;staticHFONThFont;GetObject(GetStockObject(SYSTEM_FONT),sizeof(LOGFONT),SaveText[q].hFont=CreateFontlndirect(&logfont);InvalidateRect(hwnd,NULL,TRUE);}cc.lpTemplateName=NULL;if(ChooseColor(&cc)){SaveText[q].crText=crCustomColors[0];InvalidateRect(hwnd,NULL,TRUE);}}输出函数的文本如下:voidDrawMyText(HDChdc){intj;for(j=0;j{if(SaveText[j].hFont)SelectObject(hdc,SaveText[j].hFont);//选用逻辑字体SetTextColor(hdc,SaveText[j].crText);TextOut(hdc,SaveText[j].pt.x,SaveText[j].pt.y,SaveText[j].g_EditInput,strlen(SaveText[j].g_Editlnput));SelectObject(hdc,GetStockObject(SYSTEM_FONT));〃还原逻辑字体}}.2.6文本保存代码如下voidSaveMyData(structMySaveLine*SaveLine,intLineCount,structMySaveRectangle*SaveRectangle,intRectangleCount,structMySaveEllipse*SaveEllipse,intEllipseCount,structMySaveText*SaveText,intTextCount){staticHANDLEhFile;hFile=CreateFile("e:\\abc.f",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL);if(hFile!=INVALID_HANDLE_VALUE){DWORDdwByte仁0;DWORDdwByte2=0;DWORDdwByte3=0;DWORDdwByte4=0;WriteFile(hFile,&LineCount,sizeof(int),&dwBytel,NULL);WriteFile(hFile,&RectangleCount,sizeof(int),&dwByte2,NULL);WriteFile(hFile,&EllipseCount,sizeof(int),&dwByte3,NULL);WriteFile(hFile,&TextCount,sizeof(int),&dwByte4,NULL);WriteFile(hFile,SaveLine,LineCount*sizeof(MySaveLine),&dwByte1,NULL);WriteFile(hFile,SaveRectangle,RectangleCount*sizeof(MySaveRectangle),&dwByte2,NULL);WriteFile(hFile,SaveEllipse,EllipseCount*sizeof(MySaveEllipse),&dwByte3,NULL);WriteFile(hFile,SaveText,TextCount*sizeof(MySaveText),&dwByte4,NULL);CloseHandle(hFile);}}2.7文本读取代码如下:voidLoadMyData(structMySaveLine*SaveLine,intLineCount,structMySaveRectangle*SaveRectangle,intRectangleCount,structMySaveEllipse*SaveEllipse,intEllipseCount,structMySaveText*SaveText,intTextCount){staticHANDLEhFile;hFile=CreateFile("e:\\abc.f",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);if(hFile!=INVALID_HANDLE_VALUE){DWORDdwByte仁0;DWORDdwByte2=0;DWORDdwByte3=0;DWORDdwByte4=0;ReadFile(hFile,&LineCount,sizeof(int),&dwByte1,NULL);ReadFile(hFile,&RectangleCount,sizeof(int),&dwByte2,NULL);ReadFile(hFile,&EllipseCount,sizeof(int),&dwByte3,NULL);ReadFile(hFile,&TextCount,sizeof(int),&dwByte4,NULL);ReadFile(hFile,SaveLine,LineCount*sizeof(MySaveLine),&dwByte1,NULL);ReadFile(hFile,SaveRectangle,RectangleCount*sizeof(MySaveRectangle),&dwByte2,NULL);ReadFile(hFile,SaveEllipse,EllipseCount*sizeof(MySaveEllipse),&dwByte3,NULL);ReadFile(hFile,SaveText,TextCount*sizeof(MySaveText),&dwByte4,NULL);CloseHandle(hFile);}}3实验总结本实验最大的特点是使用结构体数组,这也是我第一次使用结构体数组,从不使用结构体到使用结构体指针最后升级到结构体数组是一个循序渐进的过程,在这个过程中我越来越感觉到结构体在管理数据方面的优势。我的感觉是在主CPP文件中代码应该是越少越好,很多地方只要调用函数就可以了,然后在另外的CPP中写函数。这样的程序给人的感觉是分层做的比较好,调理比较清晰。附录关键代码一、主CPP/*******************************************************************程序:Paint文件:Paint.cpp功能:简易的图像编辑器********************************************************************/#include"draw.h"#defineMAX100TCHARszAppName[];structMySaveLineSaveLine[MAX];structMySaveRectangleSaveRectangle[MAX];structMySaveEllipseSaveEllipse[MAX];structMySaveTextSaveText[MAX];intlineCount;intrectangleCount;intellipseCount;inttextCount;DRAWDATA*pDrawData;HACCELhAccel; HDChdc;inti,m,n,p,q;BOOLInitWindow(HINSTANCEhlnstanee,intnCmdShow);LRESULTCALLBACKWinProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMIParam);/*******************************************************************函数:WinMain()功能:Win32应用程序入口函数。创建主窗口,处理消息循环********************************************************************/intWINAPIWinMain(HINSTANCEhlnstanee,HINSTANCEhPrevInstanee,PSTRszCmdLine,intiCmdShow){MSGmsg;char*p;//当前实例句柄//前一个实例句柄//命令行字符//窗口显示方式〃创建主窗口if(!InitWindow(hInstanee,iCmdShow))returnFALSE;p=0;//进入消息循环:从该应用程序的消息队列中检取消息,〃送到消息处理过程,当检取到WM_QUIT消息时,退出消息循环。while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&msg);}returnmsg.wParam;}/******************************************************************函数:InitWindow()功能:创建窗口。*******************************************************************/staticBOOLInitWindow(HINSTANCEhlnstanee,intiCmdShow){HWNDhwnd;//窗口句柄 //窗口类结构WNDCLASSwcMainWnd;ATOMa;〃填充窗口类结构wcMainWnd.style=CS_VREDRAW|CS_HREDRAW;wcMainWnd.lpfnWndProc=WinProc;wcMainWnd.cbCIsExtra=0;wcMainWnd.cbWndExtra=0;wcMainWnd.hInstanee=hlnstanee;wcMainWnd.hlcon=Loadlcon(hlnstanee,MAKEINTRESOURCE(IDI_ICON1));wcMainWnd.hCursor=LoadCursor(NULL,IDC_ARROW);wcMainWnd.hbrBackground=(structHBRUSH__*)GetStockObject(WHITE_BRUSH);wcMainWnd.lpszMenuName=(LPSTR)IDR_MENU1;;wcMainWnd.lpszCIassName=szAppName;LoadAcceIerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1));hAccel=LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1));〃注册窗口类a=RegisterClass(&wcMainWnd);if(!a){MessageBox(NULL,TEXT("注册窗口类失败!"),szAppName,MB_ICONERROR);return0;}〃创建主窗口hwnd=CreateWindow(szAppName,TEXT("简易的图形编辑器WS_OVERLAPPEDWINDOW,200,200,800,450,NULL,NULL,hlnstanee,NULL);if(!hwnd)returnFALSE;〃显示并更新窗口ShowWindow(hwnd,iCmdShow);UpdateWindow(hwnd);returnTRUE;}/******************************************************************函数:WinProc()功能:处理主窗口消息*******************************************************************/LRESULTCALLBACKWinProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMIParam){PAINTSTRUCTps;RECTrect;HMENUhMenu;bDrawing=FALSE,Line=FALSE,staticBOOLRectangle=FALSE,Ellipse=FALSE,Text=FALSE;staticHMENUhPopMenu;POINTpt;inthNewPenStyle,hNewPenWidth;intBrushColor;COLORREFhNewPenColor;staticintx,y;staticchara[100];HBRUSHhNewBrush;staticintk=1;staticHWNDhWndEdit;caseWMCREATE://创建消息pDrawData=newDRAWDATA;pDrawData->iSelectionFunc=IDM_FUNC_LINETO;pDrawData->iSelectionPenColor=IDM_PEN_BLACK;pDrawData->iSelectionPenStyle=IDM_PEN_SOLID;pDrawData->iSelectionPenWidth=IDM_PEN_WIDTH1;if(pDrawData){SetWindowLong(hWnd,0,(LONG)pDrawData);}〃初始
hPen=CreatePen(SaveRectangle[j].penStyle,SaveRectangle[j].penWidth,
SaveRectangle[j].color);
hPenOld=(HPEN)SelectObject(hdc,hPen);
switch(pDrawData->iSelectionBrush)
caseIDM_BRUSH_WHITE:
caseIDM_BRUSH_LTGRAY:
hOldBrush=(HBRUSH)SelectObject(hdc,
GetStockObject(SaveRectangle[j].brushColor));
break;
caseIDM_BRUSH_COLORSOLID:
caseIDM_BRUSH_CROSS:
hOldBrush=(HBRUSH)SelectObject(hdc,SaveRectangle[j].hNewBrush);break;
Rectangle(hdc,SaveRectangle[j].ptS.x,SaveRectangle[j].ptS.y,
SaveRectangle[j].ptE.x,SaveRectangle[j].ptE.y);
SelectObject(hdc,hOldBrush);
2.4绘制椭圆
首先定义一个椭圆的结构体数组
structMySaveEllipse//椭圆保存
绘制椭圆的函数如下:
voidDrawMyEllipse(HDChdc)//绘制椭圆
for(j=0;j{hPen=CreatePen(SaveEllipse[j].penStyle,SaveEllipse[j].penWidth,SaveEllipse[j].color);hPenOld=(HPEN)SelectObject(hdc,hPen);switch(pDrawData->iSelectionBrush){caseIDM_BRUSH_WHITE:caseIDM_BRUSH_LTGRAY:hOldBrush=(HBRUSH)SelectObject(hdc,GetStockObject(SaveEllipse[j].brushColor));break;caseIDM_BRUSH_COLORSOLID:caseIDM_BRUSH_CROSS:hOldBrush=(HBRUSH)SelectObject(hdc,SaveEllipse[j].hNewBrush);break;}Ellipse(hdc,SaveEllipse[j].ptS.x,SaveEllipse[j].ptS.y,SaveEllipse[j].ptE.x,SaveEllipse[j].ptE.y);SelectObject(hdc,hPenOld);DeleteObject(hPen);SelectObject(hdc,hOldBrush);}}2.5输入文本输入文本前可以选择文本的字体、颜色,然后点击鼠标左键在该处输入文本。先定义一个文本的结构体数组structMySaveText//文本保存{POINTpt;charg_EditInput[1024];COLORREFcrText;HFONThFont;};选择字体的函数如下:voidChooseFont(HWNDhwnd){staticLOGFONTlogfont;staticCHOOSEFONTcf;staticHFONThFont;GetObject(GetStockObject(SYSTEM_FONT),sizeof(LOGFONT),SaveText[q].hFont=CreateFontlndirect(&logfont);InvalidateRect(hwnd,NULL,TRUE);}cc.lpTemplateName=NULL;if(ChooseColor(&cc)){SaveText[q].crText=crCustomColors[0];InvalidateRect(hwnd,NULL,TRUE);}}输出函数的文本如下:voidDrawMyText(HDChdc){intj;for(j=0;j{if(SaveText[j].hFont)SelectObject(hdc,SaveText[j].hFont);//选用逻辑字体SetTextColor(hdc,SaveText[j].crText);TextOut(hdc,SaveText[j].pt.x,SaveText[j].pt.y,SaveText[j].g_EditInput,strlen(SaveText[j].g_Editlnput));SelectObject(hdc,GetStockObject(SYSTEM_FONT));〃还原逻辑字体}}.2.6文本保存代码如下voidSaveMyData(structMySaveLine*SaveLine,intLineCount,structMySaveRectangle*SaveRectangle,intRectangleCount,structMySaveEllipse*SaveEllipse,intEllipseCount,structMySaveText*SaveText,intTextCount){staticHANDLEhFile;hFile=CreateFile("e:\\abc.f",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL);if(hFile!=INVALID_HANDLE_VALUE){DWORDdwByte仁0;DWORDdwByte2=0;DWORDdwByte3=0;DWORDdwByte4=0;WriteFile(hFile,&LineCount,sizeof(int),&dwBytel,NULL);WriteFile(hFile,&RectangleCount,sizeof(int),&dwByte2,NULL);WriteFile(hFile,&EllipseCount,sizeof(int),&dwByte3,NULL);WriteFile(hFile,&TextCount,sizeof(int),&dwByte4,NULL);WriteFile(hFile,SaveLine,LineCount*sizeof(MySaveLine),&dwByte1,NULL);WriteFile(hFile,SaveRectangle,RectangleCount*sizeof(MySaveRectangle),&dwByte2,NULL);WriteFile(hFile,SaveEllipse,EllipseCount*sizeof(MySaveEllipse),&dwByte3,NULL);WriteFile(hFile,SaveText,TextCount*sizeof(MySaveText),&dwByte4,NULL);CloseHandle(hFile);}}2.7文本读取代码如下:voidLoadMyData(structMySaveLine*SaveLine,intLineCount,structMySaveRectangle*SaveRectangle,intRectangleCount,structMySaveEllipse*SaveEllipse,intEllipseCount,structMySaveText*SaveText,intTextCount){staticHANDLEhFile;hFile=CreateFile("e:\\abc.f",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);if(hFile!=INVALID_HANDLE_VALUE){DWORDdwByte仁0;DWORDdwByte2=0;DWORDdwByte3=0;DWORDdwByte4=0;ReadFile(hFile,&LineCount,sizeof(int),&dwByte1,NULL);ReadFile(hFile,&RectangleCount,sizeof(int),&dwByte2,NULL);ReadFile(hFile,&EllipseCount,sizeof(int),&dwByte3,NULL);ReadFile(hFile,&TextCount,sizeof(int),&dwByte4,NULL);ReadFile(hFile,SaveLine,LineCount*sizeof(MySaveLine),&dwByte1,NULL);ReadFile(hFile,SaveRectangle,RectangleCount*sizeof(MySaveRectangle),&dwByte2,NULL);ReadFile(hFile,SaveEllipse,EllipseCount*sizeof(MySaveEllipse),&dwByte3,NULL);ReadFile(hFile,SaveText,TextCount*sizeof(MySaveText),&dwByte4,NULL);CloseHandle(hFile);}}3实验总结本实验最大的特点是使用结构体数组,这也是我第一次使用结构体数组,从不使用结构体到使用结构体指针最后升级到结构体数组是一个循序渐进的过程,在这个过程中我越来越感觉到结构体在管理数据方面的优势。我的感觉是在主CPP文件中代码应该是越少越好,很多地方只要调用函数就可以了,然后在另外的CPP中写函数。这样的程序给人的感觉是分层做的比较好,调理比较清晰。附录关键代码一、主CPP/*******************************************************************程序:Paint文件:Paint.cpp功能:简易的图像编辑器********************************************************************/#include"draw.h"#defineMAX100TCHARszAppName[];structMySaveLineSaveLine[MAX];structMySaveRectangleSaveRectangle[MAX];structMySaveEllipseSaveEllipse[MAX];structMySaveTextSaveText[MAX];intlineCount;intrectangleCount;intellipseCount;inttextCount;DRAWDATA*pDrawData;HACCELhAccel; HDChdc;inti,m,n,p,q;BOOLInitWindow(HINSTANCEhlnstanee,intnCmdShow);LRESULTCALLBACKWinProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMIParam);/*******************************************************************函数:WinMain()功能:Win32应用程序入口函数。创建主窗口,处理消息循环********************************************************************/intWINAPIWinMain(HINSTANCEhlnstanee,HINSTANCEhPrevInstanee,PSTRszCmdLine,intiCmdShow){MSGmsg;char*p;//当前实例句柄//前一个实例句柄//命令行字符//窗口显示方式〃创建主窗口if(!InitWindow(hInstanee,iCmdShow))returnFALSE;p=0;//进入消息循环:从该应用程序的消息队列中检取消息,〃送到消息处理过程,当检取到WM_QUIT消息时,退出消息循环。while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&msg);}returnmsg.wParam;}/******************************************************************函数:InitWindow()功能:创建窗口。*******************************************************************/staticBOOLInitWindow(HINSTANCEhlnstanee,intiCmdShow){HWNDhwnd;//窗口句柄 //窗口类结构WNDCLASSwcMainWnd;ATOMa;〃填充窗口类结构wcMainWnd.style=CS_VREDRAW|CS_HREDRAW;wcMainWnd.lpfnWndProc=WinProc;wcMainWnd.cbCIsExtra=0;wcMainWnd.cbWndExtra=0;wcMainWnd.hInstanee=hlnstanee;wcMainWnd.hlcon=Loadlcon(hlnstanee,MAKEINTRESOURCE(IDI_ICON1));wcMainWnd.hCursor=LoadCursor(NULL,IDC_ARROW);wcMainWnd.hbrBackground=(structHBRUSH__*)GetStockObject(WHITE_BRUSH);wcMainWnd.lpszMenuName=(LPSTR)IDR_MENU1;;wcMainWnd.lpszCIassName=szAppName;LoadAcceIerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1));hAccel=LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1));〃注册窗口类a=RegisterClass(&wcMainWnd);if(!a){MessageBox(NULL,TEXT("注册窗口类失败!"),szAppName,MB_ICONERROR);return0;}〃创建主窗口hwnd=CreateWindow(szAppName,TEXT("简易的图形编辑器WS_OVERLAPPEDWINDOW,200,200,800,450,NULL,NULL,hlnstanee,NULL);if(!hwnd)returnFALSE;〃显示并更新窗口ShowWindow(hwnd,iCmdShow);UpdateWindow(hwnd);returnTRUE;}/******************************************************************函数:WinProc()功能:处理主窗口消息*******************************************************************/LRESULTCALLBACKWinProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMIParam){PAINTSTRUCTps;RECTrect;HMENUhMenu;bDrawing=FALSE,Line=FALSE,staticBOOLRectangle=FALSE,Ellipse=FALSE,Text=FALSE;staticHMENUhPopMenu;POINTpt;inthNewPenStyle,hNewPenWidth;intBrushColor;COLORREFhNewPenColor;staticintx,y;staticchara[100];HBRUSHhNewBrush;staticintk=1;staticHWNDhWndEdit;caseWMCREATE://创建消息pDrawData=newDRAWDATA;pDrawData->iSelectionFunc=IDM_FUNC_LINETO;pDrawData->iSelectionPenColor=IDM_PEN_BLACK;pDrawData->iSelectionPenStyle=IDM_PEN_SOLID;pDrawData->iSelectionPenWidth=IDM_PEN_WIDTH1;if(pDrawData){SetWindowLong(hWnd,0,(LONG)pDrawData);}〃初始
hPen=CreatePen(SaveEllipse[j].penStyle,SaveEllipse[j].penWidth,
SaveEllipse[j].color);
GetStockObject(SaveEllipse[j].brushColor));
hOldBrush=(HBRUSH)SelectObject(hdc,SaveEllipse[j].hNewBrush);
Ellipse(hdc,SaveEllipse[j].ptS.x,SaveEllipse[j].ptS.y,
SaveEllipse[j].ptE.x,SaveEllipse[j].ptE.y);
2.5输入文本
输入文本前可以选择文本的字体、颜色,然后点击鼠标左键在该处输入文本。
先定义一个文本的结构体数组
structMySaveText//文本保存
POINTpt;
charg_EditInput[1024];
COLORREFcrText;
HFONThFont;
选择字体的函数如下:
voidChooseFont(HWNDhwnd)
staticLOGFONTlogfont;
staticCHOOSEFONTcf;
staticHFONThFont;
GetObject(GetStockObject(SYSTEM_FONT),sizeof(LOGFONT),
SaveText[q].hFont=CreateFontlndirect(&logfont);
InvalidateRect(hwnd,NULL,TRUE);
cc.lpTemplateName=NULL;
if(ChooseColor(&cc))
SaveText[q].crText=crCustomColors[0];
输出函数的文本如下:
voidDrawMyText(HDChdc)
for(j=0;j{if(SaveText[j].hFont)SelectObject(hdc,SaveText[j].hFont);//选用逻辑字体SetTextColor(hdc,SaveText[j].crText);TextOut(hdc,SaveText[j].pt.x,SaveText[j].pt.y,SaveText[j].g_EditInput,strlen(SaveText[j].g_Editlnput));SelectObject(hdc,GetStockObject(SYSTEM_FONT));〃还原逻辑字体}}.2.6文本保存代码如下voidSaveMyData(structMySaveLine*SaveLine,intLineCount,structMySaveRectangle*SaveRectangle,intRectangleCount,structMySaveEllipse*SaveEllipse,intEllipseCount,structMySaveText*SaveText,intTextCount){staticHANDLEhFile;hFile=CreateFile("e:\\abc.f",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL);if(hFile!=INVALID_HANDLE_VALUE){DWORDdwByte仁0;DWORDdwByte2=0;DWORDdwByte3=0;DWORDdwByte4=0;WriteFile(hFile,&LineCount,sizeof(int),&dwBytel,NULL);WriteFile(hFile,&RectangleCount,sizeof(int),&dwByte2,NULL);WriteFile(hFile,&EllipseCount,sizeof(int),&dwByte3,NULL);WriteFile(hFile,&TextCount,sizeof(int),&dwByte4,NULL);WriteFile(hFile,SaveLine,LineCount*sizeof(MySaveLine),&dwByte1,NULL);WriteFile(hFile,SaveRectangle,RectangleCount*sizeof(MySaveRectangle),&dwByte2,NULL);WriteFile(hFile,SaveEllipse,EllipseCount*sizeof(MySaveEllipse),&dwByte3,NULL);WriteFile(hFile,SaveText,TextCount*sizeof(MySaveText),&dwByte4,NULL);CloseHandle(hFile);}}2.7文本读取代码如下:voidLoadMyData(structMySaveLine*SaveLine,intLineCount,structMySaveRectangle*SaveRectangle,intRectangleCount,structMySaveEllipse*SaveEllipse,intEllipseCount,structMySaveText*SaveText,intTextCount){staticHANDLEhFile;hFile=CreateFile("e:\\abc.f",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);if(hFile!=INVALID_HANDLE_VALUE){DWORDdwByte仁0;DWORDdwByte2=0;DWORDdwByte3=0;DWORDdwByte4=0;ReadFile(hFile,&LineCount,sizeof(int),&dwByte1,NULL);ReadFile(hFile,&RectangleCount,sizeof(int),&dwByte2,NULL);ReadFile(hFile,&EllipseCount,sizeof(int),&dwByte3,NULL);ReadFile(hFile,&TextCount,sizeof(int),&dwByte4,NULL);ReadFile(hFile,SaveLine,LineCount*sizeof(MySaveLine),&dwByte1,NULL);ReadFile(hFile,SaveRectangle,RectangleCount*sizeof(MySaveRectangle),&dwByte2,NULL);ReadFile(hFile,SaveEllipse,EllipseCount*sizeof(MySaveEllipse),&dwByte3,NULL);ReadFile(hFile,SaveText,TextCount*sizeof(MySaveText),&dwByte4,NULL);CloseHandle(hFile);}}3实验总结本实验最大的特点是使用结构体数组,这也是我第一次使用结构体数组,从不使用结构体到使用结构体指针最后升级到结构体数组是一个循序渐进的过程,在这个过程中我越来越感觉到结构体在管理数据方面的优势。我的感觉是在主CPP文件中代码应该是越少越好,很多地方只要调用函数就可以了,然后在另外的CPP中写函数。这样的程序给人的感觉是分层做的比较好,调理比较清晰。附录关键代码一、主CPP/*******************************************************************程序:Paint文件:Paint.cpp功能:简易的图像编辑器********************************************************************/#include"draw.h"#defineMAX100TCHARszAppName[];structMySaveLineSaveLine[MAX];structMySaveRectangleSaveRectangle[MAX];structMySaveEllipseSaveEllipse[MAX];structMySaveTextSaveText[MAX];intlineCount;intrectangleCount;intellipseCount;inttextCount;DRAWDATA*pDrawData;HACCELhAccel; HDChdc;inti,m,n,p,q;BOOLInitWindow(HINSTANCEhlnstanee,intnCmdShow);LRESULTCALLBACKWinProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMIParam);/*******************************************************************函数:WinMain()功能:Win32应用程序入口函数。创建主窗口,处理消息循环********************************************************************/intWINAPIWinMain(HINSTANCEhlnstanee,HINSTANCEhPrevInstanee,PSTRszCmdLine,intiCmdShow){MSGmsg;char*p;//当前实例句柄//前一个实例句柄//命令行字符//窗口显示方式〃创建主窗口if(!InitWindow(hInstanee,iCmdShow))returnFALSE;p=0;//进入消息循环:从该应用程序的消息队列中检取消息,〃送到消息处理过程,当检取到WM_QUIT消息时,退出消息循环。while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&msg);}returnmsg.wParam;}/******************************************************************函数:InitWindow()功能:创建窗口。*******************************************************************/staticBOOLInitWindow(HINSTANCEhlnstanee,intiCmdShow){HWNDhwnd;//窗口句柄 //窗口类结构WNDCLASSwcMainWnd;ATOMa;〃填充窗口类结构wcMainWnd.style=CS_VREDRAW|CS_HREDRAW;wcMainWnd.lpfnWndProc=WinProc;wcMainWnd.cbCIsExtra=0;wcMainWnd.cbWndExtra=0;wcMainWnd.hInstanee=hlnstanee;wcMainWnd.hlcon=Loadlcon(hlnstanee,MAKEINTRESOURCE(IDI_ICON1));wcMainWnd.hCursor=LoadCursor(NULL,IDC_ARROW);wcMainWnd.hbrBackground=(structHBRUSH__*)GetStockObject(WHITE_BRUSH);wcMainWnd.lpszMenuName=(LPSTR)IDR_MENU1;;wcMainWnd.lpszCIassName=szAppName;LoadAcceIerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1));hAccel=LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1));〃注册窗口类a=RegisterClass(&wcMainWnd);if(!a){MessageBox(NULL,TEXT("注册窗口类失败!"),szAppName,MB_ICONERROR);return0;}〃创建主窗口hwnd=CreateWindow(szAppName,TEXT("简易的图形编辑器WS_OVERLAPPEDWINDOW,200,200,800,450,NULL,NULL,hlnstanee,NULL);if(!hwnd)returnFALSE;〃显示并更新窗口ShowWindow(hwnd,iCmdShow);UpdateWindow(hwnd);returnTRUE;}/******************************************************************函数:WinProc()功能:处理主窗口消息*******************************************************************/LRESULTCALLBACKWinProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMIParam){PAINTSTRUCTps;RECTrect;HMENUhMenu;bDrawing=FALSE,Line=FALSE,staticBOOLRectangle=FALSE,Ellipse=FALSE,Text=FALSE;staticHMENUhPopMenu;POINTpt;inthNewPenStyle,hNewPenWidth;intBrushColor;COLORREFhNewPenColor;staticintx,y;staticchara[100];HBRUSHhNewBrush;staticintk=1;staticHWNDhWndEdit;caseWMCREATE://创建消息pDrawData=newDRAWDATA;pDrawData->iSelectionFunc=IDM_FUNC_LINETO;pDrawData->iSelectionPenColor=IDM_PEN_BLACK;pDrawData->iSelectionPenStyle=IDM_PEN_SOLID;pDrawData->iSelectionPenWidth=IDM_PEN_WIDTH1;if(pDrawData){SetWindowLong(hWnd,0,(LONG)pDrawData);}〃初始
if(SaveText[j].hFont)SelectObject(hdc,SaveText[j].hFont);//选用逻辑字体
SetTextColor(hdc,SaveText[j].crText);
TextOut(hdc,
SaveText[j].pt.x,SaveText[j].pt.y,SaveText[j].g_EditInput,strlen(SaveText[j].g_Editlnput));
SelectObject(hdc,GetStockObject(SYSTEM_FONT));〃还原逻辑字体
}.
2.6文本保存
代码如下
voidSaveMyData(structMySaveLine*SaveLine,intLineCount,structMySaveRectangle*SaveRectangle,intRectangleCount,structMySaveEllipse*SaveEllipse,intEllipseCount,structMySaveText*SaveText,intTextCount)
staticHANDLEhFile;
hFile=CreateFile("e:
\\abc.f",GENERIC_WRITE,0,
NULL,CREATE_ALWAYS,0,NULL);
if(hFile!
=INVALID_HANDLE_VALUE)
DWORDdwByte仁0;
DWORDdwByte2=0;
DWORDdwByte3=0;
DWORDdwByte4=0;
WriteFile(hFile,&LineCount,sizeof(int),
&dwBytel,NULL);
WriteFile(hFile,&RectangleCount,sizeof(int),
&dwByte2,NULL);
WriteFile(hFile,&EllipseCount,sizeof(int),
&dwByte3,NULL);
WriteFile(hFile,&TextCount,sizeof(int),
&dwByte4,NULL);
WriteFile(hFile,SaveLine,LineCount*sizeof(MySaveLine),
&dwByte1,NULL);
WriteFile(hFile,SaveRectangle,RectangleCount*sizeof(MySaveRectangle),
WriteFile(hFile,SaveEllipse,EllipseCount*sizeof(MySaveEllipse),
WriteFile(hFile,SaveText,TextCount*sizeof(MySaveText),
CloseHandle(hFile);
2.7文本读取
代码如下:
voidLoadMyData(structMySaveLine*SaveLine,intLineCount,structMySaveRectangle*SaveRectangle,intRectangleCount,structMySaveEllipse*SaveEllipse,intEllipseCount,structMySaveText*SaveText,intTextCount)
\\abc.f",GENERIC_READ,FILE_SHARE_READ,
NULL,OPEN_EXISTING,0,NULL);if(hFile!
ReadFile(hFile,&LineCount,sizeof(int),
ReadFile(hFile,&RectangleCount,sizeof(int),
ReadFile(hFile,&EllipseCount,sizeof(int),
ReadFile(hFile,&TextCount,sizeof(int),
ReadFile(hFile,SaveLine,LineCount*sizeof(MySaveLine),
ReadFile(hFile,SaveRectangle,RectangleCount*sizeof(MySaveRectangle),
ReadFile(hFile,SaveEllipse,EllipseCount*sizeof(MySaveEllipse),
ReadFile(hFile,SaveText,TextCount*sizeof(MySaveText),
3实验总结
本实验最大的特点是使用结构体数组,这也是我第一次使用结构体数组,从不使用结构
体到使用结构体指针最后升级到结构体数组是一个循序渐进的过程,在这个过程中我越来越
感觉到结构体在管理数据方面的优势。
我的感觉是在主CPP文件中代码应该是越少越好,
很多地方只要调用函数就可以了,然后在另外的CPP中写函数。
这样的程序给人的感觉是分层做的比较好,调理比较清晰。
附录关键代码
一、主CPP
/*******************************************************************
程序:
Paint
文件:
Paint.cpp
功能:
简易的图像编辑器
********************************************************************/
#include"draw.h"
#defineMAX100
TCHARszAppName[];
structMySaveLineSaveLine[MAX];
structMySaveRectangleSaveRectangle[MAX];
structMySaveEllipseSaveEllipse[MAX];
structMySaveTextSaveText[MAX];
intlineCount;
intrectangleCount;
intellipseCount;inttextCount;
DRAWDATA*pDrawData;HACCELhAccel;
HDChdc;inti,m,n,p,q;
BOOLInitWindow(HINSTANCEhlnstanee,intnCmdShow);
LRESULTCALLBACKWinProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMIParam);
函数:
WinMain()
Win32应用程序入口函数。
创建主窗口,处理消息循环
******************************************************************
**/
intWINAPIWinMain(HINSTANCEhlnstanee,
HINSTANCEhPrevInstanee,
PSTRszCmdLine,intiCmdShow)
MSGmsg;
char*p;
//当前实例句柄
//前一个实例句柄
//命令行字符
//窗口显示方式
〃创建主窗口
if(!
InitWindow(hInstanee,iCmdShow))
returnFALSE;
p=0;
//进入消息循环:
从该应用程序的消息队列中检取消息,
〃送到消息处理过程,当检取到WM_QUIT消息时,退出消息循环。
while(GetMessage(&msg,NULL,0,0))
TranslateMessage(&msg);
DispatchMessage(&msg);
returnmsg.wParam;
/******************************************************************
InitWindow()
创建窗口。
*******************************************************************/
staticBOOLInitWindow(HINSTANCEhlnstanee,intiCmdShow){
HWNDhwnd;
//窗口句柄
//窗口类结构
WNDCLASSwcMainWnd;
ATOMa;
〃填充窗口类结构
wcMainWnd.style=CS_VREDRAW|CS_HREDRAW;
wcMainWnd.lpfnWndProc=WinProc;
wcMainWnd.cbCIsExtra=0;
wcMainWnd.cbWndExtra=0;
wcMainWnd.hInstanee=hlnstanee;
wcMainWnd.hlcon=Loadlcon(hlnstanee,MAKEINTRESOURCE(IDI_ICON1));
wcMainWnd.hCursor=LoadCursor(NULL,IDC_ARROW);
wcMainWnd.hbrBackground=(structHBRUSH__*)GetStockObject(WHITE_BRUSH);
wcMainWnd.lpszMenuName=(LPSTR)IDR_MENU1;;
wcMainWnd.lpszCIassName=szAppName;
LoadAcceIerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1));hAccel=LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1));
〃注册窗口类
a=RegisterClass(&wcMainWnd);
a)
MessageBox(NULL,TEXT("注册窗口类失败!
"),szAppName,
MB_ICONERROR);
return0;
hwnd=CreateWindow(szAppName,
TEXT("简易的图形编辑器
WS_OVERLAPPEDWINDOW,
200,
800,
450,
NULL,
hlnstanee,
NULL);
hwnd)returnFALSE;
〃显示并更新窗口
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);returnTRUE;
}/******************************************************************
WinProc()
处理主窗口消息
LRESULTCALLBACKWinProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAM
IParam)
PAINTSTRUCTps;
RECTrect;
HMENUhMenu;
bDrawing=FALSE,Line=FALSE,
staticBOOL
Rectangle=FALSE,Ellipse=FALSE,Text=FALSE;staticHMENUhPopMenu;
inthNewPenStyle,hNewPenWidth;
intBrushColor;
COLORREFhNewPenColor;
staticintx,y;
staticchara[100];
staticintk=1;
staticHWNDhWndEdit;
caseWMCREATE:
//创建消息
pDrawData=newDRAWDATA;
pDrawData->iSelectionFunc=IDM_FUNC_LINETO;pDrawData->iSelectionPenColor=IDM_PEN_BLACK;pDrawData->iSelectionPenStyle=IDM_PEN_SOLID;
pDrawData->iSelectionPenWidth=IDM_PEN_WIDTH1;if(pDrawData)
SetWindowLong(hWnd,0,(LONG)pDrawData);
〃初始
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2