MyGui调用Win32窗口打开保存文件

[注]MyInfo这个类相关的,可以删除掉,在此没有用到,别的错误,基本上可以注释掉。

FileWindow.h

#ifndef __FILE_WINDOW_H__
#define __FILE_WINDOW_H__


#include <stdio.h>
#include <Windows.h>
#include <tchar.h>
#include <locale.h>
#include <ShlObj.h>		//选择文件夹 用到

//最大允许选择的文件数
#define	MAX_FILE_FOR_SEL 20	

namespace demo
{
	class FileWindow
	{
	public:
		FileWindow();
		~FileWindow();
	    bool  openWin32FileWindow();
		bool  saveFile();

		void showFile(LPCTSTR szFile);
		TCHAR szPathName[MAX_PATH*MAX_FILE_FOR_SEL];
		//OPENFILENAME ofn;

	};
}
#endif


FileWindow.cpp

#include "FileWindow.h"

namespace demo
{
	
   OPENFILENAME ofn = { OPENFILENAME_SIZE_VERSION_400 };//or  {sizeof (OPENFILENAME)}
	FileWindow::FileWindow()
	{
		//让wprintf 支持中文
		_tsetlocale(LC_CTYPE, TEXT(""));
	}


	FileWindow::~FileWindow()
	{

	}

	bool FileWindow::openWin32FileWindow()
	{
	
		
		// lStructSize
		// 指定这个结构的大小,以字节为单位。
		// Windows 95/98和Windows NT 4.0:特意为Windows 95/98或Windows NT 4.0,及带有WINVER和_WIN32_WINNT >= 0x0500编译时,
		//	为这个成员使用OPENFILENAME_SIZE_VERSION_400。
		// Windows 2000及更高版本:这个参数使用sizeof (OPENFILENAME) 。
		ofn.hwndOwner =GetForegroundWindow();// 打开OR保存文件对话框的父窗口
		ofn.lpstrFilter = TEXT("文本文件(*.txt)/0*.txt/0C/C++源文件(*.cpp;*.c;*.h)/0*.cpp;*.c;*.h/0All Files(*.*)/0*.*/0/0");	
		//过滤器 如果为 NULL 不使用过滤器
		//具体用法看上面  注意 /0
		lstrcpy(szPathName, TEXT(""));
		ofn.lpstrFile = szPathName;
		ofn.nMaxFile = sizeof(szPathName);//存放用户选择文件的 路径及文件名 缓冲区
		ofn.lpstrTitle = TEXT("选择打开的文件");//选择文件对话框标题
		TCHAR szCurDir[MAX_PATH];
		GetCurrentDirectory(sizeof(szCurDir),szCurDir);
		ofn.lpstrInitialDir=szCurDir;//设置对话框显示的初始目录
		ofn.Flags = OFN_EXPLORER |OFN_ALLOWMULTISELECT| OFN_FILEMUSTEXIST;//如果需要选择多个文件 则必须带有  OFN_ALLOWMULTISELECT标志
		_tprintf(TEXT("select file/n"));
		BOOL bOk = GetOpenFileName(&ofn);//调用对话框打开文件
		if (bOk)
		{
			showFile(szPathName);
		}
		//ofn.lpstrTitle = TEXT("选择打开的文件");//选择文件对话框标题

		return true;

	}

	bool  FileWindow::saveFile()
	{
		lstrcpy(szPathName, TEXT(""));
		ofn.lpstrTitle = TEXT("保存文件");//选择文件对话框标题fn.lpstrFile = szPathName;
		ofn.nMaxFile = sizeof(szPathName);//存放用户选择文件的 路径及文件名 缓冲区
		bool bOk=GetSaveFileName(&ofn);//调用对话框保存文件
		
		if (bOk)
		{
			showFile(szPathName);
			ofn.lpstrTitle = TEXT("保存文件");//选择文件对话框标题
			return true;
		}
        
		return false;
	}

	void FileWindow::showFile( LPCTSTR szFile )
	{
		_tprintf(TEXT("FileOrPath:%s/n"),szFile);
		LPCTSTR p=szFile;
		while (*(p++))
		{
		}
		if (*p==0)
		{
			_tprintf(TEXT("no Other File/n"));
			return ;
		}
		do 
		{
			_tprintf(TEXT("FileName:%s/n"),p);
			while (*(p++))
			{
			}
		} while (*p!=0);
		_tprintf(TEXT("ShowFile end/n"));

	}

}


DemoKeeper.h

/*!
	@file
	@author		Albert Semenov
	@date		09/2008
*/
#ifndef __DEMO_KEEPER_H__
#define __DEMO_KEEPER_H__

#include "BaseManager.h"
//#include "FontPanel.h"
#include "MyInfo.h"
#include"FileWindow.h"
namespace demo
{

	class DemoKeeper :
		public base::BaseManager
	{
	public:
		DemoKeeper();

		virtual void createScene();
		virtual void destroyScene();

		void notifyMouseButtonClick(MyGUI::Widget* _sender);

	private:
		virtual void setupResources();

	private:
		MyInfo* mMyInfo;
		MyGUI::Button* mOpenFile; 
		MyGUI::Button* mSaveFile;
		FileWindow* mFileWindow;
	};

} // namespace demo

#endif // __DEMO_KEEPER_H__


DemoKeeper.cpp


/*!
	@file
	@author		Albert Semenov
	@date		09/2008
*/
#include "Precompiled.h"
#include "DemoKeeper.h"
#include "Base/Main.h"

namespace demo
{

	DemoKeeper::DemoKeeper() 
	{
	}

	void DemoKeeper::setupResources()
	{
		base::BaseManager::setupResources();
		addResourceLocation(getRootMedia() + "/Tools/FontViewer");
		addResourceLocation(getRootMedia() + "/Common/Demos");
		addResourceLocation(getRootMedia() + "/Common/MessageBox");
		addResourceLocation(getRootMedia() + "/Demos/Demo_RenderBox");
		addResourceLocation(getRootMedia() + "/Demos/Demo_ItemBox");
		addResourceLocation(getRootMedia() + "/Common/Scene");

	}

	void DemoKeeper::createScene()
	{
		//MyGUI::ResourceManager::getInstance().load("MessageBoxResources.xml");

		//MyGUI::LayoutManager::getInstance().loadLayout("Wallpaper.layout");
		//const MyGUI::VectorWidgetPtr& root = MyGUI::LayoutManager::getInstance().loadLayout("HelpPanel.layout");
		//root.at(0)->findWidget("Text")->castType<MyGUI::TextBox>()->setCaption("Font generation utility with the ability to save to a MyGUI font configuration file.");

		
		mOpenFile = MyGUI::Gui::getInstance().createWidget<MyGUI::Button>("Button", MyGUI::IntCoord(50, 300, 150, 40), MyGUI::Align::Default, "Back");
		mOpenFile->setModeImage( true );
		//mOpenFile->setProperty( "Button_ImageResource", "pic_ButtonImage1" );
		mOpenFile->setCaption(L"open file window");
		mOpenFile->setVisible(true);

		mOpenFile->eventMouseButtonClick += MyGUI::newDelegate(this, &DemoKeeper::notifyMouseButtonClick);


		mSaveFile = MyGUI::Gui::getInstance().createWidget<MyGUI::Button>("Button", MyGUI::IntCoord(300, 300, 150, 40), MyGUI::Align::Default, "Back");
		mSaveFile->setModeImage( true );
		//mOpenFile->setProperty( "Button_ImageResource", "pic_ButtonImage1" );
		mSaveFile->setCaption(L"save file window");
		mSaveFile->setVisible(true);

		mSaveFile->eventMouseButtonClick += MyGUI::newDelegate(this, &DemoKeeper::notifyMouseButtonClick);

		mFileWindow = new FileWindow();
		return;

		MyGUI::LanguageManager::getInstance().load("MyInfo.xml");
		mMyInfo = new MyInfo();
		return;

	}

	void DemoKeeper::destroyScene()
	{

	}

	void DemoKeeper::notifyMouseButtonClick( MyGUI::Widget* _sender )
	{
		if(_sender == mOpenFile)
		{
			mFileWindow->openWin32FileWindow();

		}
		if (_sender == mSaveFile)
		{
			mFileWindow->saveFile();
		}

	}
} // namespace demo

MYGUI_APP(demo::DemoKeeper)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值