#include <iostream>
#include <fstream>
#include <windows.h>
#include "resource.h"
#include <string.h>
using namespace std;LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
OPENFILENAME ofn;//定义变量
TCHAR szFile[100]=TEXT("");//准备用来存放文件的缓冲区
static TCHAR szAppName[] = TEXT ("Graphic") ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
HACCEL hAccel ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON1)) ;
wndclass.hCursor = LoadCursor (hInstance,MAKEINTRESOURCE(IDC_CURSOR1)) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1) ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("The fifth assignment"),
WS_OVERLAPPEDWINDOW|WS_VSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ; iCmdShow=SW_SHOWMAXIMIZED;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
hAccel = LoadAccelerators (hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1)) ; while (GetMessage (&msg, NULL, 0, 0))
{
if (!TranslateAccelerator (hwnd, hAccel, &msg))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
return (int)msg.wParam ;
}class Graphic
{
public:
int i;//i indicate 1.Bezier 2.Ellipse 3.Rectangle
POINT point[4];
public:
Graphic(int ii,POINT ppoint[])
{
i=ii;
for(int j=0;j<=3;j++)
point[j]=ppoint[j];
}
Graphic()
{
i=0;
}
};class GraphicStack
{
public:
Graphic stack[100];
int i;//i指向栈顶位置
public:
GraphicStack()
{
i=0;
} void pushback(Graphic iup)
{
stack[i].i=iup.i;
for(int j=0;j<=3;j++)
stack[i].point[j]=iup.point[j];
i++;
}
void clear()
{
i=0;
}
};int Open_Item(HWND hwnd)
{
//initialize openfilename
ZeroMemory(&ofn,sizeof(OPENFILENAME));//整个数据结构的所有信息置0
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.hwndOwner=hwnd;
ofn.lpstrFile=szFile;
ofn.nMaxFile=sizeof(szFile);
ofn.lpstrFilter=TEXT("All Files(*.*)\0*.*\0Text Files(*.txt)\0*.TXT\0C++ Files(*.cpp)\0*.cpp\0C++ hFiles(*.h)\0*.h\0");//自定义筛选器
ofn.nFilterIndex=1;//default the number of the filter
ofn.lpstrFileTitle=NULL;
ofn.nMaxFileTitle=0;//Specifies the size, in TCHARs, of the buffer pointed to by lpstrFileTitle. For the ANSI version, this is the number of bytes; for the Unicode version, this is the number of characters. This member is ignored if lpstrFileTitle is NULL
ofn.lpstrInitialDir=NULL;//Pointer to a NULL terminated string that can specify the initial directory. The algorithm for selecting the initial directory varies on different platforms.
ofn.Flags=OFN_HIDEREADONLY|OFN_CREATEPROMPT;
//DISPLAY THE OPEN DIALOG
return GetOpenFileName(&ofn);
}int Save_Item(HWND hwnd)
{
//initialize openfilename
ZeroMemory(&ofn,sizeof(OPENFILENAME));//整个数据结构的所有信息置0
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.hwndOwner=hwnd;
ofn.lpstrFile=szFile;
ofn.nMaxFile=sizeof(szFile);
ofn.lpstrFilter=TEXT("Text Files(*.txt)\0*.TXT\0C++ Files(*.cpp)\0*.cpp\0C++ hFiles(*.h)\0*.h\0");//自定义筛选器
ofn.nFilterIndex=1;//default the number of the filter
ofn.lpstrFileTitle=NULL;
ofn.nMaxFileTitle=0;//Specifies the size, in TCHARs, of the buffer pointed to by lpstrFileTitle. For the ANSI version, this is the number of bytes; for the Unicode version, this is the number of characters. This member is ignored if lpstrFileTitle is NULL
ofn.lpstrInitialDir=NULL;//Pointer to a NULL terminated string that can specify the initial directory. The algorithm for selecting the initial directory varies on different platforms.
ofn.Flags=OFN_HIDEREADONLY|OFN_CREATEPROMPT; //DISPLAY THE OPEN DIALOG
return GetSaveFileName(&ofn);
}
#include <fstream>
#include <windows.h>
#include "resource.h"
#include <string.h>
using namespace std;LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
OPENFILENAME ofn;//定义变量
TCHAR szFile[100]=TEXT("");//准备用来存放文件的缓冲区
static TCHAR szAppName[] = TEXT ("Graphic") ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
HACCEL hAccel ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON1)) ;
wndclass.hCursor = LoadCursor (hInstance,MAKEINTRESOURCE(IDC_CURSOR1)) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1) ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("The fifth assignment"),
WS_OVERLAPPEDWINDOW|WS_VSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ; iCmdShow=SW_SHOWMAXIMIZED;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
hAccel = LoadAccelerators (hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1)) ; while (GetMessage (&msg, NULL, 0, 0))
{
if (!TranslateAccelerator (hwnd, hAccel, &msg))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
return (int)msg.wParam ;
}class Graphic
{
public:
int i;//i indicate 1.Bezier 2.Ellipse 3.Rectangle
POINT point[4];
public:
Graphic(int ii,POINT ppoint[])
{
i=ii;
for(int j=0;j<=3;j++)
point[j]=ppoint[j];
}
Graphic()
{
i=0;
}
};class GraphicStack
{
public:
Graphic stack[100];
int i;//i指向栈顶位置
public:
GraphicStack()
{
i=0;
} void pushback(Graphic iup)
{
stack[i].i=iup.i;
for(int j=0;j<=3;j++)
stack[i].point[j]=iup.point[j];
i++;
}
void clear()
{
i=0;
}
};int Open_Item(HWND hwnd)
{
//initialize openfilename
ZeroMemory(&ofn,sizeof(OPENFILENAME));//整个数据结构的所有信息置0
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.hwndOwner=hwnd;
ofn.lpstrFile=szFile;
ofn.nMaxFile=sizeof(szFile);
ofn.lpstrFilter=TEXT("All Files(*.*)\0*.*\0Text Files(*.txt)\0*.TXT\0C++ Files(*.cpp)\0*.cpp\0C++ hFiles(*.h)\0*.h\0");//自定义筛选器
ofn.nFilterIndex=1;//default the number of the filter
ofn.lpstrFileTitle=NULL;
ofn.nMaxFileTitle=0;//Specifies the size, in TCHARs, of the buffer pointed to by lpstrFileTitle. For the ANSI version, this is the number of bytes; for the Unicode version, this is the number of characters. This member is ignored if lpstrFileTitle is NULL
ofn.lpstrInitialDir=NULL;//Pointer to a NULL terminated string that can specify the initial directory. The algorithm for selecting the initial directory varies on different platforms.
ofn.Flags=OFN_HIDEREADONLY|OFN_CREATEPROMPT;
//DISPLAY THE OPEN DIALOG
return GetOpenFileName(&ofn);
}int Save_Item(HWND hwnd)
{
//initialize openfilename
ZeroMemory(&ofn,sizeof(OPENFILENAME));//整个数据结构的所有信息置0
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.hwndOwner=hwnd;
ofn.lpstrFile=szFile;
ofn.nMaxFile=sizeof(szFile);
ofn.lpstrFilter=TEXT("Text Files(*.txt)\0*.TXT\0C++ Files(*.cpp)\0*.cpp\0C++ hFiles(*.h)\0*.h\0");//自定义筛选器
ofn.nFilterIndex=1;//default the number of the filter
ofn.lpstrFileTitle=NULL;
ofn.nMaxFileTitle=0;//Specifies the size, in TCHARs, of the buffer pointed to by lpstrFileTitle. For the ANSI version, this is the number of bytes; for the Unicode version, this is the number of characters. This member is ignored if lpstrFileTitle is NULL
ofn.lpstrInitialDir=NULL;//Pointer to a NULL terminated string that can specify the initial directory. The algorithm for selecting the initial directory varies on different platforms.
ofn.Flags=OFN_HIDEREADONLY|OFN_CREATEPROMPT; //DISPLAY THE OPEN DIALOG
return GetSaveFileName(&ofn);
}
{
static HINSTANCE hInstance;
static GraphicStack stack;
static POINT point[4];
static int nState=0;
static BOOL fErase=FALSE;
static RECT rect;
static int nGraph=ID_BEZIER;
int xPos,yPos;
HDC hdc;
POINT dot;
PAINTSTRUCT ps;
TCHAR str[50];//wchar_t str[50];// char str[50];
static int k; static HMENU hMenu ;
static HANDLE hFile; switch (message)
{
case WM_CREATE:
GetClientRect(hwnd,&rect);
CheckMenuRadioItem(GetMenu(hwnd),ID_BEZIER,ID_ELLIPSE,ID_RECTANGLE,MF_BYCOMMAND);
return 0;
case WM_SIZE:
InvalidateRect (hwnd, NULL, TRUE) ;
nState=0;
return 0 ; case WM_PAINT:
hdc=BeginPaint(hwnd,&ps); FillRect(hdc,&rect,WHITE_BRUSH); for(k=0;k<stack.i;k++)
if(stack.stack[k].i==1)
PolyBezier(hdc,stack.stack[k].point,4); else if(stack.stack[k].i==2)
{
//SelectObject(hdc,GetStockObject(NULL_BRUSH));
Ellipse(hdc,stack.stack[k].point[0].x,stack.stack[k].point[0].y,stack.stack[k].point[1].x,stack.stack[k].point[1].y);
//SelectObject(hdc,GetStockObject(WHITE_BRUSH));
}
else if(stack.stack[k].i==3)
{
//SelectObject(hdc,GetStockObject(NULL_BRUSH));
Polygon(hdc,stack.stack[k].point,4);
//SelectObject(hdc,GetStockObject(WHITE_BRUSH));
}
EndPaint(hwnd,&ps);
nState=0;
return 0; case WM_LBUTTONDOWN://客户区鼠标消息
GetCursorPos(&dot);
ScreenToClient(hwnd,&dot);
xPos=dot.x;
yPos=dot.y;
SetCapture(hwnd);
hdc=GetDC(hwnd);
SetROP2(hdc,R2_NOTXORPEN);
switch(nGraph)
{
case ID_BEZIER:
switch (nState)
{
case 0:
point[0].x=xPos;
point[0].y=yPos;
nState=1;
break;
case 2:
MoveToEx(hdc,point[0].x,point[0].y,NULL);
LineTo(hdc,point[3].x,point[3].y);
point[1].x=xPos; point[1].y=yPos;
point[2].x=xPos; point[2].y=yPos;
PolyBezier(hdc,point,4);
nState=3;
fErase=TRUE;
break;
case 4:
PolyBezier(hdc,point,4);
point[2].x=xPos; point[2].y=yPos;
PolyBezier(hdc,point,4);
nState=5;
fErase=TRUE;
break;
}
break; case ID_ELLIPSE:
point[0].x=xPos;
point[0].y=yPos;
point[1].x=xPos;
point[1].y=yPos;
fErase=TRUE;
break;
case ID_RECTANGLE:
switch(nState)
{
case 0:
point[0].x=point[1].x=xPos;
point[0].y=point[1].y=yPos;
nState=1;
fErase=TRUE;
break;
}
}
ReleaseDC(hwnd,hdc);
break;
InvalidateRect(hwnd,NULL,TRUE);
return 0 ;
GetCursorPos(&dot);
ScreenToClient(hwnd,&dot);
xPos=dot.x;
yPos=dot.y; switch(nGraph)
{
case ID_BEZIER:
fErase=FALSE;
switch(nState)
{
case 1:
point[3].x=xPos; point[3].y=yPos;
nState=2;
break; case 3:
point[1].x=xPos; point[1].y=yPos;
nState=4;
break; case 5:
point[2].x=xPos; point[2].y=yPos;
nState=0;
stack.pushback(Graphic(1,point));//
break;
}
break; case ID_ELLIPSE:
stack.pushback(Graphic(2,point));//
case ID_RECTANGLE:
switch(nState)
{
case 1:
point[1].x=point[2].x=xPos;
point[1].y=point[2].y=yPos;
nState=2;
break;
case 2:
point[2].x=point[3].x=xPos;
point[2].y=point[3].y=yPos;
nState=3;
break;
case 3:
point[3].x=xPos;
point[3].y=yPos;
nState=4;
fErase=FALSE;
break;
}
break;
}
ReleaseCapture();
break;
return 0;
case WM_MOUSEMOVE:
GetCursorPos(&dot);
ScreenToClient(hwnd,&dot);
xPos=dot.x;
yPos=dot.y;
hdc=GetDC(hwnd);
SetROP2(hdc,R2_NOTXORPEN);
switch(nGraph)
{
case ID_BEZIER:
if(wParam&MK_LBUTTON){
switch (nState){
case 1:
if(fErase){
MoveToEx(hdc,point[0].x,point[0].y,NULL);
LineTo(hdc,point[3].x,point[3].y);
}
point[3].x=xPos; point[3].y=yPos;
MoveToEx(hdc,point[0].x,point[0].y,NULL);
LineTo(hdc,point[3].x,point[3].y);
break;
case 3:
if(fErase) PolyBezier(hdc,point,4);
point[1].x=xPos; point[1].y=yPos;
point[2].x=xPos; point[2].y=yPos;
PolyBezier(hdc,point,4);
break;
case 5:
if(fErase) PolyBezier(hdc,point,4);
point[2].x=xPos; point[2].y=yPos;
PolyBezier(hdc,point,4);
break;
}
fErase=TRUE;
} case ID_ELLIPSE:
if(wParam&MK_LBUTTON)
{
SetROP2(hdc,R2_NOTXORPEN);
if(fErase)
Ellipse(hdc,point[0].x,point[0].y,point[1].x,point[1].y);
point[1].x=xPos;
point[1].y=yPos;
Ellipse(hdc,point[0].x,point[0].y,point[1].x,point[1].y);
} case ID_RECTANGLE:
if(wParam&MK_LBUTTON){
SetROP2(hdc,R2_NOTXORPEN);
switch(nState)
{
case 1:
MoveToEx(hdc,point[0].x,point[0].y,NULL);
LineTo(hdc,point[1].x,point[1].y);
point[1].x=xPos;
point[1].y=yPos;
MoveToEx(hdc,point[0].x,point[0].y,NULL);
LineTo(hdc,point[1].x,point[1].y);
break;
case 2:
MoveToEx(hdc,point[1].x,point[1].y,NULL);
LineTo(hdc,point[2].x,point[2].y);
point[2].x=xPos;
point[2].y=yPos;
MoveToEx(hdc,point[1].x,point[1].y,NULL);
LineTo(hdc,point[2].x,point[2].y);
break;
case 3:
MoveToEx(hdc,point[2].x,point[2].y,NULL);
LineTo(hdc,point[3].x,point[3].y);
point[3].x=xPos;
point[3].y=yPos;
MoveToEx(hdc,point[2].x,point[2].y,NULL);
LineTo(hdc,point[3].x,point[3].y);
break;
}
break;
}
}
ReleaseDC(hwnd,hdc);
return 0; case WM_COMMAND:
hMenu = GetMenu (hwnd) ;
switch (LOWORD (wParam))
{
case ID_NEW:
stack.clear();
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case ID_LOAD:
if(Open_Item(hwnd))
{
ifstream fin(szFile,ios::binary);
fin.read((char *)(&stack), sizeof(stack));
fin.close();
InvalidateRect(hwnd,NULL,TRUE);
}
return 0; case ID_SAVE:
if(Save_Item(hwnd)>0)
{
ofstream fout(szFile, ios::binary);
fout.write((char *)(&stack), sizeof(stack));
fout.close();
}
return 0; case ID_EXIT:
SendMessage (hwnd, WM_CLOSE, 0, 0) ;
return 0 ; case ID_ABOUT_INFORMATION:
LoadString(hInstance,IDS_STRINGINFO,str,lstrlen(str));
MessageBox(hwnd,str,szAppName,MB_OK | MB_ICONINFORMATION) ;
return 0 ;
case ID_BEZIER:
case ID_ELLIPSE:
case ID_RECTANGLE:
if(nGraph==ID_RECTANGLE&&nState==4&&LOWORD(wParam)==ID_RECTANGLE)
{
stack.pushback(Graphic(3,point));
InvalidateRect(hwnd,NULL,TRUE);
}
CheckMenuRadioItem(GetMenu(hwnd),ID_BEZIER,ID_RECTANGLE,LOWORD(wParam),MF_BYCOMMAND);
nGraph=LOWORD(wParam);
nState=0;
InvalidateRect(hwnd,NULL,TRUE);
return 0; case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
}
然后把WndProc里面的那些static变量放到程序的最开始作为全局变量而存在之类的。