QQ登录

只需一步,快速开始

314上位机VC MFC实现条形码的批量打印功能

[ 复制链接 ]

314上位机VC MFC实现条形码的批量打印功能

314上位机VC MFC实现条形码的批量打印功能

条形码的批量打印功能
功能展示
日常生活如超市购物,经常看到商品上的条形码,我们当前例程实现条形码的批量打印功能,效果如图,点击列表中数据,对应条形码会显示在左下角,点击打印按钮,可以将列表中显示的全部条形码打印出来;

要点提示
例程中实现条形码的打印功能主要用到两函数GetDIBits()和StretchDIBits();
GetDIBits()函数将来自一幅位图的二进制位复制到一幅与设备无关的位图中,语法为
int GetDIBits( HDC hdc, // handle to device context HBITMAP hbmp, // handle to bitmap UINT uStartScan, // first scan line to set in destination bitmap UINT cScanLines, // number of scan lines to copy LPVOID lpvBits, // address of array for bitmap bits LPBITMAPINFO lpbi, // address of structure with bitmap data UINT uUsage // RGB or palette index );

StretchDIBits()将一幅与设备无关的位图全部或部分数据直接复制到指定的设备场景中 语法为:
int StretchDIBits( HDC hdc, // handle to device context int XDest, // x-coordinate of upper-left corner of dest. rectangle int YDest, // y-coordinate of upper-left corner of dest. rectangle int nDestWidth, // width of destination rectangle int nDestHeight, // height of destination rectangle int XSrc, // x-coordinate of upper-left corner of source rectangle int YSrc, // y-coordinate of upper-left corner of source rectangle int nSrcWidth, // width of source rectangle int nSrcHeight, // height of source rectangle CONST VOID *lpBits, // address of bitmap bits CONST BITMAPINFO *lpBitsInfo, // address of bitmap data UINT iUsage, // usage flags DWORD dwRop // raster operation code );

实现功能
1.新建基于对话框的应用程序
2.插入条形码,如果电脑无此控件可参照根目录< MSBCODE9 手动注册>注册,插入后关联变量CBarCodeCtrl m_Barcode;,插入列表控件关联变量CListCtrl m_List;
3.在STDAFX.H中添加数据库及计算的支持代码#include <math.h>
#import "C:\Program Files\Common Files\System\ado\msado15.dll" rename_namespace ("ADODB") \
rename("EOF","ADOEOF")
using namespace ADODB; 主对话框中添加两变量_ConnectionPtr pCon;_RecordsetPtr pRecord; 记得在APP类中调用函数AfxOleInit();一次;
4.在主对话框OnInitDialog()函数中初始化列表框及数据库
pCon.CreateInstance("ADODB.Connection");
pRecord.CreateInstance(__uuidof(Recordset));
pCon->ConnectionString = "rovider=Microsoft.Jet.OLEDB.4.0;Data Source=price.mdbersist Security Info=False";
try
{
pCon->Open(L"",L"",L"",-1);
}
catch(...)
{
AfxMessageBox("数据连接失败");
return FALSE;
}
m_List.ModifyStyle(0,LVS_REPORT,0);
m_List.SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_TWOCLICKACTIVATE);
m_List.InsertColumn(100,"条形码",LVCFMT_CENTER,100);
m_List.InsertColumn(100,"商品名称",LVCFMT_CENTER,100);
m_List.InsertColumn(100,"价格",LVCFMT_CENTER,80);
m_List.InsertColumn(100,"规格",LVCFMT_CENTER,80);
m_List.InsertColumn(100,"厂家",LVCFMT_CENTER,100);

CString sql= "Select * from MerchandiseInfo";
pRecord = pCon->Execute((_bstr_t)sql,NULL,0);
int row = 0;
CString data;
if (pRecord->GetState()==adStateOpen) //有数据返回
{
while (! pRecord->ADOEOF)
{
m_List.InsertItem(row,"");
for (int col = 0; col < pRecord->GetFields()->Count; col++)
{
data = (char*)(bstr_t)pRecord->GetFields()->GetItem((long)col)->Value;
m_List.SetItemText(row,col,data);
}
row+=1;
pRecord->MoveNext();
}
}
5.添加列表控件的NM_CLICK消息响应函数,用于更新条形码
void CGkbc8Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
*pResult = 0;
POSITION pos;
pos = m_List.GetFirstSelectedItemPosition();
if(NULL == pos)
return;

CString str = m_List.GetItemText(m_List.GetNextSelectedItem(pos),0);
m_Barcode.SetValue((COleVariant)str);
m_Barcode.UpdateWindow();
}
6.添加条形码打印按钮<打印列表框 全部条形码>关联函数
void CGkbc8Dlg::OnButton1()
{
CString str;
//构造打印对话框
CPrintDialog PrintDlg(false,PD_ALLPAGES | PD_USEDEVMODECOPIES |PD_RETURNDEFAULT| PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION,this);
m_Barcode.SetBackColor(RGB(255,255,255));
if (PrintDlg.DoModal()==IDOK)
{
CDC dc1 ;
dc1.Attach(PrintDlg.GetPrinterDC()); //将dc1关联到打印机句柄
dc1.StartDoc("print");

int screenx,screeny;
int printx,printy;
float ratex,ratey;
//确定打印机与屏幕的像素比率
screenx = m_Barcode.GetDC()->GetDeviceCaps(LOGPIXELSX);
screeny = m_Barcode.GetDC()->GetDeviceCaps(LOGPIXELSY);
printx = dc1.GetDeviceCaps(LOGPIXELSX);
printy = dc1.GetDeviceCaps(LOGPIXELSY);
ratex = (float)(printx)/screenx;
ratey = (float)(printy)/screeny;

for (int row = 0; row< m_List.GetItemCount();row++)
{
str = m_List.GetItemText(row,0);
m_Barcode.SetValue((COleVariant)str);
m_Barcode.UpdateWindow();
CDC* pBar = m_Barcode.GetDC();
pBar->SetBkMode(TRANSPARENT);
CDC memdc;
memdc.CreateCompatibleDC(pBar);
CRect rect;
m_Barcode.GetClientRect(rect); //获取条形码的客户区域
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(pBar,rect.Width(),rect.Height());
memdc.SelectObject(&bitmap);
memdc.BitBlt(0,0,rect.Width(),rect.Height(),pBar,0,0,SRCCOPY);


BITMAP bmp ;
bitmap.GetBitmap(&bmp);
int panelsize =0; //调色版大小
if (bmp.bmBitsPixel<16)
panelsize = pow(2,bmp.bmBitsPixel)*sizeof(RGBQUAD);
BITMAPINFO* bInfo = (BITMAPINFO*)LocalAlloc(LPTR,sizeof(BITMAPINFO)+panelsize);
bInfo->bmiHeader.biBitCount = bmp.bmBitsPixel;
bInfo->bmiHeader.biClrImportant = 0;
bInfo->bmiHeader.biClrUsed = 0;
bInfo->bmiHeader.biCompression = 0;
bInfo->bmiHeader.biHeight = bmp.bmHeight;
bInfo->bmiHeader.biPlanes = bmp.bmPlanes;
bInfo->bmiHeader.biSize = sizeof(BITMAPINFO);
bInfo->bmiHeader.biSizeImage = bmp.bmWidthBytes*bmp.bmHeight;
bInfo->bmiHeader.biWidth = bmp.bmWidth;
bInfo->bmiHeader.biXPelsPerMeter = 0;
bInfo->bmiHeader.biYPelsPerMeter = 0;

char* pdata = new char[bmp.bmWidthBytes*bmp.bmHeight];
GetDIBits(memdc.m_hDC,bitmap,0,bmp.bmHeight,pdata,bInfo,DIB_RGB_COLORS);

CDC* pDC = GetDC();
//开始打印
int x = bInfo->bmiHeader.biWidth;
int y = bInfo->bmiHeader.biHeight;
StretchDIBits(dc1.m_hDC,80,row* (int)(200*ratex),int(x*ratex),int(y*ratey),1,1,x,y,pdata,bInfo,DIB_RGB_COLORS, SRCCOPY);
delete pdata;
LocalFree(bInfo );
}
dc1.EndDoc();
}
}
我们来演示下功能实现的整个过程
请点击此处下载

请先注册会员后在进行下载

已注册会员,请先登录后下载

文件名称:314.上位机VC MFC实现条形码的批量打印功能.zip 
文件大小:239.91 KB  售价:11金币
下载权限: 不限 以上或 VIP会员   [购买捐助会员]   [充值积分]   有问题联系我

  

如果您认可,可联系功能定制!

  

如果您着急,充值会员可直接联系发您资料!

  

QQ联系我

微信扫扫联系我

  

  

您的支持是我们创作的动力!  

  

您可花点闲钱积分自助任意充值

  

成为VIP会员 全站资源任意下载永久更新!




回复

使用道具 举报

快速回复 返回列表 客服中心 搜索