📄 makecfrombmpdlg.cpp
字号:
// MakeCFromBmpDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MakeCFromBmp.h"
#include "MakeCFromBmpDlg.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 };
CString m_ShowMsg;
//}}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)
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
m_ShowMsg = _T("");
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
DDX_Text(pDX, IDC_EDIT1, m_ShowMsg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMakeCFromBmpDlg dialog
CMakeCFromBmpDlg::CMakeCFromBmpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMakeCFromBmpDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMakeCFromBmpDlg)
// 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);
}
void CMakeCFromBmpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMakeCFromBmpDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMakeCFromBmpDlg, CDialog)
//{{AFX_MSG_MAP(CMakeCFromBmpDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnMakeC)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMakeCFromBmpDlg message handlers
BOOL CMakeCFromBmpDlg::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
D_file = "";
unt=1;
// unt_c[9]=("","INT8U","INT16U","INT8U","INT32U","INT8U","","","INT64U");
argc=3;
return TRUE; // return TRUE unless you set the focus to a control
}
void CMakeCFromBmpDlg::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 CMakeCFromBmpDlg::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();
}
//#define ErrorAbort(s) MessageBox(s," Error",MB_OK|MB_ICONSTOP);return
CString s;
CFile f;
if(!f.Open(PicPath,CFile::modeRead|CFile::typeBinary|CFile::shareExclusive))
{
//s.Format ("File open faile:%s",PicPath);
//ErrorAbort(s);
return;
}
long fsz=f.GetLength(),n;
BYTE *p_mem;
BITMAPINFOHEADER *p_bmi;
HBITMAP hBmp,pBmp;
BITMAP bmMetric;
if((p_mem=new BYTE[fsz])!=NULL)
p_bmi=(BITMAPINFOHEADER*)(p_mem+14);
else
{
//ErrorAbort("Alloc memory failed");
MessageBox("CreateDIBitmap failed");
}
if((n=f.Read(p_mem,fsz))<fsz)
{
s.Format("%1d/%1d bytes readed",n,fsz);
MessageBox(s);
}
f.Close();
CRect rc;
GetClientRect(&rc);
CClientDC dc(this);
CDC memDC;
hBmp=CreateDIBitmap(dc.m_hDC,p_bmi,CBM_INIT,p_mem+*(DWORD*)(p_mem+10),(BITMAPINFO*)p_bmi,DIB_RGB_COLORS);
if(hBmp!=NULL)
GetObject(hBmp,sizeof(BITMAP),&bmMetric);
else{
//ErrorAbort("CreateDIBitmap failed");
MessageBox("CreateDIBitmap failed");
}
memDC.CreateCompatibleDC (&dc);
pBmp=(HBITMAP)memDC.SelectObject (hBmp);
dc.BitBlt (10,12,rc.right ,rc.bottom ,&memDC,0,0,SRCCOPY);
memDC.SelectObject (pBmp);
memDC.DeleteDC ();
DeleteObject(hBmp);
if(p_mem!=NULL)
delete p_mem;
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMakeCFromBmpDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMakeCFromBmpDlg::exit_pro()
{
int s,d;
/*关闭文件*/
if ((s = fclose(fps))!=0)
s = fclose(fps);
if (s!=0)
MessageBox("Source file close error");
if ((d = fclose(fpd))!=0)
s = fclose(fpd);
if (d!=0)
MessageBox("Destination file close error");
}
long CMakeCFromBmpDlg::filesize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}
void CMakeCFromBmpDlg::PrintSingle()
{
char *chr[256]={
"00","01","02","03","04","05","06","07","08","09","0a","0b","0c","0d","0e","0f",
"10","11","12","13","14","15","16","17","18","19","1a","1b","1c","1d","1e","1f",
"20","21","22","23","24","25","26","27","28","29","2a","2b","2c","2d","2e","2f",
"30","31","32","33","34","35","36","37","38","39","3a","3b","3c","3d","3e","3f",
"40","41","42","43","44","45","46","47","48","49","4a","4b","4c","4d","4e","4f",
"50","51","52","53","54","55","56","57","58","59","5a","5b","5c","5d","5e","5f",
"60","61","62","63","64","65","66","67","68","69","6a","6b","6c","6d","6e","6f",
"70","71","72","73","74","75","76","77","78","79","7a","7b","7c","7d","7e","7f",
"80","81","82","83","84","85","86","87","88","89","8a","8b","8c","8d","8e","8f",
"90","91","92","93","94","95","96","97","98","99","9a","9b","9c","9d","9e","9f",
"a0","a1","a2","a3","a4","a5","a6","a7","a8","a9","aa","ab","ac","ad","ae","af",
"b0","b1","b2","b3","b4","b5","b6","b7","b8","b9","ba","bb","bc","bd","be","bf",
"c0","c1","c2","c3","c4","c5","c6","c7","c8","c9","ca","cb","cc","cd","ce","cf",
"d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","da","db","dc","dd","de","df",
"e0","e1","e2","e3","e4","e5","e6","e7","e8","e9","ea","eb","ec","ed","ee","ef",
"f0","f1","f2","f3","f4","f5","f6","f7","f8","f9","fa","fb","fc","fd","fe","ff"
};
if (j==24)
{
fprintf (fpd,"\n ");
j=0;
}
j++;
fprintf(fpd,",");
fprintf(fpd,"0x");
fprintf(fpd,"f0");
if (j==24)
{
fprintf (fpd,"\n ");
j=0;
}
j++;
fprintf(fpd,",");
fprintf(fpd,"0x");
fprintf(fpd,"%s",chr[(buf_CompCnt[7] + 4)] ); //重复个数
if (j==24)
{
fprintf (fpd,"\n ");
j=0;
}
j++;
fprintf(fpd,",");
fprintf(fpd,"0x");
fprintf(fpd,"%s",chr[se_list[buf_char[0]]]);
if (j==24)
{
fprintf (fpd,"\n ");
j=0;
}
j++;
fprintf(fpd,",");
fprintf(fpd,"0x");
fprintf(fpd,"f0"); //填充0xf0
CompCnt +=4;
}
void CMakeCFromBmpDlg::PrintDual()
{
char *chr[256]={
"00","01","02","03","04","05","06","07","08","09","0a","0b","0c","0d","0e","0f",
"10","11","12","13","14","15","16","17","18","19","1a","1b","1c","1d","1e","1f",
"20","21","22","23","24","25","26","27","28","29","2a","2b","2c","2d","2e","2f",
"30","31","32","33","34","35","36","37","38","39","3a","3b","3c","3d","3e","3f",
"40","41","42","43","44","45","46","47","48","49","4a","4b","4c","4d","4e","4f",
"50","51","52","53","54","55","56","57","58","59","5a","5b","5c","5d","5e","5f",
"60","61","62","63","64","65","66","67","68","69","6a","6b","6c","6d","6e","6f",
"70","71","72","73","74","75","76","77","78","79","7a","7b","7c","7d","7e","7f",
"80","81","82","83","84","85","86","87","88","89","8a","8b","8c","8d","8e","8f",
"90","91","92","93","94","95","96","97","98","99","9a","9b","9c","9d","9e","9f",
"a0","a1","a2","a3","a4","a5","a6","a7","a8","a9","aa","ab","ac","ad","ae","af",
"b0","b1","b2","b3","b4","b5","b6","b7","b8","b9","ba","bb","bc","bd","be","bf",
"c0","c1","c2","c3","c4","c5","c6","c7","c8","c9","ca","cb","cc","cd","ce","cf",
"d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","da","db","dc","dd","de","df",
"e0","e1","e2","e3","e4","e5","e6","e7","e8","e9","ea","eb","ec","ed","ee","ef",
"f0","f1","f2","f3","f4","f5","f6","f7","f8","f9","fa","fb","fc","fd","fe","ff"
};
if (j==24)
{
fprintf (fpd,"\n ");
j=0;
}
j++;
fprintf(fpd,",");
fprintf(fpd,"0x");
fprintf(fpd,"f0");
if (j==24)
{
fprintf (fpd,"\n ");
j=0;
}
j++;
fprintf(fpd,",");
fprintf(fpd,"0x");
fprintf(fpd,"%s",chr[((buf_CompCnt[8]+1)/2) + 2] ); //重复个数
if (j==24)
{
fprintf (fpd,"\n ");
j=0;
}
j++;
fprintf(fpd,",");
fprintf(fpd,"0x");
fprintf(fpd,"%s",chr[se_list[buf_char[0]]]);
if (j==24)
{
fprintf (fpd,"\n ");
j=0;
}
j++;
fprintf(fpd,",");
fprintf(fpd,"0x");
fprintf(fpd,"%s",chr[se_list[buf_char[1]]]);
CompCnt +=4;
}
void CMakeCFromBmpDlg::OnOK()
{
CFileDialog ofn(TRUE,"bmp",NULL,NULL,"BMP file(*.bmp)|*.BMP|",NULL); //定义打开文件的窗口
ofn.DoModal(); //激活文件窗口
m_path=ofn.GetPathName(); //获得选择的文件路径
FileName=ofn.GetFileName();
//------------------------------------------------------------------------------------
//显示图片
#define ErrorAbort(s) MessageBox(s," Error",MB_OK|MB_ICONSTOP);return
CString fn=m_path;
PicPath=fn;
CString m_PathShow=fn;
CString s;
CFile f;
if(!f.Open(fn,CFile::modeRead|CFile::typeBinary|CFile::shareExclusive))
{
s.Format ("File open faile:%s",fn);
ErrorAbort(s);
}
long fsz=f.GetLength(),n;
BYTE *p_mem;
BITMAPINFOHEADER *p_bmi;
HBITMAP hBmp,pBmp;
BITMAP bmMetric;
if((p_mem=new BYTE[fsz])!=NULL)
p_bmi=(BITMAPINFOHEADER*)(p_mem+14);
else {ErrorAbort("Alloc memory failed");}
if((n=f.Read(p_mem,fsz))<fsz)
{
s.Format("%1d/%1d bytes readed",n,fsz);
MessageBox(s);
}
f.Close();
CRect rc;
GetClientRect(&rc);
CClientDC dc(this);
CDC memDC;
hBmp=CreateDIBitmap(dc.m_hDC,p_bmi,CBM_INIT,p_mem+*(DWORD*)(p_mem+10),(BITMAPINFO*)p_bmi,DIB_RGB_COLORS);
if(hBmp!=NULL)
GetObject(hBmp,sizeof(BITMAP),&bmMetric);
else
{
ErrorAbort("CreateDIBitmap failed");
}
memDC.CreateCompatibleDC (&dc);
pBmp=(HBITMAP)memDC.SelectObject (hBmp);
dc.BitBlt (10,12,rc.right ,rc.bottom ,&memDC,0,0,SRCCOPY);
memDC.SelectObject (pBmp);
memDC.DeleteDC ();
DeleteObject(hBmp);
if(p_mem!=NULL)
delete p_mem;
//------------------------------------------------------------------------------------
m_path = m_path.Left(m_path.GetLength() - FileName.GetLength()); //得到目标目录路径
S_file = FileName;
D_file = FileName.Left (FileName.GetLength () - 4);
D_file += ".C";
D_filePath = m_path + D_file;
}
void CMakeCFromBmpDlg::OnMakeC()
{
char ctmp[3];
char *chr[256]={
"00","01","02","03","04","05","06","07","08","09","0a","0b","0c","0d","0e","0f",
"10","11","12","13","14","15","16","17","18","19","1a","1b","1c","1d","1e","1f",
"20","21","22","23","24","25","26","27","28","29","2a","2b","2c","2d","2e","2f",
"30","31","32","33","34","35","36","37","38","39","3a","3b","3c","3d","3e","3f",
"40","41","42","43","44","45","46","47","48","49","4a","4b","4c","4d","4e","4f",
"50","51","52","53","54","55","56","57","58","59","5a","5b","5c","5d","5e","5f",
"60","61","62","63","64","65","66","67","68","69","6a","6b","6c","6d","6e","6f",
"70","71","72","73","74","75","76","77","78","79","7a","7b","7c","7d","7e","7f",
"80","81","82","83","84","85","86","87","88","89","8a","8b","8c","8d","8e","8f",
"90","91","92","93","94","95","96","97","98","99","9a","9b","9c","9d","9e","9f",
"a0","a1","a2","a3","a4","a5","a6","a7","a8","a9","aa","ab","ac","ad","ae","af",
"b0","b1","b2","b3","b4","b5","b6","b7","b8","b9","ba","bb","bc","bd","be","bf",
"c0","c1","c2","c3","c4","c5","c6","c7","c8","c9","ca","cb","cc","cd","ce","cf",
"d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","da","db","dc","dd","de","df",
"e0","e1","e2","e3","e4","e5","e6","e7","e8","e9","ea","eb","ec","ed","ee","ef",
"f0","f1","f2","f3","f4","f5","f6","f7","f8","f9","fa","fb","fc","fd","fe","ff"
};
if (D_file == "")
{
MessageBox("请选择要转换的图片!!");
return;
}
if (argc > 1)
{
s_file = S_file.GetBuffer(S_file.GetLength());
d_file=D_file.GetBuffer(D_file.GetLength());
if (argc > 2)
{
unt = 1; //暂时强制为1,以后完善了功能再开放给用户选择
untbuf = unt;
if ((unt < 1)||(unt > 5)) unt=1;
if (unt == 5)
{
unt = 1;
}
}
/*
else
{
s_file = argv[1];
strcpy (tmp,argv[1]);
d_file = tmp;
}
*/
}
/*打开文件*/
fps=fopen(s_file,"rb");
if (fps==NULL)
{
strcat(s_file, ".bmp");
fps=fopen(s_file,"rb");
if(fps==NULL)
{
MessageBox("文件打开失败!");/*提示打开源文件不成功*/
return;
}
}
fpd=fopen (D_filePath, "r"); //用只读方式打开C文件
if (fpd!=NULL) //如果文件存在
{
fclose(fpd);
/*
printf("\n the %s exist, W will overwrite, other key will exit!",d_file); //询问操作类型
if (!((getch()=='w')||(getch()=='W'))) //获得键盘输入
{
puts("\n convert fail!"); //假如输入错误则提示并退出
return;
}
*/
}
fpd=fopen (D_filePath, "w"); //重新以只写方式打开C文件
if (fpd==NULL)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -