//包含库函数
#include<windows.h>
#include<windowsx.h>
#include<stdio.h>
#include<string.h>
#include<mmsystem.h>
#include<commdlg.h>
//#include <fstream>
#include"resource.h"
#define WIN32_LEAN_AND_MEAN
//各对话框消息处理函数
LRESULT CALLBACK WindowFunc(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK DialogFunc(HWND ,UINT ,WPARAM ,LPARAM );
BOOL CALLBACK DialogFunc2(HWND ,UINT,WPARAM,LPARAM);
BOOL CALLBACK DialogFuncSet(HWND ,UINT,WPARAM,LPARAM);
BOOL CALLBACK DialogFunccom(HWND ,UINT,WPARAM,LPARAM);
BOOL CALLBACK DialogFuncLpt(HWND ,UINT,WPARAM,LPARAM);
BOOL CALLBACK DialogFuncDrv(HWND ,UINT,WPARAM,LPARAM);
BOOL CALLBACK DialogFuncSuper(HWND ,UINT,WPARAM,LPARAM);
BOOL CALLBACK DialogFuncAbout(HWND ,UINT,WPARAM,LPARAM);
//初始化窗口
HWND InitWindow(HINSTANCE hInstance,int nCmdShow);
//调用驱动程序打印
void OnDrvprint(void);
//打开端口
BOOL OpenLpt(void);
void OpenCom(void);
//测试CPU速度
unsigned __int64 GetCycleCount(void);
unsigned int cpuspeed(void);
//配置端口
void ComConfig(void);
//反馈打印机状态
void PrinterState(void);
//出错处理函数
//全局变量
//统计
BOOL Count=FALSE;
//端口
char portlpt[]="lpt1";
char portcom[]="com1";
//波特率,校验,数据位,停止位
int pt=9600;
char jy[]="n";
int sj=8;
int tz=1;
//串口结构体
DCB dcb;
COMMTIMEOUTS cto;
//驱动程序结构体
PRINTDLG printdlg;
DOCINFO docinfo;
//句柄
HANDLE hCom;
HANDLE hLpt;
//测试CPU速度
unsigned int cpu=0;
char cpuspeed1[20];
//走纸
char run11[]="\x0a";
//票据打印内容中文
char chfont51[]="\x1c\x26广州博施超级市场随着信息技术的发展,内嵌打印功能的产品越来越多,特别是在要求稳定、高可靠性、微型、超小体积、低功耗、价格相对较低打印场合。博施公司针对国内厂家对微型打印机的要求及微打未来发展状况,推出一系列微型打印机模块,以方便国内厂家产品打印功能配套或OEM,ODM。\n\n\n";
char chfont52[]="博施超级市场\n日期6/15/02 单号:010519";
char chfont53[]="机器号:01 班次:1 收款员:1001";
char chfont54[]="----------------------";
char chfont55[]="名称 单价 数量 小计";
char chfont56[]="";
char fonttest[]="正确\n";
//打印内容英文
char enfont51[]="boshiland.com.cn";
//全切纸
char fullcut11[]="\x1b\x40\x1b\x69";
//部分切纸
char partcut11[]="\x1b\x40\x1b\x6d";
//条码
char tm31[]="\x1b\x40\x1d\x48\x2";
char tm32[]="\x1d\x6b\x45\x9";
char tm33[]="123456789";
char tm34[]="\x1d\x68\x32";
char tm35[]="\x1d\x77\x2";
char tm36[]="\x1d\x68\xa2";
char tm37[]="\x1d\x77\x3";
//黑标
char blackmark11[]="\x1b\x40\x1d\x0c";
//字体加倍
char doublefont31[]="\x1b\x40\x1c\x21\xc";
char doublefont32[]="\x1c\x21\xc倍高倍宽文字打印:\n广州博施公司\n特种打印专家\nboshi boshi\x1c\x21\x0\n";
char doublefont33[]="网址:WWW.boshiland.com.cn\n";
//反向走纸
char backrun11[]="\x1b\x40\x1b\x6a\xf0";
//弹钱箱
char plus11[]="\x1b\x40\x1b\x70\x0\x20\x20";
//tm300a进入中文模式
char tm300[]="\x1c\x26";
//软件复位
char reset[]="\x1b\x58";
//char szMsg2[]="按ESC退出!";
BOOL bActive;
int debug=0;
HINSTANCE hIns;
HWND hWnd;
HWND hDlg;
HWND hDlg2;
HWND hDlg3;
HWND hDlgSet;
HWND hDlglpr;
DWORD dwError;
DWORD dwBytesWritten;
int cbstatus1=BST_UNCHECKED,cbstatus2=BST_UNCHECKED;
HACCEL hAccel;
//打开端口com
void OpenCom(void)
{
HRESULT fSuccess;
hCom=CreateFile(portcom,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(hCom==INVALID_HANDLE_VALUE)
{
dwError=GetLastError();
}
//超时设置
GetCommTimeouts(hCom,&cto);
cto.ReadIntervalTimeout=0;
cto.ReadTotalTimeoutConstant=20;
cto.ReadTotalTimeoutMultiplier=20;
cto.WriteTotalTimeoutConstant=0;
cto.WriteTotalTimeoutMultiplier=0;
SetCommTimeouts(hCom,&cto);
FillMemory(&dcb,sizeof(dcb),0);
dcb.DCBlength=sizeof(dcb);
fSuccess=GetCommState(hCom,&dcb);
dcb.BaudRate=pt;//CBR_9600;
dcb.Parity=NOPARITY;
dcb.ByteSize=sj;
dcb.StopBits=(tz-1);//ONESTOPBIT;
dcb.fDtrControl=DTR_CONTROL_ENABLE;
dcb.wReserved=0;
// Get|ComPr
fSuccess=SetCommState(hCom,&dcb);
//BuildCommDCB("9600,n,8,1",&dcb);
}
//打开端口lpt
BOOL OpenLpt(void)
{
// COMMTIMEOUTS lptto;
//hLpt=CreateFile(portlpt,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
hLpt=CreateFile(portlpt, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if(hLpt==INVALID_HANDLE_VALUE)
{
dwError=GetLastError();
return false;
}
//设置超时,如果打印机没有联机或开机则关闭端口并提示
/* lptto.ReadIntervalTimeout = 0;
lptto.ReadTotalTimeoutMultiplier = 0;
lptto.ReadTotalTimeoutConstant = 0;
lptto.WriteTotalTimeoutMultiplier =20;
lptto.WriteTotalTimeoutConstant = 0;
if (!SetCommTimeouts(hLpt,&lptto))
{
MessageBox(NULL,"SetCommTimeout error","Error",MB_OK);
CloseHandle(hLpt);
return false;
}
*/
return true;
}
//主程序
int PASCAL WinMain(HINSTANCE hThisInst,HINSTANCE hPrevInst,
LPSTR lpszArgs,int nWinMode)
{
MSG msg;
HWND hwndprev;
//如果发现程序已打开,则设置程序为当前活动程序
if((hwndprev=FindWindow("tsprinter",NULL))!=NULL)
{SetForegroundWindow(hwndprev);
return -1;
}
if((hWnd=InitWindow(hThisInst,nWinMode))==NULL)
return FALSE;
if(debug)MessageBox(hWnd,"directdraw after","error",MB_OK);
cpu=cpuspeed();
while(1)
{
if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
{
if(!GetMessage(&msg,NULL,0,0))
return msg.wParam;
if(!IsDialogMessage(hDlg,&msg))
{
if(!TranslateAccelerator(hWnd,hAccel,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
else if(bActive)
{
}
else WaitMessage();
}
}
//初始化窗口
HWND InitWindow(HINSTANCE hInstance,int nCmdShow)
{
HWND hwnd;
WNDCLASSEX wcl;
wcl.cbSize=sizeof(WNDCLASSEX);
wcl.style=0;
wcl.lpfnWndProc=(WNDPROC)WindowFunc;
wcl.cbClsExtra=0;
wcl.cbWndExtra=0;
wcl.hInstance=hInstance;
wcl.lpszClassName="tsprinter";
wcl.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
wcl.hIconSm=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON2));
wcl.hCursor=LoadCursor(NULL,IDC_ARROW);
wcl.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);
wcl.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
RegisterClassEx(&wcl);
hwnd=CreateWindowEx(NULL,
"tsprinter",
"特种打印V2.0(02.9.17.代码版:兼容POS/ESC控制代码)",
WS_THICKFRAME|WS_TILED|WS_MINIMIZEBOX|WS_SYSMENU,//WS_OVERLAPPEDWINDOW,
100,
100,
400,
350,
HWND_DESKTOP,
NULL,
hInstance,NULL);
if(!hwnd)return FALSE;
hIns=hInstance;
hAccel=LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR1));
//DialogBox(hIns,MAKEINTRESOURCE(ID_PASSDIA),hWnd,(DLGPROC)DialogFunc2);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
return hwnd;
}
//主窗口消息处理
LRESULT CALLBACK WindowFunc(HWND WhWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HRESULT respone;
switch(message)
{
case WM_ACTIVATEAPP:
bActive=wParam;
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
//MessageBox(WhWnd,"ESC 键按下了!","keyboard",MB_OK);
PostMessage(WhWnd,WM_CLOSE,0,0);
break;
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
//调用各种对话框
case IDC_MYHELP:
ShellExecute(::GetActiveWindow(), "open", "readme.chm", NULL, NULL, SW_SHOWNORMAL);
break;
case ID_OPEN:
MessageBox(WhWnd,"暂时不支持打开文件","打开",MB_OK);
break;
case IDC_COM:
hDlg3=CreateDialog(hIns,MAKEINTRESOURCE(IDD_COM),WhWnd,(DLGPROC)DialogFunccom);
break;
case ID_EXIT:
respone=MessageBox(WhWnd,"是否退出?","EXIT",MB_YESNO);
if(respone==IDYES)PostQuitMessage(0);
break;
case ID_DEMO:
hDlg=CreateDialog(hIns,MAKEINTRESOURCE(IDD_DIALOG1),WhWnd,(DLGPROC)DialogFunc);
break;
case ID_SETCOMLPT:
DialogBox(hIns,MAKEINTRESOURCE(IDD_SET),WhWnd,(DLGPROC)DialogFuncSet);
break;
case IDC_DRV:
DialogBox(hIns,MAKEINTRESOURCE(IDD_DRV),WhWnd,