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

📄 mainfrm.cpp

📁 实现了2D地图编辑器的基本功能
💻 CPP
字号:
// MainFrm.cpp : CMainFrame 类的实现
//

#include "stdafx.h"
#include "MapEditor.h"
#include "UpView.h"
#include "DownView.h"
#include "MainFrm.h"
#include "MapEditorDoc.h"
#include "MapEditorView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CString     m_Pathname;
CString     m_name;
extern MESH1    m_mesh[50][50];
int num[50][4]={0};
int l;
extern int x,y;

// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
    ON_COMMAND(ID_FILE_OPEN, &CMainFrame::OnFileOpen)
    ON_COMMAND(ID_MAP_OPEN, &CMainFrame::OnMapOpen)
    ON_COMMAND(ID_MAP_SAVE, &CMainFrame::OnMapSave)
    ON_COMMAND(ID_EDIT_UNDO, &CMainFrame::OnEditUndo)
    ON_COMMAND(ID_EDIT_DO, &CMainFrame::OnEditDo)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // 状态行指示器
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};


// CMainFrame 构造/析构

CMainFrame::CMainFrame()
{
	// TODO: 在此添加成员初始化代码
}

CMainFrame::~CMainFrame()
{
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("未能创建工具栏\n");
		return -1;      // 未能创建
	}

//    m_ToolBarEx.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP	| 
//	                        CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
//
//    m_ToolBarEx.SetButtons(NULL,4);
//	m_ToolBarEx.SetImageSize (20,20);
///*	m_ToolBarEx.AddIcon(AfxGetApp()->LoadIconW(IDI_ICON1));  
//	m_ToolBarEx.AddIcon(AfxGetApp()->LoadIconW(IDI_ICON2)); 
//	m_ToolBarEx.AddIcon(AfxGetApp()->LoadIconW(IDI_ICON3)); */ 
//
//	m_ToolBarEx.SetImage();
//    m_ToolBarEx.SetButton(0,ID_IMAGE_OPEN,0,_T("打开"));
//	m_ToolBarEx.SetButton(1,ID_IMAGE_SAVE,1,_T("保存"));
//	m_ToolBarEx.SetButton(3,ID_IMAGE_HELP,2,_T("帮助"));
//	
//    m_ToolBarEx.SetButtonInfo (2,0,TBBS_SEPARATOR,0);
//    m_ToolBarEx.SetSize();
//
//    m_ToolBarEx.EnableDocking(CBRS_ALIGN_ANY);
//    EnableDocking(CBRS_ALIGN_ANY);
//    DockControlBar(&m_ToolBarEx);

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("未能创建状态栏\n");
		return -1;      // 未能创建
	}

	// TODO: 如果不需要工具栏可停靠,则删除这三行
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式
	cs.style&=~FWS_ADDTOTITLE;
	cs.lpszName="2D地图编译器"; 

	return TRUE;
}


// CMainFrame 诊断

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG


// CMainFrame 消息处理程序

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
    // TODO: 在此添加专用代码和/或调用基类
    if (!m_Splitter.CreateStatic(this, 2, 1))
	{
		TRACE0("Failed to CreateStaticSplitter\n");
		return FALSE;
	}
     //	SIZE size;                                  
	CRect rect;     
	GetClientRect(&rect);   


	// 上视
    if (!m_Splitter.CreateView(0,0,RUNTIME_CLASS(CUpView),CSize(0, (rect.bottom - rect.top)*0.3), pContext))
	{
		TRACE0("Failed to create first pane\n");
		return FALSE;
	}

	// 下视
    if (!m_Splitter.CreateView(1,0,RUNTIME_CLASS(CDownView),CSize((0,rect.bottom - rect.top)*0.7), pContext))
	{
		TRACE0("Failed to create second pane\n");
		return FALSE;
	}

    //激活输入焦点视
	SetActiveView((CView*)m_Splitter.GetPane(0,0));
	m_pUpView   = (CUpView*)m_Splitter.GetPane(1,0);

	m_Splitter.ShowWindow(SW_SHOW);
	m_Splitter.UpdateWindow();
    return true;
    //return CFrameWnd::OnCreateClient(lpcs, pContext);
}

void CMainFrame::OnFileOpen()
{
    // TODO: 在此添加命令处理程序代码
    //static char BASED_CODE szFilter[] = _T("BMP图片(*.bmp)|*.bmp|JPEG图片(*.jpg)|*.jpg|PNG图片(*.png)|*.png|All Files (*.*)|*.*||");
	
	CFileDialog dlg(TRUE, _T("bmp"), 0, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,
        _T("BMP图片(*.bmp)|*.bmp|JPEG图片(*.jpg)|*.jpg|PNG图片(*.png)|*.png|All Files (*.*)|*.*||"));
    dlg.m_ofn.lpstrTitle=_T("导入选择的图片");
	 
	if (dlg.DoModal() == IDOK) 
	{
		m_Pathname = dlg.GetPathName();
		m_pUpView->SendMessage(WM_PAINT,0,0 );
		this->Invalidate();
		this->UpdateWindow();
    }
}

void CMainFrame::OnMapOpen()
{
    // TODO: 在此添加命令处理程序代码
    CFileDialog dlg(false,_T("*.txt"),NULL,NULL,_T("*.txt|*.txt||"));
    dlg.m_ofn.lpstrTitle=_T("打开地图");
    if (dlg.DoModal()==IDOK)
    {
        CFile fp; 
        char temp[200]="";
        char m_num[4];
        int i,j,t,k;
        l=0;
        
        if(!fp.Open( dlg.GetPathName(), CFile::modeRead))
            MessageBox(_T("打开文件失败!"));
        for(i=0;;i++) {
            fp.Read(&temp[i],1);
            if(temp[i]=='\n')
                break;
        }
        m_name.Format("%s",temp);     

        while(1)
        {
            i=0;
            if(fp.Read(&temp[i++],1)==0)
                break;
            else for(;;i++) {
                fp.Read(&temp[i],1);
                if(temp[i]=='\n')
                   break;
            }
            for(j=0,k=0;j<i;) {
                for(t=0;temp[j]!=' '&&temp[j]!='\n';j++)
                    m_num[t++]=temp[j];
                m_num[t]='\n';
                num[l][k++]=atoi(m_num);
                if(temp[j]=' ') j++;
            }
            l++;
        }
        fp.Close();
        this->Invalidate();
    }
}

void CMainFrame::OnMapSave()
{
    // TODO: 在此添加命令处理程序代码
    CString   buffString,enter; 
    int i,j;
    CFileDialog dlg(false,_T("*.txt"),NULL,NULL,_T("*.txt|*.txt||"));
    dlg.m_ofn.lpstrTitle=_T("保存图片");
    if (dlg.DoModal()==IDOK)
    {
        CFile fp;
        enter="\r\n";
        fp.Open( dlg.GetPathName(), CFile::modeWrite|CFile::modeCreate );
        buffString=m_Pathname;
        fp.Write(buffString.GetBuffer(),buffString.GetLength()); 
        fp.Write(enter.GetBuffer(),enter.GetLength());
        for(i=0;i<x;i++) 
        {
            for(j=0;j<y;j++) 
            {
                if(m_mesh[i][j].i==1) 
            {
                buffString.Format(_T("%d "),i);
                fp.Write(buffString.GetBuffer(),buffString.GetLength());
                buffString.Format(_T("%d "),j);
                fp.Write(buffString.GetBuffer(),buffString.GetLength());
                buffString.Format(_T("%d "),m_mesh[i][j].point.x);
                fp.Write(buffString.GetBuffer(),buffString.GetLength());
                buffString.Format(_T("%d"),m_mesh[i][j].point.y);
                fp.Write(buffString.GetBuffer(),buffString.GetLength());
                fp.Write(enter.GetBuffer(),enter.GetLength());
                }
            }
        }
        fp.Close();
    }
}

void CMainFrame::OnEditUndo()
{
    // TODO: 在此添加命令处理程序代码
    if(l>0) l--;
    this->Invalidate();
}

void CMainFrame::OnEditDo()
{
    // TODO: 在此添加命令处理程序代码
    l++;
    this->Invalidate();
}

⌨️ 快捷键说明

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