home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / vb_code1 / bar_vbx / maindlg.cpp < prev    next >
C/C++ Source or Header  |  1993-11-15  |  5KB  |  188 lines

  1. // maindlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "vctest.h"
  6. #include "maindlg.h"
  7. #include "setprops.h"
  8. #include "commdlg.h"
  9. #include "barcode.h"
  10.  
  11. #ifdef _DEBUG
  12. #undef THIS_FILE
  13. static char BASED_CODE THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. extern "C" LPSTR GetLastErrorString(void);
  17. extern "C" int GetLastErrorCode(void);
  18. extern "C" int PrintBarCode(BARCODEPRINTDATA FAR *);
  19.  
  20. HINSTANCE hVBXInst;
  21. CVBControl *BarCodeVBX;
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CMainDlg dialog
  25.  
  26. CMainDlg::CMainDlg(CWnd* pParent /*=NULL*/)
  27.    : CDialog(CMainDlg::IDD, pParent)
  28. {
  29.    //{{AFX_DATA_INIT(CMainDlg)
  30.    m_BarCode = NULL;
  31.    m_Edit = "";
  32.    //}}AFX_DATA_INIT
  33. }
  34.  
  35. void CMainDlg::DoDataExchange(CDataExchange* pDX)
  36. {
  37.    CDialog::DoDataExchange(pDX);
  38.    //{{AFX_DATA_MAP(CMainDlg)
  39.    DDX_VBControl(pDX, IDC_BARCODE1, m_BarCode);
  40.    DDX_Text(pDX, IDC_EDIT1, m_Edit);
  41.    //}}AFX_DATA_MAP
  42. }
  43.  
  44. BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
  45.    //{{AFX_MSG_MAP(CMainDlg)
  46.    ON_WM_DESTROY()
  47.    ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
  48.    ON_VBXEVENT(VBN_DBLCLICK, IDC_BARCODE1, OnDblclickBarcode1)
  49.    ON_VBXEVENT(VBN_CHANGED, IDC_BARCODE1, OnChangedBarcode1)
  50.    ON_BN_CLICKED(IDB_PRINT, OnClickedPrint)
  51.    //}}AFX_MSG_MAP
  52. END_MESSAGE_MAP()
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CMainDlg message handlers
  56.  
  57.  
  58. void CMainDlg::OnDblclickBarcode1(UINT, int, CWnd*, LPVOID)
  59. {     
  60.    BarCodeVBX = m_BarCode;
  61.    
  62.    CSetProps  dlg;
  63.    dlg.DoModal();
  64. }
  65.  
  66. extern "C" LPSTR GetLastErrorString()
  67. {
  68.    static char Str[81] = "";   
  69.    LPSTR (FAR PASCAL *GetErrorStr) (LPSTR, int) = NULL;
  70.    
  71.    GetErrorStr = (char __far *(__far __pascal *)(LPSTR, int))GetProcAddress(hVBXInst,"BarCodeGetLastErrorStringC");
  72.    
  73.    if(GetLastErrorString != NULL)
  74.       (*GetErrorStr)((LPSTR)Str, 80);
  75.       
  76.    return (LPSTR)Str;
  77. }
  78.       
  79. extern "C" int GetLastErrorCode()
  80. {          
  81.    int retval = 0;
  82.    int (FAR PASCAL *GetErrorCode) (void) = NULL;
  83.    
  84.    GetErrorCode = (int (__far __pascal *)(void))GetProcAddress(hVBXInst,"BarCodeGetLastErrorCode");
  85.    
  86.    if(GetErrorCode != NULL)
  87.       retval = (*GetErrorCode)();
  88.       
  89.    return retval;
  90. }
  91.  
  92. extern "C" int PrintBarCode(BARCODEPRINTDATA FAR *PrintData)
  93. {          
  94.    int retval = 0;
  95.    int (FAR PASCAL *PrintBarCode) (BARCODEPRINTDATA FAR *) = NULL;
  96.    
  97.    PrintBarCode = (int (__far __pascal *)(BARCODEPRINTDATA FAR *))GetProcAddress(hVBXInst,"BarCodePrint");
  98.    
  99.    if(PrintBarCode != NULL)
  100.       retval = (*PrintBarCode)(PrintData);
  101.       
  102.    return retval;
  103. }
  104.  
  105. BOOL CMainDlg::OnInitDialog()
  106. {
  107.    CDialog::OnInitDialog();
  108.    
  109.    CenterWindow();
  110.    
  111.    hVBXInst = LoadLibrary("BARCODE.VBX");
  112.    
  113.    m_Edit = "123456";
  114.    m_BarCode->SetStrProperty("Text",m_Edit);
  115.    
  116.    UpdateData(FALSE);
  117.    
  118.    return TRUE;  // return TRUE  unless you set the focus to a control
  119. }
  120.  
  121. void CMainDlg::OnDestroy()
  122. {
  123.    FreeLibrary(hVBXInst);   
  124.    
  125.    CDialog::OnDestroy();   
  126. }
  127.  
  128. void CMainDlg::OnChangeEdit1()
  129. {
  130.    UpdateData();
  131.    
  132.    m_BarCode->SetStrProperty("Text",m_Edit);
  133.  
  134.    if(GetLastErrorCode())
  135.       AfxMessageBox(GetLastErrorString());
  136. }
  137.  
  138. void CMainDlg::OnChangedBarcode1(UINT, int, CWnd*, LPVOID)
  139. {
  140.    UpdateData();
  141.    
  142.    m_Edit = m_BarCode->GetStrProperty("Text");   
  143.  
  144.    UpdateData(FALSE);
  145. }
  146.  
  147. void CMainDlg::OnClickedPrint()
  148. {              
  149.    int PrintedOK;
  150.    PRINTDLG pd;
  151.    DOCINFO DocInfo;
  152.    BARCODEPRINTDATA PrintData;
  153.    
  154.    memset(&pd,0,sizeof(pd));
  155.           
  156.    pd.lStructSize = sizeof(PRINTDLG);
  157.    pd.Flags = PD_PRINTSETUP | PD_RETURNDC;
  158.    
  159.    if(PrintDlg(&pd) != 0) {
  160.       
  161.       if(pd.hDC != 0) {
  162.          DocInfo.cbSize = 0;
  163.          DocInfo.lpszDocName = 0;
  164.          DocInfo.lpszOutput = 0;
  165.  
  166.          PrintData.BarCodeType = (int)m_BarCode->GetNumProperty("BarCodeType");
  167.          PrintData.Checksum = (int)m_BarCode->GetNumProperty("Checksum");
  168.          lstrcpy((LPSTR)PrintData.Text,(LPSTR)(const char *)m_Edit);
  169.          PrintData.x = 0;
  170.          PrintData.y = 0;
  171.          PrintData.Height = 100;
  172.          PrintData.Rotation = 0;
  173.          PrintData.NarrowBarWidth = (int)m_BarCode->GetNumProperty("NarrowBarWidth") * 2;
  174.          PrintData.Ratio = (int)m_BarCode->GetNumProperty("Ratio");
  175.          PrintData.hDCPrinter = (int)pd.hDC;
  176.  
  177.          StartDoc(pd.hDC, &DocInfo);
  178.          StartPage(pd.hDC);
  179.          PrintedOK = PrintBarCode(&PrintData);
  180.          if(!PrintedOK)
  181.             AfxMessageBox(GetLastErrorString());
  182.          EndPage(pd.hDC);
  183.          EndDoc(pd.hDC);
  184.          DeleteDC(pd.hDC);
  185.          }
  186.       }
  187. }
  188.