?? window.cpp
字號:
// Window.cpp: implementation of the CWindow class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MixedCS.h"
#include "Window.h"
#include "MyFileDialog.h"
#include <shlobj.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWindow::CWindow()
{
wnd.m_hWnd = GetActiveWindow();
wnd.GetWindowText(caption);
}
CWindow::~CWindow()
{
EndWaitCursor();
wnd.m_hWnd = 0;//必須置0,否則Windows將關閉當前窗口
}
///////////////////////////////////////////////////////////////////////////////
// CWindows Functions
///////////////////////////////////////////////////////////////////////////////
#define STR_MAXLEN 1000
#define CHECK(x) {if( !(x) ) return false;}
#define CHECK_MSG(x,msg) {if( !(x) ) {ShowMessage(msg);return false;}}
/******************************************************************************/
// 名稱:ShowWaitCursor()
// 功能:顯示等待光標
// 參數:
// 返回:
// 備注:
// 更新:2003/1/25
// 作者:0200950原常青
/******************************************************************************/
void CWindow::ShowWaitCursor()
{
wnd.m_hWnd = GetActiveWindow();
wnd.GetWindowText(caption);
wnd.BeginWaitCursor();
}
/******************************************************************************/
// 名稱:EndWaitCursor
// 功能:結束等待光標
// 參數:
// 返回:
// 備注:
// 更新:2003/1/25
// 作者:0200950原常青
/******************************************************************************/
void CWindow::EndWaitCursor()
{
wnd.EndWaitCursor();
wnd.SetWindowText(caption);
}
/******************************************************************************/
// 名稱:SetWindowCaption
// 功能:設置窗口標題
// 參數:
// 返回:
// 備注:
// 更新:2003/1/25
// 作者:0200950原常青
/******************************************************************************/
void CWindow::SetWindowCaption(char *Str,int v1,int v2,int v3)
{
CString newname;
newname.Format(Str,v1,v2,v3);
wnd.SetWindowText(newname);
}
/******************************************************************************/
// 名稱:ShowMessage
// 功能:顯示消息框
// 參數:
// 返回:
// 備注:
// 更新:2003/1/25
// 作者:0200950原常青
/******************************************************************************/
int CWindow::ShowMessage(const char *msg,const char *title,UINT flage)
{
CHECK( msg && title )
return MessageBox(GetActiveWindow(),msg,title,flage);
}
/******************************************************************************/
// 名稱:InputKeyStr
// 功能:打開選擇文件對話框,導入字符串
// 參數:
// 返回:導入成功返回true,否則返回false
// 備注:
// 更新:2003/1/25
// 作者:0200950原常青
/******************************************************************************/
bool CWindow::InputStr(CString &Str)
{
CString Filter= "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||";
CMyFileDialog FileDlg (TRUE, NULL, NULL, OFN_HIDEREADONLY, Filter);
CHECK( FileDlg.DoModal()==IDOK )
char buf[STR_MAXLEN+1];
CHECK( GetStrFromFile(buf,FileDlg.GetPathName().GetBuffer(0)) )
Str = buf;
return true;
}
/******************************************************************************/
// 名稱:OutputKeyStr
// 功能:打開選擇文件對話框,導出字符串
// 參數:
// 返回:導出成功返回true,否則返回false
// 備注:
// 更新:2003/1/25
// 作者:0200950原常青
/******************************************************************************/
bool CWindow::OutputStr(CString &Str)
{
CHECK_MSG( !Str.IsEmpty(), "空內容! " )
CString Filter= "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||";
CMyFileDialog FileDlg(FALSE, "txt", NULL, OFN_HIDEREADONLY, Filter);
CHECK( FileDlg.DoModal()==IDOK )
CHECK( SaveStrToFile(Str.GetBuffer(0),FileDlg.GetPathName().GetBuffer(0)) )
return true;
}
/******************************************************************************/
// 名稱:GetFolder
// 功能:目錄選擇對話框
// 參數:
// 返回:選擇成功返回true,否則返回false
// 備注:
// 更新:2003/1/25
// 作者:0200950原常青
/******************************************************************************/
bool CWindow::GetFolder(CString &Folder)
{
BROWSEINFO bi;
ITEMIDLIST *pidl;
char Dir[256];
memset(&bi, 0, sizeof(bi));
bi.pszDisplayName = Dir;
bi.lpszTitle = "請選擇目錄";
bi.ulFlags = BIF_RETURNONLYFSDIRS;
pidl = SHBrowseForFolder(&bi);
CHECK( pidl!=NULL )
SHGetPathFromIDList(pidl,Dir);
GlobalFree(pidl);
Folder = Dir;
return true;
}
/******************************************************************************/
// 名稱:IsFileExist
// 功能:檢查文件是否存在
// 參數:
// 返回:存在返回true,否則返回false
// 備注:
// 更新:2003/1/25
// 作者:0201005劉紅旗
/******************************************************************************/
bool CWindow::IsFileExist(const char *File)
{
HFILE fh;
CHECK( File )
CHECK( (fh=_lopen(File,OF_READ)) != -1 )
_lclose(fh);
return true;
}
/******************************************************************************/
// 名稱:GetStrFromFile
// 功能:從文件中讀取字符串
// 參數:
// 返回:讀取成功返回true,否則返回false
// 備注:
// 更新:2003/1/25
// 作者:0201005劉紅旗
/******************************************************************************/
bool CWindow::GetStrFromFile(char *Str,const char *File)
{
CHECK( Str && File );
HFILE fh;
CHECK_MSG( (fh=_lopen(File,OF_READ)) != -1, "錯誤:無法打開文件! " )
int len = _lread(fh,Str,STR_MAXLEN);
Str[len] = '\0';
_lclose(fh);
return true;
}
/******************************************************************************/
// 名稱:SaveStrToFile
// 功能:保存字符串到文件中
// 參數:
// 返回:保存成功返回true,否則返回false
// 備注:
// 更新:2003/1/25
// 作者:0201005劉紅旗
/******************************************************************************/
bool CWindow::SaveStrToFile(const char *Str,const char *File)
{
CHECK( Str && File );
HFILE fh;
if( IsFileExist(File) )
CHECK( IDYES == ShowMessage("該文件已存在,是否覆蓋? ","提示",
MB_YESNO | MB_ICONQUESTION) )
CHECK_MSG( (fh=_lcreat(File,0)) != -1, "錯誤:無法創建文件! ")
_lwrite(fh,Str,strlen(Str));
_lclose(fh);
return true;
}
/******************************************************************************/
// 名稱:GetFileNameWithExt
// 功能:獲取文件名(帶擴展名)
// 參數:
// 返回:文件名(帶擴展名)
// 備注:
// 更新:2003/1/25
// 作者:0201005劉紅旗
/******************************************************************************/
CString CWindow::GetFileNameWithExt(const CString &File)
{
int i,j=File.GetLength();
CString Name;
for(i=j-1; i>=0 && File.GetAt(i)!='\\'; --i);
Name = (i<0 ) ? File : File.Right(j-i-1);
return Name;
}
/******************************************************************************/
// 名稱:GetFileNameNoExt
// 功能:獲取文件名(無擴展名)
// 參數:
// 返回:文件名(無擴展名)
// 備注:
// 更新:2003/1/25
// 作者:0201005劉紅旗
/******************************************************************************/
CString CWindow::GetFileNameNoExt(const CString &File)
{
CString Name = GetFileNameWithExt(File);
CString Ext = GetExtName(Name);
int len = Ext.IsEmpty() ? 0 : Ext.GetLength()+1;
Name = Name.Left(Name.GetLength()-len);
return Name;
}
/******************************************************************************/
// 名稱:GetExtName
// 功能:獲取擴展名
// 參數:
// 返回:擴展名
// 備注:
// 更新:2003/1/25
// 作者:0201005劉紅旗
/******************************************************************************/
CString CWindow::GetExtName(const CString &File)
{
int i,j=File.GetLength();
CString Ext;
for(i=0; i<j && File.GetAt(i)!='.'; ++i);
Ext = i<j ? File.Right(j-i-1) : "";
return Ext;
}
/******************************************************************************/
// 名稱:GetPath
// 功能:獲取路徑
// 參數:
// 返回:路徑
// 備注:
// 更新:2003/1/25
// 作者:0201005劉紅旗
/******************************************************************************/
CString CWindow::GetPath(const CString &File)
{
int i,j=File.GetLength();
CString Path;
for(i=j-1; i>=0 && File.GetAt(i)!='\\'; --i);
Path = (i<0 ) ? "" : File.Left(i);
return Path;
}
///////////////////////////////////////////////////////////////////////////////
// End of Files
///////////////////////////////////////////////////////////////////////////////
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -