📄 codecwizarddlg.cpp
字号:
// CodecWizardDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CodecWizard.h"
#include "CodecWizardDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#ifdef CSLCS_TRACE_CODEC
DiskFile TraceFile;
char g_buf[512];
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCodecWizardDlg dialog
CCodecWizardDlg::CCodecWizardDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCodecWizardDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCodecWizardDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_hAccel = NULL;
m_bInitOK = FALSE;
m_nMinWidth = 0;
m_nMinHeight = 0;
}
void CCodecWizardDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCodecWizardDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCodecWizardDlg, CDialog)
//{{AFX_MSG_MAP(CCodecWizardDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_PASTE, OnPaste)
ON_BN_CLICKED(IDC_COPY, OnCopy)
ON_BN_CLICKED(IDC_DECODE, OnDecode)
ON_WM_SIZE()
ON_WM_SIZING()
ON_COMMAND(IDC_ALLDONE, OnAlldone)
ON_COMMAND(IDC_ALLUNDO, OnAllundo)
ON_COMMAND(IDC_SHOW, OnShow)
ON_MESSAGE(WM_HOTKEY,OnHotkey)
ON_BN_CLICKED(IDC_FILE, OnFile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCodecWizardDlg message handlers
BOOL CCodecWizardDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CListBox *pb = (CListBox *)GetDlgItem(IDC_METHODS);
pb->AddString("hex <-> bin");
pb->AddString("hex <-> C array");
pb->AddString("URL <-> URI");
pb->AddString("UTF7 <-> Binary(Base64 codec)");
pb->AddString("Run-Length Encoding <-> original(RLE codec)");
pb->AddString("RLE-hex <-> original(RLEHex codec)");
pb->AddString("Trans8 <-> Original(T8 codec)");
pb->AddString("Trans2s <-> Original(T2 codec)");
pb->AddString("Trans1Cycle <-> Original(T1 codec)");
pb->SetCurSel(4);
m_bInitOK = TRUE;
CRect rc;
GetClientRect(rc);
m_nMinWidth = rc.right - rc.left;
m_nMinHeight = rc.bottom - rc.top;
m_hotKey.Register(m_hWnd,MOD_WIN,'1',IDC_SHOW);
m_Decode.Register(m_hWnd,MOD_WIN,'A',IDC_DECODE);
m_Encode.Register(m_hWnd,MOD_WIN,'Q',IDOK);
return TRUE; // return TRUE unless you set the focus to a control
}
void CCodecWizardDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CCodecWizardDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCodecWizardDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCodecWizardDlg::OnCancel()
{
m_hotKey.Unregister();
m_Encode.Unregister();
m_Decode.Unregister();
CDialog::OnCancel();
}
BOOL CCodecWizardDlg::PreTranslateMessage(MSG* pMsg)
{
if(m_hAccel && TranslateAccelerator(m_hWnd,m_hAccel,pMsg)) return TRUE;
return CDialog::PreTranslateMessage(pMsg);
}
void CCodecWizardDlg::OnPaste()
{
CEdit *pe = (CEdit *)GetDlgItem(IDC_INPUT);
pe->SetSel(0,pe->GetWindowTextLength());
pe->ReplaceSel("",FALSE);
pe->Paste();
}
void CCodecWizardDlg::OnCopy()
{
CEdit *pe = (CEdit *)GetDlgItem(IDC_OUTPUT);
pe->SetSel(0,pe->GetWindowTextLength());
pe->Copy();
}
MIMECodec* AllocCodec(int nCodec)
{
switch(nCodec)
{
case CODEC_HEX:
return new HexCodec;
case CODEC_HEX2C:
return new Hex2CCodec;
case CODEC_URL:
return new URLCodec;
case CODEC_UTF7://BASE64
return new UTF7Codec;
case CODEC_RLE:
return new RLECodec;
case CODEC_RLEHEX:
return new RLEHexCodec;
case CODEC_TRANS8:
return new Trans8Codec;
case CODEC_TRANS2S:
return new Trans2sCodec;
case CODEC_TRANS1CYCLE:
return new Trans1CycleCodec;
default:
return NULL;
}
}
void CCodecWizardDlg::OnOK()
{
CListBox *method = (CListBox *)GetDlgItem(IDC_METHODS);
int nMethod = method->GetCurSel();
Destructor<CCSAutoMem>mi(GetInput(nMethod,TRUE));
#ifdef CSLCS_TRACE_CODEC
TraceFile.Open("Encode.txt",GENERIC_WRITE,CREATE_ALWAYS);
#endif
Destructor<MIMECodec>c(*AllocCodec(nMethod));
ByteArray byOut;
if(mi->m_nSize>1024*10) byOut.SetGrowSize(8*1024);
#ifdef CSLCS_TRACE_CODEC
TraceFile.Write("开始编码\n");
#endif
int nLen = c->Encode(mi->m_pBuf,mi->m_nSize,byOut);
#ifdef CSLCS_TRACE_CODEC
TraceFile.Write("编码结束\n");
TraceFile.Close();
#endif
SetOutput(byOut,nMethod,TRUE);
}
void CCodecWizardDlg::OnDecode()
{
CListBox *method = (CListBox *)GetDlgItem(IDC_METHODS);
int nMethod = method->GetCurSel();
Destructor<CCSAutoMem>mi(GetInput(nMethod,TRUE));
#ifdef CSLCS_TRACE_CODEC
TraceFile.Open("Decode.txt",GENERIC_WRITE,CREATE_ALWAYS);
#endif
CCSAutoMem mo(mi->m_nSize * 4 + 4);
Destructor<MIMECodec>c(*AllocCodec(nMethod));
ByteArray byOut;
if(mi->m_nSize>1024*10) byOut.SetGrowSize(8*1024);
#ifdef CSLCS_TRACE_CODEC
TraceFile.Write("开始解码\n");
#endif
int nLen = c->Decode(mi->m_pBuf,mi->m_nSize,byOut);
#ifdef CSLCS_TRACE_CODEC
TraceFile.Write("解码结束\n");
TraceFile.Close();
#endif
SetOutput(byOut,nMethod,FALSE);
}
void CCodecWizardDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if(!m_bInitOK) return;
CWnd *wnd = GetDlgItem(IDCANCEL);
CRect rc;
GetClientRect(rc);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -