home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DP Tool Club 26
/
CD_ASCQ_26_1295.iso
/
vrac
/
ezbar32.zip
/
BARDEMO.C
next >
Wrap
C/C++ Source or Header
|
1995-09-24
|
29KB
|
1,037 lines
// bardemo.c v2.0 32-bit for Windows 95
// Copyright (c) 1995 Bokai Corporation
#include "windows.h"
#include "commdlg.h"
#include "stdlib.h"
#include "string.h"
#include "resource.h"
#include "easybar.h"
typedef struct tagLONGSTRING
{
long id;
char *szName;
} LONGSTRING;
LONGSTRING g_styles[] =
{
BDF_LEFT,"Left"
,BDF_RIGHT, "Right"
,BDF_TOP, "Top"
,BDF_BOTTOM, "Bottom"
,BDF_CENTER, "Center"
,BDF_VCENTER, "VCenter"
,BDF_HIDEMAINTEXT, "HideMainText"
,BDF_HIDEADDONTEXT, "HideAddOnText"
,BDF_ADDONTEXTATTOP, "AddOnTextAtTop"
,BDF_ADDONTEXTATBOTTOM, "AddOnTextAtBottom"
,BDF_MAINTEXTATTOP, "MainTextAtTop"
,BDF_MAINTEXTATBOTTOM, "MainTextAtBottom"
,BDF_NOUPCSMALLFONT, "NoUpcSmallFont"
,BDF_NOSTRETCHTEXT, "NoStretchText"
,BDF_UNIBARHEIGHT, "UniBarHeight"
,BDF_RETAINASPECTRATIO, "RetainAspectRatio"
,BDF_NOPIXELALIGN, "NoPixelAlign"
,BDF_CALCSIZEONLY, "CalcSizeOnly"
,BDF_SHOWCODE39STARTSTOP, "ShowCode39StartStop"
,BDF_WYSIWYGEXCLCOLOR, "WysiwygExclColor"
};
HBARCODE g_hbar = 0;
RECT g_rcCanvas;
RECT g_rc;
int g_iOrient = 0;
DWORD g_style = 0;
int g_iType = 0;
LOGFONT g_logfont;
char g_tmp[100];
COLORREF g_clfFg;
COLORREF g_clfBg;
int g_iBkMode;
HGLOBAL g_hDevMode = 0;
HGLOBAL g_hDevNames = 0;
HDC g_hPrintDC = 0;
BOOL g_bUserAbort = FALSE;
HWND g_hPrintDlg = 0;
BOOL g_bInvalidData1 = FALSE;
BOOL g_bInvalidData2 = FALSE;
unsigned int g_uStyle = 0;
#define HT_INSIDE 1
#define HT_LEFT 2
#define HT_RIGHT 3
#define HT_TOP 4
#define HT_BOTTOM 5
#define HT_LEFTTOP 6
#define HT_RIGHTTOP 7
#define HT_RIGHTBOTTOM 8
#define HT_LEFTBOTTOM 9
LRESULT APIENTRY WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL APIENTRY AboutDlgProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam);
BOOL APIENTRY AdvancedDlgProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam);
BOOL APIENTRY AbortProc(HDC hdcPrint, short sCode);
BOOL APIENTRY PrintDlgProc(HWND hDlg, UINT msg,
WPARAM wParam, LPARAM lParam);
static void drawBarcode(HWND hwnd, HDC hdc, HDC hicTarget,
HBARCODE hBar, LPRECT lprc);
static void printBarcode(HWND hwnd, HDC hdcPrint,
HBARCODE hBar, LPRECT lprc);
static long cmdProc(HWND hwnd, WPARAM wParam, LPARAM lParam);
static void XORDottedFrame (HDC hdc, LPRECT prc);
static DWORD getStyle(HWND hwnd);
static int hitTest(int x, int y, LPRECT lprc);
int APIENTRY WinMain(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
static char szAppName[] = "Easybar";
HWND hwnd;
MSG msg;
if (!hPrevInstance)
{
WNDCLASS cs;
cs.style = CS_HREDRAW | CS_VREDRAW;
cs.lpfnWndProc = WndProc;
cs.cbClsExtra = 0;
cs.cbWndExtra = DLGWINDOWEXTRA;
cs.hInstance = hInstance;
cs.hIcon = LoadIcon(hInstance, "BARDEMO");
cs.hCursor = NULL; //LoadCursor(hInstance, IDC_ARROW);
cs.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
cs.lpszMenuName = NULL;
cs.lpszClassName = szAppName;
RegisterClass(&cs);
}
hwnd = CreateDialog(hInstance, szAppName, 0, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
if (!IsDialogMessage(hwnd, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
LRESULT APIENTRY WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
HWND hctl;
int iType;
int i, ind;
LPSTR lp;
PAINTSTRUCT ps;
HDC hdc;
HFONT hFont, hFont0;
HBRUSH hBrush;
static PRINTDLG pd;
static bInitialized = FALSE;
static nDrag = 0;
static POINT pt;
static RECT rcPrev;
static TEXTMETRIC tm;
switch(msg) {
case WM_CREATE:
memset(&pd, 0, sizeof(pd));
pd.lStructSize = sizeof(pd);
pd.hwndOwner = hwnd;
pd.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
if (PrintDlg(&pd))
{
g_hPrintDC = pd.hDC;
g_hDevMode = pd.hDevMode;
g_hDevNames = pd.hDevNames;
}
return 0;
case WM_MOVE:
if (bInitialized) break;
bInitialized = TRUE;
g_hbar = 0;
for (i = 0; (i = EnumBarcodeTypes(i, &iType, &lp)) != 0; )
{
ind = (int)SendDlgItemMessage(hwnd, IDC_BARCODE, CB_ADDSTRING,
0, (LONG)lp);
SendDlgItemMessage(hwnd, IDC_BARCODE, CB_SETITEMDATA,
ind, (LONG)iType);
}
SendDlgItemMessage(hwnd, IDC_BARCODE, CB_SETCURSEL, 0, 0);
iType = (int) SendDlgItemMessage(hwnd, IDC_BARCODE,
CB_GETITEMDATA, (WPARAM)0, (LPARAM)0);
g_hbar = BarCreate(iType, g_uStyle);
g_iType = iType;
BarGetData(g_hbar, g_tmp, sizeof(g_tmp), NULL, 0);
SendDlgItemMessage(hwnd, IDC_DATA, WM_SETTEXT, 0,
(LONG)(LPSTR)g_tmp);
BarGetAddOnData(g_hbar, g_tmp, sizeof(g_tmp), NULL, 0);
if (!g_tmp[0])
{
EnableWindow(GetDlgItem(hwnd, IDC_ADDONDATA), FALSE);
EnableWindow(GetDlgItem(hwnd, IDC_ADDONMESG), FALSE);
}
else
{
SendDlgItemMessage(hwnd, IDC_ADDONDATA, WM_SETTEXT, 0,
(LONG)(LPSTR)g_tmp);
}
SendDlgItemMessage(hwnd, IDC_ORIENT, CB_ADDSTRING, 0, (LONG)(LPSTR)"0");
SendDlgItemMessage(hwnd, IDC_ORIENT, CB_ADDSTRING, 0, (LONG)(LPSTR)"90");
SendDlgItemMessage(hwnd, IDC_ORIENT, CB_ADDSTRING, 0, (LONG)(LPSTR)"180");
SendDlgItemMessage(hwnd, IDC_ORIENT, CB_ADDSTRING, 0, (LONG)(LPSTR)"270");
SendDlgItemMessage(hwnd, IDC_ORIENT, CB_SETCURSEL, 0, 0);
g_iOrient = 0;
for (i = 0; i < sizeof(g_styles) / sizeof(g_styles[0]); i++)
{
ind = (int) SendDlgItemMessage(hwnd, IDC_STYLE, LB_ADDSTRING,
(WPARAM)0, (LONG)(LPSTR)(g_styles[i].szName));
SendDlgItemMessage(hwnd, IDC_STYLE, LB_SETITEMDATA,
(WPARAM)ind, (LPARAM)g_styles[i].id);
}
// init font
hdc = GetDC(hwnd);
hFont0 = SelectObject(hdc, GetStockObject(DEVICE_DEFAULT_FONT));
GetObject(hFont0, sizeof(g_logfont), (LPSTR)&g_logfont);
strcpy(g_logfont.lfFaceName, "Arial");
g_logfont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
g_logfont.lfClipPrecision = CLIP_TT_ALWAYS;
g_logfont.lfWidth = 0;
hFont = CreateFontIndirect(&g_logfont);
SelectObject(hdc, hFont);
GetTextFace(hdc, LF_FACESIZE, g_logfont.lfFaceName);
GetTextMetrics(hdc, &tm);
if (tm.tmHeight < 0)
i = tm.tmHeight;
else
i = -(tm.tmHeight - tm.tmInternalLeading);
g_logfont.lfHeight = i;
wsprintf(g_tmp, "%d", MulDiv(-i, 72, GetDeviceCaps(hdc, LOGPIXELSY)));
SetWindowText(GetDlgItem(hwnd, IDC_POINT), g_tmp);
SetWindowText(GetDlgItem(hwnd, IDC_FONTNAME), g_logfont.lfFaceName);
SelectObject(hdc, hFont0);
DeleteObject(hFont);
// init color
g_clfFg = GetTextColor(hdc);
g_clfBg = GetBkColor(hdc);
g_iBkMode = GetBkMode(hdc);
if (g_iBkMode == TRANSPARENT)
SendDlgItemMessage(hwnd, IDC_TRANSPARENT, BM_SETCHECK, 1, 0);
else
SendDlgItemMessage(hwnd, IDC_TRANSPARENT, BM_SETCHECK, 0, 0);
ReleaseDC(hwnd, hdc);
hctl = GetDlgItem(hwnd, IDC_CANVAS);
GetWindowRect(hctl, &g_rcCanvas);
g_rcCanvas.right += 5;
g_rcCanvas.bottom += 5;
ScreenToClient(hwnd, (LPPOINT)&g_rcCanvas);
ScreenToClient(hwnd, ((LPPOINT)&g_rcCanvas) + 1);
MoveWindow(hctl, g_rcCanvas.left, g_rcCanvas.top,
g_rcCanvas.right - g_rcCanvas.left,
g_rcCanvas.bottom - g_rcCanvas.top, FALSE);
InflateRect(&g_rcCanvas, -1, -1);
CopyRect(&g_rc, &g_rcCanvas);
InflateRect(&g_rc, -(g_rcCanvas.right - g_rcCanvas.left) / 8,
-(g_rcCanvas.bottom - g_rcCanvas.top) / 4);
InvalidateRect(hwnd, &g_rcCanvas, FALSE);
SetFocus(GetDlgItem(hwnd, IDC_BARCODE));
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
IntersectClipRect(hdc, g_rcCanvas.left, g_rcCanvas.top,
g_rcCanvas.right, g_rcCanvas.bottom);
// draw bg
hBrush = SelectObject(hdc,
CreateSolidBrush(RGB(255, 255, 232)));
PatBlt(hdc, g_rcCanvas.left, g_rcCanvas.top,
g_rcCanvas.right - g_rcCanvas.left,
g_rcCanvas.bottom - g_rcCanvas.top, PATCOPY);
DeleteObject(SelectObject(hdc, hBrush));
if (g_bInvalidData1 || g_bInvalidData2)
{
DrawText(hdc, "Invalid Data!", -1, &g_rcCanvas,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
else
{
if (SendDlgItemMessage(hwnd, IDC_WYSIWYG,
BM_GETCHECK, 0, 0) == 0)
drawBarcode(hwnd, hdc, 0, g_hbar, &g_rc);
else
drawBarcode(hwnd, hdc, g_hPrintDC, g_hbar, &g_rc);
XORDottedFrame (hdc, &g_rc);
}
EndPaint(hwnd, &ps);
return 0;
case WM_MOUSEMOVE:
if (!nDrag)
{
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
if (!PtInRect(&g_rcCanvas, pt))
{
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
break;
}
i = hitTest(LOWORD(lParam), HIWORD(lParam), &g_rc);
switch (i) {
case HT_LEFT:
case HT_RIGHT:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
break;
case HT_TOP:
case HT_BOTTOM:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
break;
case HT_LEFTTOP:
case HT_RIGHTBOTTOM:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
break;
case HT_LEFTBOTTOM:
case HT_RIGHTTOP:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
break;
case HT_INSIDE:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZE)));
break;
default:
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
}
}
else
{
hdc = GetDC(hwnd);
XORDottedFrame(hdc, &rcPrev);
switch (nDrag) {
case HT_LEFT:
rcPrev.left = g_rc.left + LOWORD(lParam) - pt.x;
break;
case HT_RIGHT:
rcPrev.right = g_rc.right + LOWORD(lParam) - pt.x;
break;
case HT_TOP:
rcPrev.top = g_rc.top + HIWORD(lParam) - pt.y;
break;
case HT_BOTTOM:
rcPrev.bottom = g_rc.bottom + HIWORD(lParam) - pt.y;
break;
case HT_LEFTTOP:
rcPrev.left = g_rc.left + LOWORD(lParam) - pt.x;
rcPrev.top = g_rc.top + HIWORD(lParam) - pt.y;
break;
case HT_RIGHTBOTTOM:
rcPrev.right = g_rc.right + LOWORD(lParam) - pt.x;
rcPrev.bottom = g_rc.bottom + HIWORD(lParam) - pt.y;
break;
case HT_LEFTBOTTOM:
rcPrev.left = g_rc.left + LOWORD(lParam) - pt.x;
rcPrev.bottom = g_rc.bottom + HIWORD(lParam) - pt.y;
break;
case HT_RIGHTTOP:
rcPrev.right = g_rc.right + LOWORD(lParam) - pt.x;
rcPrev.top = g_rc.top + HIWORD(lParam) - pt.y;
break;
case HT_INSIDE:
CopyRect(&rcPrev, &g_rc);
OffsetRect(&rcPrev, LOWORD(lParam) - pt.x,
HIWORD(lParam) - pt.y);
break;
default:
;
}
XORDottedFrame(hdc, &rcPrev);
ReleaseDC(hwnd, hdc);
}
return 0;
case WM_LBUTTONDOWN:
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
if (!PtInRect(&g_rcCanvas, pt)) break;
nDrag = hitTest(pt.x, pt.y, &g_rc);
if (nDrag)
{
SetCapture(hwnd);
CopyRect(&rcPrev, &g_rcCanvas);
ClientToScreen(hwnd, (LPPOINT)&rcPrev);
ClientToScreen(hwnd, ((LPPOINT)&rcPrev) + 1);
ClipCursor(&rcPrev);
CopyRect(&rcPrev, &g_rc);
hdc = GetDC(hwnd);
XORDottedFrame(hdc, &rcPrev);
ReleaseDC(hwnd, hdc);
}
return 0;
case WM_LBUTTONUP:
if (GetCapture() == hwnd)
{
if (rcPrev.left > rcPrev.right)
{
i = rcPrev.left; rcPrev.left = rcPrev.right; rcPrev.right = i;
}
if (rcPrev.top > rcPrev.bottom)
{
i = rcPrev.top; rcPrev.top = rcPrev.bottom; rcPrev.bottom = i;
}
nDrag = 0;
ClipCursor(NULL);
ReleaseCapture();
hdc = GetDC(hwnd);
XORDottedFrame(hdc, &rcPrev);
ReleaseDC(hwnd, hdc);
if (!EqualRect(&rcPrev, &g_rc))
{
CopyRect(&g_rc, &rcPrev);
InvalidateRect(hwnd, &g_rcCanvas, FALSE);
}
return 0;
}
break;
case WM_SETFOCUS:
SetFocus(GetDlgItem(hwnd, IDC_BARCODE));
return 0;
case WM_COMMAND:
return cmdProc(hwnd, wParam, lParam);
case WM_CLOSE:
break;
case WM_DESTROY:
if (g_hbar) BarDestroy(g_hbar);
if (g_hPrintDC) DeleteDC(g_hPrintDC);
if (g_hDevMode) GlobalFree(g_hDevMode);
if (g_hDevNames) GlobalFree(g_hDevNames);
PostQuitMessage(0);
return 0;
/*
case WM_CTLCOLORSTATIC:
return (LRESULT) (HBRUSH) (COLOR_BTNFACE + 1);
*/
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
static int hitTest(int x, int y, LPRECT lprc)
{
int d = 2;
int n;
if (x < lprc->left - d)
return 0;
else if (x <= lprc->left + d)
n = HT_LEFT;
else if (x < lprc->right - d)
n = HT_INSIDE;
else if (x <= lprc->right + d)
n = HT_RIGHT;
else
return 0;
if (y < lprc->top - d)
return 0;
else if (y <= lprc->top + d)
switch (n) {
case HT_LEFT:
n = HT_LEFTTOP; break;
case HT_RIGHT:
n = HT_RIGHTTOP; break;
default:
n = HT_TOP;
}
else if (y < lprc->bottom - d)
;
else if (y <= lprc->bottom + d)
switch (n) {
case HT_LEFT:
n = HT_LEFTBOTTOM; break;
case HT_RIGHT:
n = HT_RIGHTBOTTOM; break;
default:
n = HT_BOTTOM;
}
else
return 0;
return n;
}
static void XORDottedFrame (HDC hdc, LPRECT prc)
{
HPEN hp;
int r2;
int m;
r2 = SetROP2(hdc, R2_NOTXORPEN);
hp = SelectObject(hdc, CreatePen (PS_DOT, 0, RGB (0, 0, 0)));
m = SetBkMode(hdc, TRANSPARENT);
MoveToEx(hdc, prc->left - 1, prc->top - 1, NULL);
LineTo(hdc, prc->right, prc->top - 1);
LineTo(hdc, prc->right, prc->bottom);
LineTo(hdc, prc->left - 1, prc->bottom);
LineTo(hdc, prc->left - 1, prc->top - 1);
DeleteObject(SelectObject(hdc, hp));
SetROP2(hdc, r2);
SetBkMode(hdc, m);
}
static long cmdProc(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
int iType;
static char buf[64];
static char buf2[64];
DWORD dwIndex;
FARPROC lpfnDlgProc;
HINSTANCE hInst;
static CHOOSEFONT cf;
static CHOOSECOLOR cc;
static PRINTDLG pd;
static DWORD dwCustColors[16];
HWND hCtl;
RECT rc;
switch (LOWORD(wParam)) {
case IDC_ABOUT:
hInst = (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE);
lpfnDlgProc = MakeProcInstance((FARPROC)AboutDlgProc, hInst);
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUT), hwnd, lpfnDlgProc);
FreeProcInstance(lpfnDlgProc);
return 0;
case IDC_BTNFONT:
memset(&cf, 0, sizeof(cf));
cf.lStructSize = sizeof(CHOOSEFONT);
cf.hwndOwner = hwnd;
cf.lpLogFont = &g_logfont;
cf.iPointSize = 0;
cf.rgbColors = g_clfFg;
cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_EFFECTS;
if (g_hPrintDC)
{
if (!g_hPrintDC) MessageBeep(0);
cf.hDC = g_hPrintDC;
cf.Flags |= CF_PRINTERFONTS;
}
if (ChooseFont(&cf))
{
SetWindowText(GetDlgItem(hwnd, IDC_FONTNAME),
g_logfont.lfFaceName);
wsprintf(buf, "%d", cf.iPointSize / 10);
if (cf.iPointSize % 10 > 0)
wsprintf(buf + strlen(buf), ".%d", cf.iPointSize % 10);
SetWindowText(GetDlgItem(hwnd, IDC_POINT), buf);
g_clfFg = cf.rgbColors;
InvalidateRect(hwnd, &g_rcCanvas, FALSE);
}
return 0;
case IDC_BG:
memset(&cc, 0, sizeof(cc));
cc.lStructSize = sizeof(CHOOSECOLOR);
cc.hwndOwner = hwnd;
cc.rgbResult = g_clfBg;
cc.Flags = CC_PREVENTFULLOPEN | CC_RGBINIT;
cc.lpCustColors = dwCustColors;
if (ChooseColor(&cc))
{
g_clfBg = cc.rgbResult;
SendDlgItemMessage(hwnd, IDC_TRANSPARENT, BM_SETCHECK, 0, 0);
g_iBkMode = OPAQUE;
InvalidateRect(hwnd, &g_rcCanvas, FALSE);
}
return 0;
case IDC_PRINT:
memset(&pd, 0, sizeof(pd));
pd.lStructSize = sizeof(pd);
pd.hwndOwner = hwnd;
pd.nFromPage = pd.nToPage = 1;
pd.nMinPage = pd.nMaxPage = 1;
pd.Flags = PD_NOSELECTION | PD_ALLPAGES
| PD_RETURNDC | PD_USEDEVMODECOPIES;
pd.hDevMode = g_hDevMode;
pd.hDevNames = g_hDevNames;
if (PrintDlg(&pd))
{
if (g_hPrintDC) DeleteDC(g_hPrintDC);
g_hPrintDC = pd.hDC;
g_hDevMode = pd.hDevMode;
g_hDevNames = pd.hDevNames;
CopyRect(&rc, &g_rc);
OffsetRect(&rc, -g_rcCanvas.left, -g_rcCanvas.top);
printBarcode(hwnd, g_hPrintDC, g_hbar, &rc);
// TODO: if WYSIWYG, refresh
}
else
{
g_hDevMode = pd.hDevMode;
g_hDevNames = pd.hDevNames;
}
// printer config may have changed
InvalidateRect(hwnd, &g_rcCanvas, FALSE);
return 0;
case IDC_TRANSPARENT:
if (HIWORD(wParam) == BN_CLICKED)
{
if (SendDlgItemMessage(hwnd, IDC_TRANSPARENT,
BM_GETCHECK, 0, 0) == 0)
g_iBkMode = OPAQUE;
else
g_iBkMode = TRANSPARENT;
InvalidateRect(hwnd, &g_rcCanvas, FALSE);
return 0;
}
break;
case IDC_WYSIWYG:
if (HIWORD(wParam) == BN_CLICKED)
{
InvalidateRect(hwnd, &g_rcCanvas, FALSE);
return 0;
}
break;
case IDC_BARCODE:
if (HIWORD(wParam) == CBN_SELCHANGE)
{
dwIndex = SendDlgItemMessage(hwnd, IDC_BARCODE, CB_GETCURSEL, 0, 0);
iType = (int) SendDlgItemMessage(hwnd, IDC_BARCODE,
CB_GETITEMDATA, (WPARAM)dwIndex, (LPARAM)0);
if (g_hbar && g_iType == iType) return 0;
if (g_hbar) BarDestroy(g_hbar);
g_iType = iType;
g_hbar = BarCreate(iType, g_uStyle);
SetWindowText(GetDlgItem(hwnd, IDC_MESSAGE), "");
SetWindowText(GetDlgItem(hwnd, IDC_ADDONMESG), "");
BarGetData(g_hbar, g_tmp, sizeof(g_tmp), NULL, 0);
SendDlgItemMessage(hwnd, IDC_DATA, WM_SETTEXT, 0,
(LONG)(LPSTR)g_tmp);
BarGetAddOnData(g_hbar, g_tmp, sizeof(g_tmp), NULL, 0);
if (!g_tmp[0])
{
EnableWindow(GetDlgItem(hwnd, IDC_ADDONDATA), FALSE);
EnableWindow(GetDlgItem(hwnd, IDC_ADDONMESG), FALSE);
}
else
{
EnableWindow(GetDlgItem(hwnd, IDC_ADDONDATA), TRUE);
EnableWindow(GetDlgItem(hwnd, IDC_ADDONMESG), TRUE);
}
SendDlgItemMessage(hwnd, IDC_ADDONDATA, WM_SETTEXT, 0,
(LONG)(LPSTR)g_tmp);
g_bInvalidData1 = g_bInvalidData2 = FALSE;
InvalidateRect(hwnd, &g_rcCanvas, FALSE);
return 0;
}
break;
case IDC_STYLE:
if (HIWORD(wParam) == LBN_SELCHANGE)
{
InvalidateRect(hwnd, &g_rcCanvas, FALSE);
return 0;
}
break;
case IDC_ORIENT:
if (HIWORD(wParam) == CBN_SELCHANGE)
{
int n;
dwIndex = SendDlgItemMessage(hwnd, IDC_ORIENT, CB_GETCURSEL, 0, 0);
SendDlgItemMessage(hwnd, IDC_ORIENT, CB_GETLBTEXT,
(WPARAM)dwIndex, (LPARAM)(LPSTR)buf);
n = atoi(buf);
if ((g_iOrient - n) % 180 != 0)
{
int tmp;
tmp = g_rc.left; g_rc.left = g_rc.top; g_rc.top = tmp;
tmp = g_rc.right; g_rc.right = g_rc.bottom; g_rc.bottom = tmp;
OffsetRect(&g_rc, -(g_rc.left - g_rcCanvas.left - 5),
-(g_rc.top - g_rcCanvas.top - 5));
}
g_iOrient = n;
InvalidateRect(hwnd, &g_rcCanvas, FALSE);
return 0;
}
break;
case IDC_DATA:
case IDC_MESSAGE:
if (HIWORD(wParam) == EN_KILLFOCUS)
{
SendDlgItemMessage(hwnd, IDC_DATA, WM_GETTEXT,
sizeof(buf), (LPARAM)(LPSTR)buf);
SendDlgItemMessage(hwnd, IDC_MESSAGE, WM_GETTEXT,
sizeof(buf2), (LPARAM)(LPSTR)buf2);
if (g_hbar)
{
g_bInvalidData1 = FALSE;
if (buf[0] == 0)
{
BarGetData(g_hbar, g_tmp, sizeof(g_tmp), NULL, 0);
SendDlgItemMessage(hwnd, IDC_DATA, WM_SETTEXT, 0,
(LONG)(LPSTR)g_tmp);
}
else if (!BarSetData(g_hbar, buf, lstrlen(buf),
buf2, lstrlen(buf2)))
{
g_bInvalidData1 = TRUE;
}
}
InvalidateRect(hwnd, &g_rcCanvas, FALSE);
return 0;
}
break;
case IDC_ADDONDATA:
case IDC_ADDONMESG:
if (HIWORD(wParam) == EN_KILLFOCUS)
{
SendDlgItemMessage(hwnd, IDC_ADDONDATA, WM_GETTEXT,
sizeof(buf), (LPARAM)(LPSTR)buf);
SendDlgItemMessage(hwnd, IDC_ADDONMESG, WM_GETTEXT,
sizeof(buf2), (LPARAM)(LPSTR)buf2);
if (g_hbar)
{
g_bInvalidData2 = FALSE;
if (buf[0] == 0)
{
BarGetAddOnData(g_hbar, g_tmp, sizeof(g_tmp), NULL, 0);
SendDlgItemMessage(hwnd, IDC_ADDONDATA, WM_SETTEXT, 0,
(LONG)(LPSTR)g_tmp);
}
else if (!BarSetAddOnData(g_hbar, buf, lstrlen(buf),
buf2, lstrlen(buf2)))
{
g_bInvalidData2 = TRUE;
}
}
InvalidateRect(hwnd, &g_rcCanvas, FALSE);
return 0;
}
break;
case IDOK:
hCtl = GetFocus();
if (hCtl && (hCtl = GetNextDlgTabItem(hwnd, hCtl, FALSE)))
SetFocus(hCtl);
return 0;
case IDC_BTNADVANCED:
hInst = (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE);
lpfnDlgProc = MakeProcInstance((FARPROC)AdvancedDlgProc, hInst);
if (DialogBox(hInst, MAKEINTRESOURCE(IDD_ADVANCED), hwnd, lpfnDlgProc))
InvalidateRect(hwnd, &g_rcCanvas, FALSE);
FreeProcInstance(lpfnDlgProc);
return 0;
default:
break;
}
return DefWindowProc(hwnd, WM_COMMAND, wParam, lParam);
}
static DWORD getStyle(HWND hwnd)
{
static int ai[50];
int i, c;
DWORD dw;
c = (int)SendDlgItemMessage(hwnd, IDC_STYLE, LB_GETSELITEMS,
(WPARAM)50, (LPARAM)(LPINT)ai);
dw = 0;
for (i = 0; i < c; i++)
dw |= SendDlgItemMessage(hwnd, IDC_STYLE, LB_GETITEMDATA,
(WPARAM)ai[i], (LPARAM)0);
return dw;
}
static void drawBarcode(HWND hwnd, HDC hdc, HDC hicTarget,
HBARCODE hBar, LPRECT lprc)
{
HFONT hf, hf2;
LOGFONT lf;
RECT rc;
if (!g_hbar || !hdc) return;
if (!hicTarget)
{
SetBkMode(hdc, g_iBkMode);
SetBkColor(hdc, g_clfBg);
SetTextColor(hdc, g_clfFg);
hf2 = CreateFontIndirect(&g_logfont);
hf = SelectObject(hdc, hf2);
BarDraw(g_hbar, hdc, NULL, lprc, g_iOrient, getStyle(hwnd));
SelectObject(hdc, hf);
DeleteObject(hf2);
}
else
{
SetBkMode(hicTarget, g_iBkMode);
SetBkColor(hicTarget, g_clfBg);
SetTextColor(hicTarget, g_clfFg);
SetMapMode(hdc, MM_ANISOTROPIC);
SetWindowExtEx(hdc, 1440, 1440, NULL);
SetViewportExtEx(hdc, GetDeviceCaps(hdc, LOGPIXELSX),
-GetDeviceCaps(hdc, LOGPIXELSY), NULL);
SetMapMode(hicTarget, MM_TWIPS);
CopyRect(&rc, lprc);
DPtoLP(hdc, (LPPOINT)&rc, 2);
// physical coord to logical
memcpy(&lf, &g_logfont, sizeof(lf));
lf.lfHeight = MulDiv(lf.lfHeight, 1440,
GetDeviceCaps(hdc, LOGPIXELSY));
lf.lfWidth = 0;
hf2 = CreateFontIndirect(&lf);
hf = SelectObject(hicTarget, hf2);
BarDraw(g_hbar, hdc, hicTarget, &rc, g_iOrient, getStyle(hwnd));
SetMapMode(hdc, MM_TEXT);
SetMapMode(hicTarget, MM_TEXT);
SelectObject(hicTarget, hf);
DeleteObject(hf2);
}
}
static void printBarcode(HWND hwnd, HDC hdcPrint, HBARCODE hBar, LPRECT lprc)
{
HFONT hf, hf2;
LOGFONT lf;
RECT rc;
HDC hdcWnd;
FARPROC lpfnPrintDlgProc;
FARPROC lpfnAbortProc;
HINSTANCE hInst = (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE);
if (!g_hbar || !hdcPrint) return;
lpfnPrintDlgProc = MakeProcInstance((FARPROC)PrintDlgProc, hInst);
g_hPrintDlg = CreateDialog(hInst, MAKEINTRESOURCE(IDD_PRINTABORT),
hwnd, lpfnPrintDlgProc);
lpfnAbortProc = MakeProcInstance((FARPROC)AbortProc, hInst);
Escape(hdcPrint, SETABORTPROC, 0, (LPSTR)lpfnAbortProc, NULL);
SetBkMode(hdcPrint, g_iBkMode);
SetBkColor(hdcPrint, g_clfBg);
SetTextColor(hdcPrint, g_clfFg);
hdcWnd = GetDC(hwnd);
SetMapMode(hdcWnd, MM_ANISOTROPIC);
SetWindowExtEx(hdcWnd, 1440, 1440, NULL);
SetViewportExtEx(hdcWnd, GetDeviceCaps(hdcWnd, LOGPIXELSX),
-GetDeviceCaps(hdcWnd, LOGPIXELSY), NULL);
SetMapMode(hdcPrint, MM_TWIPS);
CopyRect(&rc, lprc);
DPtoLP(hdcWnd, (LPPOINT)&rc, 2);
// physical coord to logical
memcpy(&lf, &g_logfont, sizeof(lf));
lf.lfHeight = MulDiv(lf.lfHeight, 1440,
GetDeviceCaps(hdcWnd, LOGPIXELSY));
lf.lfWidth = 0;
hf2 = CreateFontIndirect(&lf);
hf = SelectObject(hdcPrint, hf2);
if (Escape(hdcPrint, STARTDOC, 7, "Barcode", NULL) > 0)
{
BarDraw(g_hbar, hdcPrint, NULL, &rc, g_iOrient, getStyle(hwnd));
if (Escape(hdcPrint, NEWFRAME, 0, NULL, NULL) > 0)
Escape(hdcPrint, ENDDOC, 0, NULL, NULL);
}
SetMapMode(hdcWnd, MM_TEXT);
SetMapMode(hdcPrint, MM_TEXT);
SelectObject(hdcPrint, hf);
DeleteObject(hf2);
ReleaseDC(hwnd, hdcWnd);
if (g_hPrintDlg)
{
EnableWindow(hwnd, TRUE);
DestroyWindow(g_hPrintDlg);
g_hPrintDlg = 0;
}
FreeProcInstance(lpfnPrintDlgProc);
FreeProcInstance(lpfnAbortProc);
}
BOOL APIENTRY AboutDlgProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
switch(msg) {
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch(wParam) {
case IDOK:
case IDCANCEL:
EndDialog(hwnd, 0);
return TRUE;
}
break;
}
return FALSE;
}
BOOL APIENTRY PrintDlgProc(HWND hDlg, UINT msg,
WPARAM wParam, LPARAM lParam)
{
switch(msg) {
case WM_INITDIALOG:
g_bUserAbort = FALSE;
EnableWindow(GetParent(hDlg), FALSE);
return TRUE;
case WM_COMMAND:
g_bUserAbort = TRUE;
EnableWindow(GetParent(hDlg), TRUE);
DestroyWindow(hDlg);
return TRUE;
case WM_DESTROY:
g_hPrintDlg = 0;
return TRUE;
}
return FALSE;
}
BOOL APIENTRY AbortProc(HDC hdcPrint, short sCode)
{
MSG msg;
while (!g_bUserAbort && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (!g_hPrintDlg || !IsDialogMessage(g_hPrintDlg, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return !g_bUserAbort;
}
BOOL APIENTRY AdvancedDlgProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
int i, n;
switch(msg) {
case WM_INITDIALOG:
wsprintf(g_tmp, "%d", BarGetClearArea(g_hbar, BGCA_LEFT));
SetWindowText(GetDlgItem(hwnd, IDC_SPLEFT), g_tmp);
wsprintf(g_tmp, "%d", BarGetClearArea(g_hbar, BGCA_RIGHT));
SetWindowText(GetDlgItem(hwnd, IDC_SPRIGHT), g_tmp);
wsprintf(g_tmp, "%d", BarGetClearArea(g_hbar, BGCA_TOP));
SetWindowText(GetDlgItem(hwnd, IDC_SPTOP), g_tmp);
wsprintf(g_tmp, "%d", BarGetClearArea(g_hbar, BGCA_BOTTOM));
SetWindowText(GetDlgItem(hwnd, IDC_SPBOTTOM), g_tmp);
wsprintf(g_tmp, "%d", BarGetClearArea(g_hbar, BGCA_MIDDLE));
if (g_tmp[0] == '0')
EnableWindow(GetDlgItem(hwnd, IDC_SPMIDDLE), FALSE);
else
SetWindowText(GetDlgItem(hwnd, IDC_SPMIDDLE), g_tmp);
n = BarGetNumBarExts(g_hbar);
for (i = 0; i < n; i++)
{
wsprintf(g_tmp, "%d", BarGetBarExt(g_hbar, i));
SetWindowText(GetDlgItem(hwnd, IDC_BAR0 + i), g_tmp);
}
for (i = n; i < 4; i++)
EnableWindow(GetDlgItem(hwnd, IDC_BAR0 + i), FALSE);
n = BarGetNumSpaceExts(g_hbar);
for (i = 0; i < n; i++)
{
wsprintf(g_tmp, "%d", BarGetSpaceExt(g_hbar, i));
SetWindowText(GetDlgItem(hwnd, IDC_SPACE0 + i), g_tmp);
}
for (i = n; i < 4; i++)
EnableWindow(GetDlgItem(hwnd, IDC_SPACE0 + i), FALSE);
wsprintf(g_tmp, "%d", BarGetInterCharExt(g_hbar));
if (g_tmp[0] == '0')
EnableWindow(GetDlgItem(hwnd, IDC_INTERCHARGAP), FALSE);
else
SetWindowText(GetDlgItem(hwnd, IDC_INTERCHARGAP), g_tmp);
return TRUE;
case WM_COMMAND:
switch(wParam) {
case IDOK:
SendDlgItemMessage(hwnd, IDC_SPLEFT, WM_GETTEXT,
sizeof(g_tmp), (LPARAM)(LPSTR)g_tmp);
BarSetClearArea(g_hbar, BGCA_LEFT, atoi(g_tmp));
SendDlgItemMessage(hwnd, IDC_SPRIGHT, WM_GETTEXT,
sizeof(g_tmp), (LPARAM)(LPSTR)g_tmp);
BarSetClearArea(g_hbar, BGCA_RIGHT, atoi(g_tmp));
SendDlgItemMessage(hwnd, IDC_SPMIDDLE, WM_GETTEXT,
sizeof(g_tmp), (LPARAM)(LPSTR)g_tmp);
BarSetClearArea(g_hbar, BGCA_MIDDLE, atoi(g_tmp));
SendDlgItemMessage(hwnd, IDC_SPTOP, WM_GETTEXT,
sizeof(g_tmp), (LPARAM)(LPSTR)g_tmp);
BarSetClearArea(g_hbar, BGCA_TOP, atoi(g_tmp));
SendDlgItemMessage(hwnd, IDC_SPBOTTOM, WM_GETTEXT,
sizeof(g_tmp), (LPARAM)(LPSTR)g_tmp);
BarSetClearArea(g_hbar, BGCA_BOTTOM, atoi(g_tmp));
n = BarGetNumBarExts(g_hbar);
for (i = 0; i < n; i++)
{
SendDlgItemMessage(hwnd, IDC_BAR0 + i, WM_GETTEXT,
sizeof(g_tmp), (LPARAM)(LPSTR)g_tmp);
BarSetBarExt(g_hbar, i, atoi(g_tmp));
}
n = BarGetNumSpaceExts(g_hbar);
for (i = 0; i < n; i++)
{
SendDlgItemMessage(hwnd, IDC_SPACE0 + i, WM_GETTEXT,
sizeof(g_tmp), (LPARAM)(LPSTR)g_tmp);
BarSetSpaceExt(g_hbar, i, atoi(g_tmp));
}
SendDlgItemMessage(hwnd, IDC_INTERCHARGAP, WM_GETTEXT,
sizeof(g_tmp), (LPARAM)(LPSTR)g_tmp);
BarSetInterCharExt(g_hbar, atoi(g_tmp));
EndDialog(hwnd, TRUE);
return TRUE;
case IDCANCEL:
EndDialog(hwnd, FALSE);
return TRUE;
}
break;
}
return FALSE;
}