home *** CD-ROM | disk | FTP | other *** search
/ CD Direkt 1995 #6 / CDD_6_95.ISO / cdd / winanw / bardemo / bardemo.c next >
C/C++ Source or Header  |  1995-01-18  |  29KB  |  1,029 lines

  1. // bardemo.c
  2.  
  3. #include "windows.h"
  4. #include "commdlg.h"
  5. #include "stdlib.h"
  6. #include "string.h"
  7.  
  8. #include "resource.h"
  9. #include "easybar.h"
  10.  
  11. typedef struct tagLONGSTRING
  12. {
  13.     long    id;
  14.     char    *szName;
  15.  
  16. } LONGSTRING;
  17.  
  18. LONGSTRING g_styles[] =
  19. {
  20.     BDF_LEFT,"Left"
  21.     ,BDF_RIGHT, "Right"
  22.     ,BDF_TOP, "Top"
  23.     ,BDF_BOTTOM, "Bottom"
  24.     ,BDF_CENTER, "Center"
  25.     ,BDF_VCENTER, "VCenter"
  26.     ,BDF_HIDEMAINTEXT, "HideMainText"
  27.     ,BDF_HIDEADDONTEXT, "HideAddOnText"
  28.     ,BDF_ADDONTEXTATTOP, "AddOnTextAtTop"
  29.     ,BDF_ADDONTEXTATBOTTOM, "AddOnTextAtBottom"
  30.     ,BDF_MAINTEXTATTOP, "MainTextAtTop"
  31.     ,BDF_MAINTEXTATBOTTOM, "MainTextAtBottom"
  32.     ,BDF_NOUPCSMALLFONT, "NoUpcSmallFont"
  33.     ,BDF_NOSTRETCHTEXT, "NoStretchText"
  34.     ,BDF_UNIBARHEIGHT, "UniBarHeight"
  35.     ,BDF_RETAINASPECTRATIO, "RetainAspectRatio"
  36.     ,BDF_NOPIXELALIGN, "NoPixelAlign"
  37.     ,BDF_CALCSIZEONLY, "CalcSizeOnly"
  38. };
  39.  
  40. HBARCODE     g_hbar = 0;
  41. RECT        g_rcCanvas;
  42. RECT        g_rc;
  43. int            g_iOrient = 0;
  44. DWORD        g_style = 0;
  45. int            g_iType = 0;
  46. LOGFONT        g_logfont;
  47. char        g_tmp[100];
  48. COLORREF    g_clfFg;
  49. COLORREF    g_clfBg;
  50. int            g_iBkMode;
  51. HGLOBAL        g_hDevMode = 0;
  52. HGLOBAL        g_hDevNames = 0;
  53. HDC            g_hPrintDC = 0;
  54. BOOL        g_bUserAbort = FALSE;
  55. HWND        g_hPrintDlg = 0;
  56. BOOL        g_bInvalidData1 = FALSE;
  57. BOOL        g_bInvalidData2 = FALSE;
  58.  
  59. #define HT_INSIDE        1
  60. #define HT_LEFT            2
  61. #define HT_RIGHT        3
  62. #define HT_TOP            4
  63. #define HT_BOTTOM        5
  64. #define HT_LEFTTOP        6
  65. #define HT_RIGHTTOP        7
  66. #define HT_RIGHTBOTTOM    8
  67. #define HT_LEFTBOTTOM    9
  68.  
  69. long FAR PASCAL _export WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  70. BOOL FAR PASCAL _export AboutDlgProc(HWND hwnd, UINT msg, 
  71.                             WPARAM wParam, LPARAM lParam);
  72. BOOL FAR PASCAL _export AdvancedDlgProc(HWND hwnd, UINT msg, 
  73.                             WPARAM wParam, LPARAM lParam);
  74. BOOL FAR PASCAL _export AbortProc(HDC hdcPrint, short sCode);
  75. BOOL FAR PASCAL _export PrintDlgProc(HWND hDlg, UINT msg, 
  76.                             WPARAM wParam, LPARAM lParam);
  77. static void drawBarcode(HWND hwnd, HDC hdc, HDC hicTarget, 
  78.                 HBARCODE hBar, LPRECT lprc);
  79. static void printBarcode(HWND hwnd, HDC hdcPrint, 
  80.                 HBARCODE hBar, LPRECT lprc);
  81. static long cmdProc(HWND hwnd, WPARAM wParam, LPARAM lParam);
  82. static void XORDottedFrame (HDC hdc, LPRECT prc);
  83. static DWORD getStyle(HWND hwnd);
  84. static int hitTest(int x, int y, LPRECT lprc);
  85.  
  86. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, 
  87.                 LPSTR lpszCmdLine, int nCmdShow)
  88. {
  89.     static char szAppName[] = "Easybar";
  90.     HWND    hwnd;            
  91.     MSG        msg;                                    
  92.     
  93.     if (!hPrevInstance)
  94.     {
  95.         WNDCLASS    cs;
  96.         
  97.         cs.style = CS_HREDRAW | CS_VREDRAW;
  98.         cs.lpfnWndProc = WndProc;
  99.         cs.cbClsExtra = 0;
  100.         cs.cbWndExtra = DLGWINDOWEXTRA;
  101.         cs.hInstance = hInstance;
  102.         cs.hIcon = LoadIcon(hInstance, "BARDEMO");
  103.         cs.hCursor = NULL; //LoadCursor(hInstance, IDC_ARROW);
  104.         cs.hbrBackground = COLOR_WINDOW + 1;
  105.         cs.lpszMenuName = NULL;
  106.         cs.lpszClassName = szAppName;
  107.  
  108.         RegisterClass(&cs);
  109.     }
  110.  
  111.     hwnd = CreateDialog(hInstance, szAppName, 0, NULL);
  112.     
  113.     ShowWindow(hwnd, nCmdShow);
  114.     UpdateWindow(hwnd);
  115.     
  116.     while(GetMessage(&msg, NULL, 0, 0))
  117.     {
  118.         if (!IsDialogMessage(hwnd, &msg))
  119.         {
  120.             TranslateMessage(&msg);
  121.             DispatchMessage(&msg);
  122.         }
  123.     }
  124.  
  125.     return msg.wParam;
  126. }
  127.  
  128. long FAR PASCAL _export WndProc(HWND hwnd, UINT msg, 
  129.                             WPARAM wParam, LPARAM lParam)
  130. {                        
  131.     HWND        hctl;                  
  132.     int            iType;          
  133.     int            i, ind;
  134.     LPSTR        lp;                             
  135.     PAINTSTRUCT    ps;
  136.     HDC            hdc;
  137.     HFONT        hFont, hFont0;
  138.     HBRUSH        hBrush;
  139.     static PRINTDLG        pd;
  140.     static        bInitialized = FALSE;
  141.     static        nDrag = 0;  
  142.     static POINT    pt;
  143.     static RECT        rcPrev;     
  144.     static TEXTMETRIC    tm;
  145.     
  146.     switch(msg) {                                   
  147.     case WM_CREATE:
  148.         memset(&pd, 0, sizeof(pd));
  149.         pd.lStructSize = sizeof(pd);
  150.         pd.hwndOwner = hwnd;
  151.         pd.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
  152.         if (PrintDlg(&pd)) 
  153.         {
  154.             g_hPrintDC = pd.hDC;
  155.             g_hDevMode = pd.hDevMode;
  156.             g_hDevNames = pd.hDevNames;
  157.         }
  158.         return 0;
  159.     case WM_MOVE:
  160.         if (bInitialized) break;
  161.         bInitialized = TRUE;
  162.         g_hbar = 0;
  163.         for (i = 0; (i = EnumBarcodeTypes(i, &iType, &lp)) != 0; )
  164.         {
  165.             ind = (int)SendDlgItemMessage(hwnd, IDC_BARCODE, CB_ADDSTRING, 
  166.                                         0, (LONG)lp);
  167.             SendDlgItemMessage(hwnd, IDC_BARCODE, CB_SETITEMDATA, 
  168.                                         ind, (LONG)iType);
  169.         }
  170.         SendDlgItemMessage(hwnd, IDC_BARCODE, CB_SETCURSEL, 0, 0);
  171.            iType = (int) SendDlgItemMessage(hwnd, IDC_BARCODE, 
  172.                         CB_GETITEMDATA, (WPARAM)0, (LPARAM)0);
  173.         g_hbar = BarCreate(iType, 0);
  174.         g_iType = iType;
  175.  
  176.         BarGetData(g_hbar, g_tmp, sizeof(g_tmp), NULL, 0);
  177.         SendDlgItemMessage(hwnd, IDC_DATA, WM_SETTEXT, 0, 
  178.                 (LONG)(LPSTR)g_tmp);
  179.  
  180.         BarGetAddOnData(g_hbar, g_tmp, sizeof(g_tmp), NULL, 0);
  181.         if (!g_tmp[0])
  182.         {
  183.             EnableWindow(GetDlgItem(hwnd, IDC_ADDONDATA), FALSE);
  184.             EnableWindow(GetDlgItem(hwnd, IDC_ADDONMESG), FALSE);
  185.         }
  186.         else
  187.         {
  188.             SendDlgItemMessage(hwnd, IDC_ADDONDATA, WM_SETTEXT, 0, 
  189.                 (LONG)(LPSTR)g_tmp);
  190.         }
  191.         
  192.         SendDlgItemMessage(hwnd, IDC_ORIENT, CB_ADDSTRING, 0, (LONG)(LPSTR)"0");
  193.         SendDlgItemMessage(hwnd, IDC_ORIENT, CB_ADDSTRING, 0, (LONG)(LPSTR)"90");
  194.         SendDlgItemMessage(hwnd, IDC_ORIENT, CB_ADDSTRING, 0, (LONG)(LPSTR)"180");
  195.         SendDlgItemMessage(hwnd, IDC_ORIENT, CB_ADDSTRING, 0, (LONG)(LPSTR)"270");
  196.         SendDlgItemMessage(hwnd, IDC_ORIENT, CB_SETCURSEL, 0, 0);
  197.         g_iOrient = 0;
  198.  
  199.         for (i = 0; i < sizeof(g_styles) / sizeof(g_styles[0]); i++)
  200.         {
  201.             ind = (int) SendDlgItemMessage(hwnd, IDC_STYLE, LB_ADDSTRING, 
  202.                     (WPARAM)0, (LONG)(LPSTR)(g_styles[i].szName));
  203.             SendDlgItemMessage(hwnd, IDC_STYLE, LB_SETITEMDATA,
  204.                     (WPARAM)ind, (LPARAM)g_styles[i].id);
  205.         }
  206.  
  207.         // init font
  208.         hdc = GetDC(hwnd);   
  209.         hFont0 = SelectObject(hdc, GetStockObject(DEVICE_DEFAULT_FONT));
  210.         GetObject(hFont0, sizeof(g_logfont), (LPSTR)&g_logfont);
  211.         strcpy(g_logfont.lfFaceName, "Arial");
  212.         g_logfont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
  213.         g_logfont.lfClipPrecision = CLIP_TT_ALWAYS;
  214.         g_logfont.lfWidth = 0;
  215.         hFont = CreateFontIndirect(&g_logfont);
  216.         SelectObject(hdc, hFont);
  217.         GetTextFace(hdc, LF_FACESIZE, g_logfont.lfFaceName);
  218.         GetTextMetrics(hdc, &tm);
  219.         if (tm.tmHeight < 0)
  220.             i = tm.tmHeight;
  221.         else
  222.             i = -(tm.tmHeight - tm.tmInternalLeading);
  223.         g_logfont.lfHeight = i;
  224.         wsprintf(g_tmp, "%d", MulDiv(-i, 72, GetDeviceCaps(hdc, LOGPIXELSY)));
  225.         SetWindowText(GetDlgItem(hwnd, IDC_POINT), g_tmp);
  226.         SetWindowText(GetDlgItem(hwnd, IDC_FONTNAME), g_logfont.lfFaceName);
  227.         SelectObject(hdc, hFont0);
  228.         DeleteObject(hFont);
  229.  
  230.         // init color
  231.         g_clfFg = GetTextColor(hdc);
  232.         g_clfBg = GetBkColor(hdc);
  233.         g_iBkMode = GetBkMode(hdc);
  234.         if (g_iBkMode == TRANSPARENT) 
  235.             SendDlgItemMessage(hwnd, IDC_TRANSPARENT, BM_SETCHECK, 1, 0);
  236.         else
  237.             SendDlgItemMessage(hwnd, IDC_TRANSPARENT, BM_SETCHECK, 0, 0);
  238.         
  239.         ReleaseDC(hwnd, hdc);
  240.  
  241.         hctl = GetDlgItem(hwnd, IDC_CANVAS);
  242.         GetWindowRect(hctl, &g_rcCanvas);
  243.         g_rcCanvas.right += 5;
  244.         g_rcCanvas.bottom += 5;
  245.         ScreenToClient(hwnd, (LPPOINT)&g_rcCanvas);
  246.         ScreenToClient(hwnd, ((LPPOINT)&g_rcCanvas) + 1);
  247.         MoveWindow(hctl, g_rcCanvas.left, g_rcCanvas.top,
  248.                 g_rcCanvas.right - g_rcCanvas.left,
  249.                 g_rcCanvas.bottom - g_rcCanvas.top, FALSE);
  250.         InflateRect(&g_rcCanvas, -1, -1);
  251.         CopyRect(&g_rc, &g_rcCanvas);
  252.         InflateRect(&g_rc, -(g_rcCanvas.right - g_rcCanvas.left) / 8,
  253.                     -(g_rcCanvas.bottom - g_rcCanvas.top) / 4);
  254.         InvalidateRect(hwnd, &g_rcCanvas, FALSE);
  255.         SetFocus(GetDlgItem(hwnd, IDC_BARCODE));
  256.         return 0;
  257.     case WM_PAINT:
  258.         hdc = BeginPaint(hwnd, &ps);          
  259.         IntersectClipRect(hdc, g_rcCanvas.left, g_rcCanvas.top,
  260.                     g_rcCanvas.right, g_rcCanvas.bottom);
  261.         // draw bg
  262.         hBrush = SelectObject(hdc, 
  263.                     CreateSolidBrush(RGB(255, 255, 232)));
  264.         PatBlt(hdc, g_rcCanvas.left, g_rcCanvas.top, 
  265.                 g_rcCanvas.right - g_rcCanvas.left,
  266.                 g_rcCanvas.bottom - g_rcCanvas.top, PATCOPY);
  267.         DeleteObject(SelectObject(hdc, hBrush));
  268.  
  269.         if (g_bInvalidData1 || g_bInvalidData2)
  270.         {
  271.             DrawText(hdc, "Invalid Data!", -1, &g_rcCanvas,
  272.                     DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  273.         }
  274.         else
  275.