MFC标签设计工具 图片控件上,移动鼠标显示图片控件内的鼠标xy的水平和垂直辅助线要在标签模板上加上文字、条型码、二维码 找准坐标和字体大小 源码
// CDlgLableDesign.cpp: 实现文件
//
#include "pch.h"
#include "afxdialogex.h"
#include "CDlgLableDesign.h"
#include "Resource.h"
#define MAX_SCROLL_POS 2000
typedef struct Para
{
char templateName[200];
LONG fontHeight=82;
CPoint point;
}Para;
static Para para = {0};
// CDlgLableDesign 对话框
IMPLEMENT_DYNAMIC(CDlgLableDesign, CDialogEx)
CDlgLableDesign::CDlgLableDesign(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_DIALOG_LabelDesign, pParent)
{
}
CDlgLableDesign::~CDlgLableDesign()
{
}
void CDlgLableDesign::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_LOG2, m_editDisplay);
//DDX_Control(pDX, IDC_STATIC_IMAGE, m_pictureCtrl);
}
BEGIN_MESSAGE_MAP(CDlgLableDesign, CDialogEx)
ON_BN_CLICKED(IDC_BUTTON_PRINT_OUTBOX, &CDlgLableDesign::OnBnClickedButtonPrintOutbox)
ON_WM_TIMER()
// ON_WM_MOUSEMOVE()
// ON_WM_PAINT()
ON_STN_CLICKED(IDC_STATIC_IMAGE, &CDlgLableDesign::OnClickedStaticImage)
//ON_WM_VSCROLL()
ON_WM_CLOSE()
END_MESSAGE_MAP()
void CDlgLableDesign::Log(CString szInfo)
{
logger.INFO_F(szInfo.GetBuffer(0));
CString strInfo;
m_editDisplay.GetWindowText(strInfo);
int nLength = strInfo.GetLength();
m_editDisplay.SetSel(nLength, nLength, FALSE);
m_editDisplay.SetFocus();
m_editDisplay.SetSel(-1, -1);
m_editDisplay.ReplaceSel(szInfo + "\r\n");
}
// CDlgLableDesign 消息处理程序
BOOL CDlgLableDesign::LoadImageToPictureControl(CString bmpPath)
{
CStatic* pPictureControl = (CStatic*)GetDlgItem(IDC_STATIC_IMAGE);
if (pPictureControl == nullptr || bmpPath.IsEmpty())
return FALSE;
// 加载图片
CImage image;
HRESULT hr = image.Load(bmpPath);
if (FAILED(hr))
{
Log("加载图片失败"+ bmpPath);
return FALSE;
}
// 获取图片大小
int nWidth = image.GetWidth();
int nHeight = image.GetHeight();
//Log("宽高 "+CString(to_string(nWidth).c_str())+" * " + CString(to_string(nHeight).c_str()) +" 转为mm " + CString(to_string(nWidth* PixToMm600).c_str()) + " * " + CString(to_string(nHeight * PixToMm600).c_str()));
// 调整控件大小以适应图片
//CRect rect;
//pPictureControl->GetWindowRect(&rect);
pPictureControl->MoveWindow(208, 10, nWidth, nHeight);
// 获取控件的DC
CDC* pDC = pPictureControl->GetDC();
if (pDC == nullptr)
return FALSE;
// 创建兼容DC
CDC dcImage;
if (!dcImage.CreateCompatibleDC(pDC))
return FALSE;
// 选择图片到DC
CBitmap bmp;
bmp.Attach(image.Detach());
CBitmap* pOldBmp = dcImage.SelectObject(&bmp);
// 将图片绘制到控件上
pDC->BitBlt(0, 0, nWidth, nHeight, &dcImage, 0, 0, SRCCOPY);
// 清理
dcImage.SelectObject(pOldBmp);
pPictureControl->ReleaseDC(pDC);
//CImage image;
//HRESULT hResult = image.Load(_T(bmpPath)); // 加载图片
//if (SUCCEEDED(hResult))
//{
// // 获取Picture Control的句柄
// CStatic* pPictureControl = (CStatic*)GetDlgItem(IDC_STATIC_IMAGE);
// // 获取控件大小
// CRect rect;
// pPictureControl->GetClientRect(&rect);
// int controlWidth = rect.Width();
// int controlHeight = rect.Height();
// // 获取图片原始尺寸
// int imageWidth = image.GetWidth();
// int imageHeight = image.GetHeight();
// // 创建兼容的DC
// CDC* pDC = pPictureControl->GetDC();
// CDC memDC;
// memDC.CreateCompatibleDC(pDC);
// // 创建兼容的位图,并选入内存DC
// CBitmap bmp;
// bmp.CreateCompatibleBitmap(pDC, controlWidth, controlHeight);
// CBitmap* pOldbmp = memDC.SelectObject(&bmp);
// // 清空背景
// memDC.FillSolidRect(&rect, pDC->GetBkColor());
// // 使用CImage绘制到内存DC,这里使用缩放
// image.Draw(memDC.m_hDC, 0, 0, controlWidth, controlHeight, 0, 0, imageWidth, imageHeight);
// // 将内存DC绘制到控件上
// pDC->BitBlt(0, 0, controlWidth, controlHeight, &memDC, 0, 0, SRCCOPY);
// // 清理
// memDC.SelectObject(pOldbmp);
// ReleaseDC(pDC);
//}
//else
//{
// Log("图片加载失败:" + bmpPath);
//}
return TRUE;
}
BOOL ReadPara()
{
CString DebugPara = "";
GetPrivateProfileString("DEBUG", "DebugPara", "", DebugPara.GetBuffer(2000), 2000, configFileName); DebugPara.ReleaseBuffer();
if (DebugPara.GetLength() > 0)
{
utlStrToHex((BYTE*)¶, DebugPara.GetBuffer(), DebugPara.GetLength() / 2);
return TRUE;
}
return FALSE;
}
BOOL WritePara()
{
char DebugPara[4000] = {0};
utlHexToStr(DebugPara, (BYTE*)¶, sizeof(para));
if (WritePrivateProfileString("DEBUG", "DebugPara", DebugPara, configFileName))
{
return TRUE;
}
return FALSE;
}
BOOL CDlgLableDesign::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetTimer(1, 100, NULL);
ShowWindow(SW_MAXIMIZE);
// 启用垂直滚动条
SetScrollRange(SB_VERT, 0, MAX_SCROLL_POS); // 假设滚动范围是0到200
//log位置自动长度
CRect rect;
GetDlgItem(IDC_EDIT_LOG2)->GetWindowRect(&rect);
GetClientRect(&rect);
rect.left = 8;
rect.top = 70;
rect.right = 8+195;
rect.bottom = rect.bottom -10;
GetDlgItem(IDC_EDIT_LOG2)->MoveWindow(&rect);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CDlgLableDesign::OnBnClickedButtonPrintOutbox()
{
CFileDialog fileDlg(TRUE, _T("bmp"), NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, _T("Bitmap Files (*.bmp)|*.bmp||"));
if (fileDlg.DoModal() == IDOK)
{
memset((char*) & para, 0, sizeof(para));
memcpy(para.templateName, fileDlg.GetPathName().GetBuffer(), fileDlg.GetPathName().GetLength());
WritePara();
Log("加载模板:" + CString(para.templateName));
LoadImageToPictureControl(para.templateName); // 加载图片
}
}
void CDlgLableDesign::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (nIDEvent == 1)
{
if (ReadPara())
{
if (strlen(para.templateName) > 0)
{
Log("加载模板:" + CString(para.templateName));
LoadImageToPictureControl(para.templateName); // 加载图片
}
}
KillTimer(1); // 销毁定时器
}
CDialogEx::OnTimer(nIDEvent);
}
//void CDlgLableDesign::OnMouseMove(UINT nFlags, CPoint point)
//{
// TODO: 在此添加消息处理程序代码和/或调用默认值
// 获取图片控件的位置和大小
// CRect rect;
// CStatic* pPictureControl = (CStatic*)GetDlgItem(IDC_STATIC_IMAGE);
// pPictureControl->GetWindowRect(&rect);
// ScreenToClient(&rect);
//
// 计算鼠标相对于图片控件的坐标
// int x = point.x - rect.left;
// int y = point.y - rect.top;
//
// 在log中输出坐标
// CString str;
// str.Format(_T("x: %d, y: %d"), x, y);
//
//
//
// 判断点是否在矩形内
// if (rect.PtInRect(point)) {
获取鼠标的当前位置
//CPoint mousePos = point;
绘制水平和垂直辅助线
//CClientDC dc(this);
//CPen pen(PS_SOLID, 1, RGB(255, 0, 0)); // 红色辅助线
//dc.SelectObject(&pen);
//dc.MoveTo(mousePos.x, 0);
//dc.LineTo(mousePos.x, rect.Height());
//dc.MoveTo(0, mousePos.y);
//dc.LineTo(rect.Width(), mousePos.y);
重绘图片控件
//Invalidate();
//CClientDC dc(this);
设置字体
//CFont font;
//font.CreatePointFont(100, _T("Arial"), &dc);
//CFont* pOldFont = dc.SelectObject(&font);
使用TextOut函数在鼠标位置下方绘制文本
//dc.TextOut(point.x, point.y + 20, str);
清除原来的文本
CRect rect;
GetClientRect(&rect);
InvalidateRect(rect);
恢复原始字体
//dc.SelectObject(pOldFont);
Invalidate();
// }
//Log(str);
// CDialogEx::OnMouseMove(nFlags, point);
//}
void CDlgLableDesign::OnClickedStaticImage()
{
::GetCursorPos(¶.point); // 获取当前鼠标位置
ScreenToClient(¶.point); // 将屏幕坐标转换为窗口坐标
ShowImage();
}
void CDlgLableDesign::ShowImage()
{
LoadImageToPictureControl(para.templateName); // 加载图片
// 获取图片控件的位置和大小
CRect rect;
CStatic* pPictureControl = (CStatic*)GetDlgItem(IDC_STATIC_IMAGE);
pPictureControl->GetWindowRect(&rect);// 获取控件位置
ScreenToClient(&rect); // 将屏幕坐标转换为窗口坐标
// 计算鼠标相对于图片控件的坐标
int x = para.point.x - rect.left;
int y = para.point.y - rect.top;
// 在log中输出坐标
CString str;
str.Format(_T("%d, %d "), x, y);
Log(str);
SetClipboard(str);
//画辅助线
CDC* pDC = GetDC();
//CRect rect;
//GetClientRect(&rect);
// 保存当前DC状态
int nSavedDC = pDC->SaveDC();
DrawSomething(pDC, para.point.x, para.point.y); //画一些别的东西
// 设置ROP模式为R2_NOT,这样可以用相同的操作来清除线条
//pDC->SetROP2(R2_NOT);
CPen redPen;
redPen.CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); // 1像素宽的实线,红色
// 保存旧的画笔
CPen* pOldPen = pDC->SelectObject(&redPen);
// 绘制水平线
pDC->MoveTo(rect.left, para.point.y);
pDC->LineTo(rect.right, para.point.y);
// 绘制垂直线
pDC->MoveTo(para.point.x, rect.top);
pDC->LineTo(para.point.x, rect.bottom);
// 恢复旧的画笔
pDC->SelectObject(pOldPen);
// 恢复DC状态
pDC->RestoreDC(nSavedDC);
ReleaseDC(pDC);
}
BOOL CDlgLableDesign::DrawSomething(CDC* pDC,int x,int y)
{
// 设置字体
CFont font;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
lf.lfHeight = para.fontHeight; // 字体大小
lf.lfWeight = FW_HEAVY; // 加粗
_tcscpy_s(lf.lfFaceName, ("黑体")); // 字体名称
font.CreateFontIndirect(&lf);
//font.CreatePointFont(240, "Arial", NULL);
CFont* pOldFont = pDC->SelectObject(&font);
SetBkMode(pDC->m_hDC, TRANSPARENT);
pDC->TextOut(x, y, _T("DEV1"));
pDC->SelectObject(pOldFont);
return TRUE;
}
BOOL CDlgLableDesign::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
switch (pMsg->wParam)
{
case VK_LEFT:
para.point.x--;
ShowImage();
break;
case VK_RIGHT:
para.point.x++;
ShowImage();
break;
case VK_UP:
para.point.y--;
ShowImage();
break;
case VK_DOWN:
para.point.y++;
ShowImage();
break;
case VK_PRIOR:
case VK_OEM_MINUS:
case VK_SUBTRACT:
para.fontHeight--;
Log("字高:"+ CString(to_string(para.fontHeight).c_str()));
ShowImage();
break;
case VK_NEXT:
case VK_OEM_PLUS:
case VK_ADD:
para.fontHeight++;
Log("字高:" + CString(to_string(para.fontHeight).c_str()));
ShowImage();
break;
default:
break;
}
return TRUE;
}
return CDialogEx::PreTranslateMessage(pMsg);
}
void CDlgLableDesign::OnClose()
{
WritePara();
CDialogEx::OnClose();
}