📄 dotmatrixdlg.cpp
字号:
// DotMatrixDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DotMatrix.h"
#include "DotMatrixDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CDotMatrixDlg dialog
CDotMatrixDlg::CDotMatrixDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDotMatrixDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDotMatrixDlg)
m_text = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDotMatrixDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDotMatrixDlg)
DDX_Text(pDX, IDC_EDIT1, m_text);
DDV_MaxChars(pDX, m_text, 2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDotMatrixDlg, CDialog)
//{{AFX_MSG_MAP(CDotMatrixDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDotMatrixDlg message handlers
BOOL CDotMatrixDlg::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 CDotMatrixDlg::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 CDotMatrixDlg::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 CDotMatrixDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/*************************************************************
* 参数说明: x, 绘制字符的起始位置
* y, 同上
* nChar, 要绘制的单个字符,可以是中文或者英文
* clr, 绘制字符用的颜色,在这个例子里我们没有
* 编写对应的代码
*************************************************************/
void CDotMatrixDlg::DrawChar(int x, int y, char *nChar, COLORREF clr)
{
register i,j,k; // for quick loop
unsigned long location = 0; // for chinese chars's location
unsigned char qh = 0,
wh = 0;
int nGridStep = 8; // step when draw big char
char *p_chs16 = "CHS16", // 16*16 chinese dotmatrix lib
*p_eng16 = "ASC16"; // 16*16 english dotmatrix lib
FILE *fp_chs16, // file pointer to upon files
*fp_eng16;
char nDot_chs16[32], // 32B dotmatrix buffer
nDot_eng16[16]; // 16B dotmatrix buffer
CClientDC dc(this);
if(sizeof(*nChar) > 2) // verify the length of nChar
return;
// open the dotmatrix file
if( (NULL == (fp_chs16 = fopen(p_chs16, "rb"))) |
(NULL == (fp_eng16 = fopen(p_eng16, "rb"))))
{
return;
}
// clear data buffer
memset(nDot_chs16, 0, sizeof(nDot_chs16));
memset(nDot_eng16, 0, sizeof(nDot_eng16));
if((unsigned char)*nChar > 128)
// draw chinese dotmatrix
{
qh = *nChar - 0xA0;
wh = *(nChar + 1) - 0xA0;
location = (94 * (qh - 1) + (wh - 1)) * 32L; // get the start position of current char's dot-matrix
fseek(fp_chs16, location, SEEK_SET); // move file pointer to there
fread(nDot_chs16, sizeof(nDot_chs16), 1, fp_chs16); // read the dot-matrix of current char.
for(i = 0; i < 16; i++)
for(j = 0; j < 2; j++)
for(k = 0; k < 8; k++)
{
if(((nDot_chs16[i*2 + j] >> (7-k)) & 0x1) != NULL)
{
dc.Ellipse( (x + 8*j+k)*nGridStep,
(y+i)*nGridStep,
(x + 8*j+k+1)*nGridStep,
(y+i+1)*nGridStep);
}
}
}
else
// 8*16 english
{
location = nChar[0] << 4 ; // 16 byres each char in 5*8 english
fseek(fp_eng16, location, SEEK_SET);
fread(nDot_eng16, sizeof(nDot_eng16), 1, fp_eng16);
for(i = 0; i < 16; i++)
for(k = 0; k < 8; k++)
{
if(((nDot_eng16[i] >> (7-k)) & 0x1) != NULL)
dc.Ellipse( (x + k)*nGridStep,
(y + i)*nGridStep,
(x + k + 1)*nGridStep,
(y + i + 1)*nGridStep);
}
}
fclose(fp_chs16); // don't forget to close the files.
fclose(fp_eng16);
}
void CDotMatrixDlg::OnButton1()
{
// TODO: Add your control notification handler code here
RedrawWindow(); // clear pre-draws
UpdateData(TRUE); // get the user inputs
DrawChar(5, 15, (LPSTR)(LPCTSTR)m_text, RGB(0, 0, 0));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -