PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV Mobile (précédentes versions) → [WM12] Lecteur de datamatrix
[WM12] Lecteur de datamatrix
Débuté par Joel VILLETTE, 14 sep. 2009 01:37 - Aucune réponse
Posté le 14 septembre 2009 - 01:37
J'essai de réaliser une application qui utilise quickmark pour scaner des datamatrix.
Le code est fourni par Quickmark mais je ne connais pas suffisamment le c++.
Quelqu'un pourrait-il m'aider ?
Voici le code :
// QuickMark msgapi.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "msgapi.h"
#include <windows.h>
#include <commctrl.h>


/***************QuickMark Messenger****************/
//Usage:
//void LaunchQuickMark()
// Launch QuickMark application to the foreground, and wait for result.
//LRESULT QuickMarkMSG( HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
// When QuickMark decode successfully, it will SendMessage(hWnd,WM_COPYDATA,NULL,(LPARAM)&oCopyData).

#include <atlbase.h> //registry
typedef enum {
TYPE_WIDECHAR = 0, //default
TYPE_CHAR
} ReturnStringDataType;
void LaunchQuickMark();
LRESULT QuickMarkMSG( HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam);
UINT RM_QuickMarkMessengerAPIApp = 0; //Window message id: communication with QuickMark.
HWND hwndMain = NULL; //Main Window

//Flow of QuickMark Messenger
//Open msgapi --> Register a window message for inter-application communication (RegisterWindowMessage)
//-->launch QuickMark (Press OK button) --> send a unique message for inter-application communication(HWND_BROADCAST)
// --> QuickMark recevie the handle of msgapi's window. --> Decoding using QuickMark
// --> QuickMark send result to handle. --> msgapi show the result.
//

/***************QuickMark Messenger****************/

#define MAX_LOADSTRING 100

TCHAR szTitle[MAX_LOADSTRING]; // title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name

// Global Variables:
HINSTANCE g_hInst; // current instance
HWND g_hWndMenuBar; // menu bar handle

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}

#ifndef WIN32_PLATFORM_WFSP
HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MSGAPI));
#endif // !WIN32_PLATFORM_WFSP

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
#ifndef WIN32_PLATFORM_WFSP
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
#endif // !WIN32_PLATFORM_WFSP
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}

//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MSGAPI));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;

return RegisterClass(&wc);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

g_hInst = hInstance; // Store instance handle in our global variable

#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
// SHInitExtraControls should be called once during your application's initialization to initialize any
// of the device specific controls such as CAPEDIT and SIPPREF.
//SHInitExtraControls();
#endif // WIN32_PLATFORM_PSPC || WIN32_PLATFORM_WFSP

LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MSGAPI, szWindowClass, MAX_LOADSTRING);

#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
//If it is already running, then focus on the window, and exit
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// set focus to foremost child window
// The "| 0x00000001" is used to bring any owned windows to the foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}
#endif // WIN32_PLATFORM_PSPC || WIN32_PLATFORM_WFSP

if (!MyRegisterClass(hInstance, szWindowClass))
{
return FALSE;
}

hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

#ifdef WIN32_PLATFORM_PSPC
// When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
if (g_hWndMenuBar)
{
RECT rc;
RECT rcMenuBar;

GetWindowRect(hWnd, &rc);
GetWindowRect(g_hWndMenuBar, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);

MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
}
#endif // WIN32_PLATFORM_PSPC


/***************QuickMark Messenger****************/
hwndMain = hWnd;
//obtain a unique message for inter-application communication.
RM_QuickMarkMessengerAPIApp = RegisterWindowMessage(TEXT("QuickMarkMessengerAPIApplication"));
/***************QuickMark Messenger****************/

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

#if defined(SHELL_AYGSHELL) && !defined(WIN32_PLATFORM_WFSP)
static SHACTIVATEINFO s_sai;
#endif // SHELL_AYGSHELL && !WIN32_PLATFORM_WFSP

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_OK:
LaunchQuickMark();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
//#ifdef SHELL_AYGSHELL
SHMENUBARINFO mbi;

memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;

if (!SHCreateMenuBar(&mbi))
{
g_hWndMenuBar = NULL;
}
else
{
g_hWndMenuBar = mbi.hwndMB;
}

#ifndef WIN32_PLATFORM_WFSP
// Initialize the shell activate info structure
memset(&s_sai, 0, sizeof (s_sai));
s_sai.cbSize = sizeof (s_sai);
#endif // !WIN32_PLATFORM_WFSP
//#endif // SHELL_AYGSHELL
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);

// TODO: Add any drawing code here...

EndPaint(hWnd, &ps);
break;
case WM_SIZE:
if(wParam != 1) break;
case WM_DESTROY:
#ifdef SHELL_AYGSHELL
CommandBar_Destroy(g_hWndMenuBar);
#endif // SHELL_AYGSHELL
PostQuitMessage(0);
break;

#if defined(SHELL_AYGSHELL) && !defined(WIN32_PLATFORM_WFSP)
case WM_ACTIVATE:
// Notify shell of our activate message
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
break;
case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
break;
#endif // SHELL_AYGSHELL && !WIN32_PLATFORM_WFSP
case WM_COPYDATA: //QuickMark
QuickMarkMSG( hWnd, message, wParam, lParam);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
#ifdef SHELL_AYGSHELL
{
// Create a Done button and size it.
SHINITDLGINFO shidi;
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
}
#endif // SHELL_AYGSHELL

return (INT_PTR)TRUE;

case WM_COMMAND:
#ifdef SHELL_AYGSHELL
if (LOWORD(wParam) == IDOK)
#endif
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;

case WM_CLOSE:
EndDialog(hDlg, message);
return (INT_PTR)TRUE;

#ifdef _DEVICE_RESOLUTION_AWARE
case WM_SIZE:
{
DRA::RelayoutDialog(
g_hInst,
hDlg,
DRA::GetDisplayMode() != DRA::Portrait ? MAKEINTRESOURCE(IDD_ABOUTBOX_WIDE) : MAKEINTRESOURCE(IDD_ABOUTBOX));
}
break;
#endif
}
return (INT_PTR)FALSE;
}

///////////////////////////////////////////////////////
void LaunchQuickMark()
{
//Step 1: QueryStringValue from registry for Launching QuickMark.
TCHAR szQuickMarkApp[MAX_PATH];
TCHAR temp[1024];
CRegKey key;
HRESULT hr;
DWORD dwSize = 0;
TCHAR szQMReg[] = _T("Software\\QuickMark");

memset(szQuickMarkApp,0,MAX_PATH*sizeof(TCHAR));
memset(temp,0,1024*sizeof(TCHAR));

hr = key.Open(HKEY_CURRENT_USER, szQMReg);
if (hr == ERROR_SUCCESS)
{
if (key.QueryStringValue(_T("QuickMarkAppPath"),NULL,&dwSize) == ERROR_SUCCESS)
key.QueryStringValue(_T("QuickMarkAppPath"), szQuickMarkApp, &dwSize);
}
key.Close();

//Step 2: Set default application path of QuickMark.
if (_tcslen(szQuickMarkApp) == 0)
wsprintf(szQuickMarkApp, L"\\Program Files\\QuickMark\\QuickMark.exe"); //default path

//Step 3: Launch QuickMark.
HANDLE h = NULL;
WIN32_FIND_DATA f;
int checkWin = 0;
h = FindFirstFile(szQuickMarkApp, &f);
if(h != INVALID_HANDLE_VALUE)
{
SHELLEXECUTEINFO ShExecInfo;
memset(&ShExecInfo, 0, sizeof(SHELLEXECUTEINFO));
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.lpVerb = L"Open";
ShExecInfo.lpFile =szQuickMarkApp;
ShExecInfo.lpParameters = L"";
ShExecInfo.nShow = SW_SHOW;
ShellExecuteEx(&ShExecInfo);
Sleep(1000); //wait one second.
}
FindClose(h);

//step 4: send RM_QuickMarkMessengerAPIApp to all top windows.
//parameters:
// HWND hWnd : HWND_BROADCAST , all top windows.
// UINT Msg : A unique message for inter-application communication.
// WPARAM wParam : Handle to the window whose window procedure will receive the decoded result.
// LPARAM lParam : Set QuickMark application to 1D or 2D scanning. (1:1D 2:2D)
int DefaultBarcodeType = 2; //Default is 2D scanning.
PostMessage(HWND_BROADCAST, RM_QuickMarkMessengerAPIApp, (WPARAM)hwndMain, (LPARAM)DefaultBarcodeType);
}

LRESULT QuickMarkMSG( HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
//When QuickMark decode successfully, it will callback.

//Receive the decoded result.
//STRUCT COPYDATASTRUCT member
//dwData:Data types of data.
// TYPE_CHAR: The data is a Binary data.
// TYPE_WIDECHAR: The data is a Unicode string.
//lpData:Long pointer to data.
//cbData:Specifies the size, in bytes, of the data pointed to by the lpData member.

COPYDATASTRUCT* oCopyData;
oCopyData=(COPYDATASTRUCT *)lParam;

if (oCopyData->dwData == TYPE_CHAR)
{
//Implement
//TODO: Add your code here.
//Result(Binary Data)
wchar_t szUCS2[1024];
int nLength=MultiByteToWideChar(65001,0,(char *)oCopyData->lpData,-1,NULL,0);
MultiByteToWideChar(65001, 0, (char *)oCopyData->lpData,oCopyData->cbData+1, szUCS2,nLength);
MessageBox(hWnd, szUCS2, TEXT("Result"), MB_OK);
}else{
wchar_t receive[1024];
wsprintfW(receive,(LPCWSTR)oCopyData->lpData);
MessageBox(hWnd, receive, TEXT("Result"), MB_OK);
}
return 1;
}