📄 convertdlg.cpp
字号:
// convertDlg.cpp : implementation file
//
#include "stdafx.h"
#include "convert.h"
#include "convertDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#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()
/////////////////////////////////////////////////////////////////////////////
// CConvertDlg dialog
CConvertDlg::CConvertDlg(CWnd* pParent /*=NULL*/)
: CDialog(CConvertDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CConvertDlg)
m_strOutName = _T("");
m_strFrom2Name = _T("");
m_strFrom1Name = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CConvertDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CConvertDlg)
DDX_Text(pDX, IDC_EDIT3, m_strOutName);
DDX_Text(pDX, IDC_EDIT2, m_strFrom2Name);
DDX_Text(pDX, IDC_EDIT1, m_strFrom1Name);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CConvertDlg, CDialog)
//{{AFX_MSG_MAP(CConvertDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CConvertDlg message handlers
BOOL CConvertDlg::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
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CConvertDlg::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 CConvertDlg::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 CConvertDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CConvertDlg::OnOK()
{
BITMAPFILEHEADER head1,head2,head3;
BITMAPINFOHEADER info1,info2,info3;
TCHAR *pBuffer1,*pBuffer2,*pTemp;
CFile fileFrom1,fileFrom2,fileOut;
DWORD dwFrom1Length,dwFrom2Length,dwLength,dwHeight,dwWidth,dwSeek,dwCount=1;
if (!fileFrom1.Open(m_strFrom2Name,CFile::modeRead))
{
MessageBox("打开要隐藏显示的BMP位图文件失败!");
return;
}
if(!fileFrom2.Open(m_strFrom1Name,CFile::modeRead))
{
MessageBox("打开要正常显示的BMP位图文件失败!");
return;
}
if(!fileOut.Open(m_strOutName,CFile::modeCreate|CFile::modeWrite))
{
MessageBox("创建输出文件失败");
return;
}
//获得来源BMP位图文件的格式信息
fileFrom1.ReadHuge(&head1,sizeof(head1));
fileFrom2.ReadHuge(&head2,sizeof(head2));
fileFrom1.ReadHuge(&info1,sizeof(info1));
fileFrom2.ReadHuge(&info2,sizeof(info2));
if(info1.biBitCount<16||info2.biBitCount<16||info1.biBitCount!=info2.biBitCount)
{
MessageBox("BMP位图文件的色彩不正确");
return;
}
//计算BMP位图文件每一行数据需要多大的缓存空间
dwFrom1Length=((info1.biWidth*(info1.biBitCount/8)+7)/8)*8;
dwFrom2Length=((info2.biWidth*(info2.biBitCount/8)+7)/8)*8;
dwSeek=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
//如果来源的两个BMP文件的长、宽属性不一致,统一以最小的长宽数据为标准进行处理
if(info1.biWidth<info2.biWidth)dwWidth=info1.biWidth;
else dwWidth=info2.biWidth;
if(info1.biHeight<info2.biHeight)dwHeight=info1.biHeight;
else dwHeight=info2.biHeight;
head3=head1;
info3=info1;
info3.biWidth=dwWidth;
info3.biHeight=dwHeight;
//最后生成的BMP位图文件每一行数据需要多大的缓存空间
dwLength=(dwWidth*(info3.biBitCount/8)+7)/8*8;
pBuffer1=new TCHAR[dwFrom1Length];
pBuffer2=new TCHAR[dwFrom2Length];
pTemp=new TCHAR[dwLength];
//写生成的BMP位图文件头
fileOut.WriteHuge(&head3,sizeof(head3));
fileOut.WriteHuge(&info3,sizeof(info3));
for(int i=dwHeight-1;i>0;i--)
{
//读两个源BMP位图文件的每一行数据
fileFrom1.Seek(dwFrom1Length*i+dwSeek,CFile::begin);
fileFrom2.Seek(dwFrom2Length*i+dwSeek,CFile::begin);
fileFrom1.ReadHuge(pBuffer1,dwFrom1Length);
fileFrom2.ReadHuge(pBuffer2,dwFrom2Length);
if(dwCount%2)
{
for(int j=0;j<dwWidth;j++)
{
if(j%2)memcpy(pTemp+j*(info3.biBitCount/8),pBuffer1+j*(info1.biBitCount/8),info1.biBitCount/8);
else memcpy(pTemp+j*(info3.biBitCount/8),pBuffer2+j*(info2.biBitCount/8),info2.biBitCount/8);
}
}
else
{
for(int j=0;j<dwWidth;j++)
{
if(j%2)memcpy(pTemp+j*(info3.biBitCount/8),pBuffer2+j*(info2.biBitCount/8),info2.biBitCount/8);
else memcpy(pTemp+j*(info3.biBitCount/8),pBuffer1+j*(info1.biBitCount/8),info1.biBitCount/8);
}
}
dwCount++;
//写入数据
fileOut.Seek(dwLength*i+dwSeek,CFile::begin);
fileOut.WriteHuge(pTemp,dwLength);
}
delete[] pTemp;
delete[] pBuffer1;
delete[] pBuffer2;
//关闭文件
fileFrom1.Close();
fileFrom2.Close();
fileOut.Close();
MessageBox("OK",NULL,MB_OK);
}
void CConvertDlg::OnButton1()
{
UpdateData(TRUE);
CFileDialog file(TRUE);
file.m_ofn.Flags|=OFN_EXPLORER;
file.m_ofn.lpstrFilter="BMP文件(*.BMP)\0*.BMP";
if(file.DoModal()==IDOK)
{
m_strFrom1Name=file.GetPathName();
m_strFrom1Name.MakeUpper();
UpdateData(FALSE);
}
}
void CConvertDlg::OnButton2()
{
UpdateData(TRUE);
CFileDialog file(TRUE);
file.m_ofn.Flags|=OFN_EXPLORER;
file.m_ofn.lpstrFilter="BMP文件(*.BMP)\0*.BMP";
if(file.DoModal()==IDOK)
{
m_strFrom2Name=file.GetPathName();
m_strFrom2Name.MakeUpper();
UpdateData(FALSE);
}
}
void CConvertDlg::OnButton3()
{
UpdateData(TRUE);
CFileDialog file(FALSE);
file.m_ofn.Flags|=OFN_EXPLORER;
file.m_ofn.lpstrFilter="BMP文件(*.BMP)\0*.BMP";
if(file.DoModal()==IDOK)
{
m_strOutName=file.GetPathName();
m_strOutName.MakeUpper();
if(m_strOutName.Right(4).Compare(".BMP")!=0)m_strOutName+=".BMP";
UpdateData(FALSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -