📄 mappingconverterdlg.cpp
字号:
// MappingConverterDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MappingConverter.h"
#include "MappingConverterDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CMappingConverterDlg dialog
CMappingConverterDlg::CMappingConverterDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMappingConverterDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMappingConverterDlg)
m_fValue = 0.0f;
m_nradio = -1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMappingConverterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMappingConverterDlg)
DDX_Control(pDX, IDC_COMBO1, m_cbo);
DDX_Control(pDX, IDC_LIST1, m_lst);
DDX_Text(pDX, IDC_EDIT1, m_fValue);
DDX_Radio(pDX, IDC_RADIO1, m_nradio);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMappingConverterDlg, CDialog)
//{{AFX_MSG_MAP(CMappingConverterDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CONVERT, OnConvert)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMappingConverterDlg message handlers
BOOL CMappingConverterDlg::OnInitDialog()
{
// 设置默认选中的单选钮为“逻辑的”
m_nradio=0;
CDialog::OnInitDialog();
// 在系统菜单中添加“关于”菜单项
// IDM_ABOUTBOX 必须在系统命令的范围内
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);
}
}
//设置对话框的图标。当应用程序的主窗口不是对话框时框架会自动进行这个过程
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: 添加额外的初始化代码
// 在列表控件中添加两列:“单位”和“值”
m_lst.InsertColumn(0, "单位", LVCFMT_LEFT, 180, 0);
m_lst.InsertColumn(1, "值", LVCFMT_LEFT, 100, 1);
// 初始化列表控件中的项所用的字符串数组
char *szItemTexts[]={"MM_HIENGLISH (0.001英寸)",
"MM_LOENGLISH (0.01英寸)",
"MM_HIMETRIC (0.01毫米)",
"MM_LOMETRIC (0.1毫米)",
"MM_TEXT (象素)",
"MM_TWIPS (缇)",
"磅",
"英寸",
"厘米",
"水平对话框单位 (系统字体)",
"垂直对话框单位 (系统字体)"
};
// 初始化列表控件中的项
for (int i=0; i<11; i++)
{
m_lst.InsertItem(i, szItemTexts[i]);
}
// 设置组合框控件中的当前选择项为 MM_HIENGLISH
m_cbo.SetCurSel(0);
return TRUE;
// 除非将输入焦点设置为某一控件,否则消息处理函数OnInitDialog 应该返回真值
}
void CMappingConverterDlg::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 CMappingConverterDlg::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 CMappingConverterDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMappingConverterDlg::OnConvert()
{
// TODO: 在此增加控件通知处理代码
// 获得当前在组合框中的选择项
int nCurSel=m_cbo.GetCurSel();
// 如果选择项不为空...
if (nCurSel!=LB_ERR)
{
// 更新映射到控件的成员变量
UpdateData();
// 获得对话框的当前设备上下文,该设备上下文用于不同映射模式的单位转换
CClientDC dc(this);
// 如果选择了“逻辑英寸”,则将映射模式设置为 MM_TEXT
if (m_nradio==0)
{
dc.SetMapMode(MM_TEXT);
}
// 如果选择了“物理英寸”,则将映射模式设置为 MM_HIENGLISH
else
{
dc.SetMapMode(MM_HIENGLISH);
}
// 获得每英寸长度的象素值
CSize sz(10000, 10000);
dc.DPtoHIMETRIC(&sz);
double fPixelPerInch=10000./sz.cx*100*25.4;
// 获得用户需要进行转换的单位值
double fValue=m_fValue;
// 获取当前对话框基本单位,其高位字为垂直对话框基本单位,
// 低位字为水平对话框基本单位
long lDbUnits=GetDialogBaseUnits();
// nType 为 1 表示经过初步转换的单位为英寸,
// nType 为 2 表示经过初步转换的单位为象素
int nType;
// 根据用户所选择的当前单位对输入的值进行初步的转换
switch (nCurSel)
{
case 0: // MM_HIENGLISH
nType=1;
fValue/=1000;
break;
case 1: // MM_LOENGLISH
nType=1;
fValue/=100;
break;
case 2: // MM_HIMETRIC
nType=1;
fValue/=2540;
break;
case 3: // MM_LOMETRIC
nType=1;
fValue/=254;
break;
case 4: // MM_TEXT
nType=2;
break;
case 5: // MM_TWIPS
nType=1;
fValue/=1440;
break;
case 6: // 磅
nType=1;
fValue/=72;
break;
case 7: // 英寸
nType=1;
break;
case 8: // 厘米
nType=1;
fValue/=2.54;
break;
case 9: // 水平对话框单位
nType=2;
fValue=fValue/4.*LOWORD(lDbUnits);
break;
case 10: // 垂直对话框单位
nType=2;
fValue=fValue/8.*HIWORD(lDbUnits);
break;
}
//列表控件中第二列的输出项对应的字符串
CString str[11];
// 根据初步转换得到的结果生成在列表控件中给出的转换后的值
switch (nType)
{
case 1: // 英寸
{
// 得到对应的象素值
double fPixel=fValue*fPixelPerInch;
str[0].Format("%g", fValue*1000);
str[1].Format("%g", fValue*100);
str[2].Format("%g", fValue*25.4*100);
str[3].Format("%g", fValue*25.4*10);
str[4].Format("%g", fPixel);
str[5].Format("%g", fValue*72.*20);
str[6].Format("%g", fValue*72);
str[7].Format("%g", fValue);
str[8].Format("%g", fValue*2.54);
str[9].Format("%g", fPixel*4./LOWORD(lDbUnits));
str[10].Format("%g", fPixel*8./HIWORD(lDbUnits));
break;
}
case 2: // 象素
{
// 得到对应的英寸值
double fInch=fValue/fPixelPerInch;
str[0].Format("%g", fInch*1000);
str[1].Format("%g", fInch*100);
str[2].Format("%g", fInch*25.4*100);
str[3].Format("%g", fInch*25.4*10);
str[4].Format("%g", fValue);
str[5].Format("%g", fInch*72*20);
str[6].Format("%g", fInch*72);
str[7].Format("%g", fInch);
str[8].Format("%g", fInch*2.54);
str[9].Format("%g", fValue*4./LOWORD(lDbUnits));
str[10].Format("%g", fValue*8./HIWORD(lDbUnits));
break;
}
}
// 将转换后的值写到列表控件中对应项的第二列中
for (int i=0; i<11; i++)
{
m_lst.SetItem(i, 1, LVIF_TEXT, str[i], 0, 0, 0, LPARAM(0));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -