⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawer.cpp

📁 Windows下的简易小画板
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "Drawer.h"

static HPEN		hPen[6];
static HBRUSH	hbrush[6];
static COLORREF col[6];
static int		width = 0;
static int		psstyle;
#define WM_STATUSBAR	60

static OPENFILENAME  ofn;

typedef struct
{
	POINT		begin;
	POINT		end;
	int			width;
	COLORREF	color;
	int			psstyle;
	int			Kind;
	
}shape;

static vector<shape> shapes;
static list<shape> stacks;

static TCHAR	szStatus[] = TEXT("就绪");
static TCHAR    szFileName[MAX_PATH], szTitleName[MAX_PATH] ;
static TCHAR	szFilter[] = TEXT ("Text Files (*.00)\0*.00\0")\
TEXT ("All Files (*.*)\0*.*\0\0") ;


LRESULT CALLBACK	WndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInstance = NULL;
HINSTANCE hInst;

//HWND hStatusWnd;

int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,
					LPSTR IpCmdLine,int nCmdShow)
{
	
	static TCHAR szAppName[] = TEXT("HelloWin");
	HWND		hwnd;
	MSG			msg;
	WNDCLASS	wndclass;
	g_hInstance = hInstance;
	hInst = hInstance;
	HACCEL	hAccel;
	
	wndclass.style			= CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc	= WndProc;
	wndclass.cbClsExtra		= 0;
	wndclass.cbWndExtra		= 0;
	wndclass.hInstance		= hInstance;
	wndclass.hIcon			= LoadIcon (hInstance,(LPCTSTR)IDI_HEART);
	wndclass.hCursor		= LoadCursor (hInstance,(LPCTSTR)IDC_HAND);
	wndclass.hbrBackground	= (HBRUSH) GetStockObject (WHITE_BRUSH);
	wndclass.lpszMenuName	= (LPCTSTR)IDR_MENU;
	wndclass.lpszClassName	=szAppName;
	
	if (!RegisterClass (&wndclass))
	{
		MessageBox(NULL,TEXT("Wrong"),TEXT("WARNING"),MB_ICONERROR);
		return 0;
	}
	
	hwnd = CreateWindow (szAppName,
		TEXT ("简易小画板---高文涛"),
		WS_OVERLAPPEDWINDOW,
		200,
		110,
		650,
		500,
		NULL,
		NULL,
		hInstance,
		NULL);
	hAccel = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR));
	
	ShowWindow (hwnd,nCmdShow);
	UpdateWindow (hwnd);
	
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccel, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return msg.wParam;
}

void DrawBitmap(HDC hdc,int xStart,int yStart,HBITMAP hBitmap)
{
	BITMAP bm;
	HDC hMemDC;
	POINT pt;
	hMemDC=CreateCompatibleDC(hdc);
	SelectObject(hMemDC,hBitmap);
	GetObject(hBitmap,sizeof(BITMAP),&bm);
	pt.x=bm.bmWidth;
	pt.y=bm.bmHeight;
	
	BitBlt(hdc,xStart,yStart,pt.x,pt.y,hMemDC,0,0,SRCCOPY);
	DeleteDC(hMemDC);
}

double Distance(POINT pt,POINT line1,POINT line2)
{
	double s1,s2,s3,s,d;
	s1 = sqrt((double) (line1.x - pt.x)*(line1.x - pt.x)
		+(line1.y - pt.y)*(line1.y - pt.y));
	s2 = sqrt((double) (line2.x - pt.x)*(line2.y - pt.x)
		+(line2.y- pt.y)*(line2.y - pt.y));
	s3 = sqrt((double) (line1.x - line2.x)*(line1.x - line2.x)
		+(line1.y - line2.y)*(line1.y - line2.y));
	s = (s1 + s2 + s3) / 2;
	return d = sqrt( s*(s-s1)*(s-s2)*(s-s3) / s3 );
}


LRESULT CALLBACK WndProc (HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
	HDC				hdc;
	HPEN			hpen;
	HBRUSH			hBr;
	PAINTSTRUCT		ps;
	HWND			hToolsWindow;
	HBITMAP			hBitmap;
	RECT			rect;
	GetClientRect(hwnd,&rect);
	LPNMTTDISPINFO TTtext;
	
	static HWND		hwndEdit;
	static HINSTANCE hInstance;
	static int		cxChar,cxCaps,cyChar,cxClient, cyClient;
	static int		count=0,st = 0;
	static int		flag;
	static			bPress = 0;
	static BOOL		FD = false,SX = false,SAVED = TRUE,bKEY = FALSE,choose = FALSE;
	static POINT	begin,end,pchoose;
	static DWORD	dwCharSet = DEFAULT_CHARSET ;
	static BOOL		curf = false;
	static HDC		hdcmem;
	static HBITMAP	hmap;
	static HPEN		Hpen;
	static CHOOSECOLOR	cc;
	static COLORREF		crCustColors[16];
	static shape	pb,pstack;
	static double	dis;
	
	static int				i,j;
	int				iReturn;
	
	cc.lStructSize			= sizeof(CHOOSECOLOR);
	cc.hwndOwner			= hwnd;
	cc.hInstance			= NULL;
	cc.rgbResult			= RGB(0x80,0x80,0x80);
	cc.lpCustColors			= crCustColors;
	cc.Flags				= CC_RGBINIT |CC_FULLOPEN;
	cc.lCustData			= 0;
	cc.lpfnHook				= NULL;
	cc.lpTemplateName		= NULL;
	
	ofn.lStructSize       = sizeof (OPENFILENAME) ;
	ofn.hwndOwner         = hwnd ;
	ofn.hInstance         = NULL ;
	ofn.lpstrFilter       = szFilter ;
	ofn.lpstrCustomFilter = NULL ;
	ofn.nMaxCustFilter    = 0 ;
	ofn.nFilterIndex      = 0 ;
	ofn.lpstrFile         = szFileName ;            // Set in Open and Close functions
	ofn.nMaxFile          = MAX_PATH ;
	ofn.lpstrFileTitle    = szTitleName ;          // Set in Open and Close functions
	ofn.nMaxFileTitle     = MAX_PATH ;
	ofn.lpstrInitialDir   = NULL ;
	ofn.lpstrTitle        = NULL ;
	ofn.Flags             = 0 ;             // Set in Open and Close functions
	ofn.nFileOffset       = 0 ;
	ofn.nFileExtension    = 0 ;
	ofn.lpstrDefExt       = TEXT ("00") ;
	ofn.lCustData         = 0L ;
	ofn.lpfnHook          = NULL ;
	ofn.lpTemplateName    = NULL ;
	
	switch (message)
	{
		////////////////////////////  case WM_CREATE:  ////////////////////////////////////////
	case WM_CREATE:
		{
			//////////////////////////////////     起始屏      /////////////////////////////////////////
			hInstance=(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE);
			hdc=CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);
			hBitmap=LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP1));
			DrawBitmap(hdc,320,230,hBitmap);
			DeleteDC(hdc);
			Sleep(1500);
			
			/////////////////////////////        创建工具条          ///////////////////////////////////			
			TBBUTTON ColorToolBar[21];
			for (i = 0; i < 21; i ++)
			{
				ColorToolBar[i].iBitmap = i;
				ColorToolBar[i].idCommand = i;
				ColorToolBar[i].fsState = TBSTATE_ENABLED;
				ColorToolBar[i].fsStyle = TBSTYLE_CHECKGROUP; 
				ColorToolBar[i].dwData = 0;
				ColorToolBar[i].iString = 0;
			}
			
			ColorToolBar[0].fsStyle = TBSTYLE_BUTTON;
			ColorToolBar[1].fsStyle = TBSTYLE_BUTTON;
			ColorToolBar[2].fsStyle = TBSTYLE_BUTTON;
			ColorToolBar[3].fsStyle = TBSTYLE_BUTTON;
			ColorToolBar[4].fsStyle = TBSTYLE_BUTTON;
			ColorToolBar[5].fsStyle = TBSTYLE_BUTTON;
			ColorToolBar[6].fsStyle = TBSTYLE_BUTTON;
			ColorToolBar[7].fsStyle = TBSTYLE_BUTTON;
			ColorToolBar[13].fsStyle = TBSTYLE_BUTTON;
			ColorToolBar[14].fsStyle = TBSTYLE_BUTTON;
			ColorToolBar[20].fsStyle = TBSTYLE_BUTTON;
			
			hToolsWindow=::CreateToolbarEx( hwnd, 
				WS_VISIBLE|CCS_ADJUSTABLE|TBSTYLE_WRAPABLE |TBSTYLE_TOOLTIPS,										
				IDR_TOOLBAR,					
				22,
				g_hInstance,	
				IDR_COLORTOOLBAR,
				ColorToolBar,	
				21,
				16,
				16,
				16,
				16,
				sizeof(TBBUTTON));
			
			TBADDBITMAP ToolBarBitmap;
			ToolBarBitmap.hInst = g_hInstance;
			ToolBarBitmap.nID = IDR_COLORTOOLBAR;
			
			::SendMessage(hToolsWindow,TB_ADDBITMAP,(WPARAM)3,(LPARAM)&ToolBarBitmap); 
			
			////////////////////////////////      创建状态栏////////////////////////////////////////////
			/*hStatusWnd = CreateStatusWindow(
				WS_CHILD | WS_VISIBLE,
				szStatus,
				hwnd,
				WM_STATUSBAR
				);*/
		}
		return 0;
		/////////////////////////////     case WM_COMMAND:      ////////////////////////////////
	case WM_COMMAND:
		bPress = wParam;
		hdc = GetDC(hwnd);
		switch(wParam)
		{
		case IDM_FILE_NEW:
		case 0:
			if(SAVED == FALSE)
				iReturn = MessageBox(hwnd,TEXT("文档没有保存,保存吗?"),TEXT("提示"),
				MB_YESNOCANCEL|MB_APPLMODAL|MB_ICONQUESTION);
			if(iReturn == IDYES)
			{
				SendMessage (hwnd, WM_COMMAND, 2, 0);
				return 0;
			}
			if(iReturn == IDNO)
			{
				shapes.clear();
				InvalidateRect(hwnd,NULL,true);
				SAVED = TRUE;
			}
			if(iReturn == IDCANCEL)
				return 0;
			break;
		case IDM_FILE_OPEN:
		case 1:
			{
				DWORD dwByteRead;
				HANDLE hFile;
				shape temp;
				ofn.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT ;
				iReturn = GetOpenFileName (&ofn) ;
				if(iReturn == TRUE)
				{
					hFile=CreateFile(ofn.lpstrFile,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
					shapes.clear();
					while(ReadFile(hFile,&temp,sizeof(shape),&dwByteRead,NULL)!=0&&dwByteRead!=0)
						shapes.push_back(temp);
					CloseHandle(hFile);
					InvalidateRect(hwnd,NULL,TRUE);
				}
			}
			break;
		case IDM_FILE_SAVE:
		case 2:
			{
				vector<shape>::iterator it;
				shape temp;
				DWORD dwByteWritten;
				HANDLE hFile;
				
				ofn.Flags= OFN_OVERWRITEPROMPT ;
				iReturn = GetSaveFileName (&ofn) ;
				if(iReturn)
				{
					hFile=CreateFile(ofn.lpstrFile,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
					for(it=shapes.begin();it!=shapes.end();it++)
					{
						temp=*it;
						WriteFile(hFile,&temp,sizeof(shape),&dwByteWritten,NULL);
					}
					if(iReturn == TRUE)
						SAVED = TRUE;
					CloseHandle(hFile);
				}
			}
			break;
		case IDM_UNDO:
		case 3:
			if(shapes.size()>0)
			{
				stacks.push_back(shapes.back());
				shapes.pop_back();
				st++;
				InvalidateRect(hwnd,NULL,TRUE);
			}
			break;
		case IDM_REDO:
		case 4:
			if(st != 0)
			{
				shapes.push_back(stacks.back());
				stacks.pop_back();
				st--;
				InvalidateRect(hwnd,NULL,TRUE);
			}
			break;
		case IDM_CHOOSE:
		case 5:
			choose = TRUE;
			break;
		case IDM_DELETE:
		case 6:
			shapes[j].color = RGB(255,255,255);
			InvalidateRect(hwnd,NULL,TRUE);
			break;
		case IDM_COLOR:
		case 7:
			ChooseColor(&cc);
			pb.color = cc.rgbResult;
			break;
		case IDM_SOLID:
		case 8:
			psstyle = PS_SOLID;
			break;
		case IDM_DASH:
		case 9:
			psstyle = PS_DASH;
			break;
		case IDM_ONE:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -