?? imagescanforgood.cpp
字號:
完整的圖像掃描類
發布時間:2007-11-19 23:37:47.0 瀏覽人數:377
// ctwain.h - application interface to TWAIN Protocol
//
#ifndef CTWAIN_H
#define CTWAIN_H
#include "twain.h"
//結構和用法:
// CTwain 最好作為 mainframe的第二個基類來使用,
// 這樣,回調比較方便,程序也比較緊湊。
//
// RegisterApp 可以填入真實信息。
//
// 0. If !TwainAvailable(), 無twain,菜單被禁止,無須其他處理。
// 1. 在main frame window 建立后(on create),調用SetDefWindow(this),
// 和 OpenSourceManager()。如果 OpenSourceManager() 失敗,你可以
// 提示用戶twain 出錯(這需要你附加代碼).
// 2. 當State() == SOURCE_MANAGER_OPEN 時,使 Acquire and Select Source
// 命令可用。
// 3. 在 Acquire 命令響應中,調用 BeginAcquire.
// 4. Override CWnd::PreTranslateMessage(pMsg),加入:
// return (TwainMessageHook(pMsg) ||
// <base class>::PreTranslateMessage(pMsg);
// 5. Override CTwain::DibReceived 處理native transfers返回的dib句柄。
// 注意:在調用BeginAcquire后, 你可以傳輸多幅圖象,data source
// 始終打開著,若要關閉,調用 CloseSource.
// 可待擴展:Add support for Memory and File transfer(可能不標準).
typedef enum {
NO_TWAIN_STATE, // 0 internal use only
PRE_SESSION, // 1 ground state, nothing loaded
SOURCE_MANAGER_LOADED, // 2 DSM loaded but not open
SOURCE_MANAGER_OPEN, // 3 DSM open
SOURCE_OPEN, // 4 some Source open - Negotiation state!
SOURCE_ENABLED, // 5 acquisition started
TRANSFER_READY, // 6 data ready to transfer
TRANSFERRING // 7 transfer started
} TW_STATE;
typedef enum {
TWERR_OPEN_DSM, // unable to load or open Source Manager
TWERR_OPEN_SOURCE, // unable to open Datasource
TWERR_ENABLE_SOURCE, // unable to enable Datasource
} TW_ERR;
class CTwain
{
public:
BOOL SetPixelType(TW_UINT32 pxlType);
CTwain(void);
~CTwain();
// General notes:
// 1. 一旦source manager 被裝載, 直到對象被釋放才被卸載。
// (或UnloadSourceManager)
// 第一次調用TwainAvailable時裝載 source manager.
// attributes i.e. query functions
int TwainAvailable(void);
// TRUE ,如果 TWAIN Datasource Manager 存在并被裝載。
// 不檢查是否有 datasources!
// 可用IsAvailable 來 enable or disable menu items .
TW_STATE State(void);
// Return the current (presumed) state of the Twain connection.
unsigned ResultCode(void);
// last result code (see twain.h)
unsigned ConditionCode(void);
// retrieve condition code from last triplet - see twain.h.
// Top-level operations
void SetDefWindow(CWnd* pWnd);
//def window 是主窗口。
// If no default window is set, AfxGetMainWnd() is used.
int SelectSource(CWnd* pWnd = NULL);
// Post the standard Select Source dialog
void ModalAcquire(CWnd* pWnd = NULL);
// Acquire images from current or default source.
// By default, displays the u/i of the source and acquires images
// until a CLOSEDSREQ is received from the source.
// Acquire returns the Twain connection to the starting state
// or to SOURCE_ENABLED, whichever is lower.
int BeginAcquire(CWnd* pWnd = NULL);
// Open and enable the default source.
// TRUE if successful, FALSE if something goes wrong.
// If successful, State() == SOURCE_ENABLED.
// You must now pass all messages to MessageHook until the state
// drops below SOURCE_OPEN: In MFC, override PreTranslateMessage.
// If BeginAcquire fails, it returns TWAIN to the state it
// found it in.
void RegisterApp( // Record application information
int nMajorNum, int nMinorNum, // major and incremental revision of application. E.g.
// for version 2.1, nMajorNum == 2 and nMinorNum == 1
int nLanguage, // language of this version (use TWLG_xxx from TWAIN.H)
int nCountry, // country of this version (use TWCY_xxx from TWAIN.H)
LPSTR lpszVersion, // version info string e.g. "1.0b3 Beta release"
LPSTR lpszMfg, // name of manufacturer/developer e.g. "Crazbat Software"
LPSTR lpszFamily, // product family e.g. "BitStomper"
LPSTR lpszProduct); // specific product e.g. "BitStomper Deluxe Pro"
// This is not necessary for acquisition, but is required for full
// TWAIN compliance.
// 內部操作:
int OpenSourceManager(CWnd* pWnd = NULL);
// (Load and) open the Datasource Manager
// Loads the DSM if necessary, opens it if necessary.
// If State >= 3, does nothing and returns TRUE.
int OpenDefaultSource(void);
// Open the default datasource (last source selected in Select Source dialog.)
// Invalid (returns FALSE) if State > 3 (a source is already open)
// Invalid (returns FALSE) if State < 3 (DSM is not open)
// If successful, returns TRUE with State == 4.
// Otherwise, returns FALSE with State unchanged.
void SetShowUI(BOOL bShow = 1);
// Set flag for whether source should be enabled with
// user interface visible (bShow == TRUE) or not.
// At construction, ShowUI is set TRUE.
BOOL GetShowUI(void);
// Return state of ShowUI flag.
int EnableSource(CWnd* pWnd = NULL);
// Enable the open source, which allows image acquisition to begin.
// Invalid if State != 4 (source open).
// If successful, returns TRUE with State == 5.
// Otherwise, returns FALSE with State unchanged.
int DisableSource(void);
// Disable the current source.
// If State == 5, disables the current source and returns TRUE.
// If State < 5, does nothing and returns TRUE.
// If State > 5, does nothing and returns FALSE.
int CloseSource(void);
int CloseSourceManager(CWnd* pWnd = NULL);
int UnloadSourceManager(void);
int DropToState(int nS, CWnd* pWnd = NULL);
// 底層操作
int LoadSourceManager(void);
// loads the DSM (Datasource Manager) into process space
void ModalEventLoop(void);
// get and dispatch messages until source is disabled.
int TwainMessageHook(LPMSG lpmsg);
int EndXfer(void);
// In State 7, ends the current transfer by sending MSG_ENDXFER.
// If successful, goes to State 6 if there are more transfers
// available (pendingXfers != 0), or to State 5 if not.
// Returns TRUE if the resulting State < 7.
int CancelXfers(void);
// In State 6, cancels any pending transfers.
// (In State 7, does an EndXfer first)
// If successful, goes to State 5.
// Returns TRUE if the resulting State < 6.
// DS,SM
int DS(unsigned long dg, unsigned dat, unsigned msg, void FAR *pd);
// send a triplet to the current Datasource.
// returns TRUE if the result code (rc) == RC_SUCCESS, FALSE otherwise.
// Note that this is not meaningful with some triplets.
// Does ASSERT(nState < 4);
int SM(unsigned long dg, unsigned dat, unsigned msg, void FAR *pd);
// Send a triplet to the Source Manager.
// returns TRUE if the result code (rc) == RC_SUCCESS, FALSE otherwise.
// Note that this is not meaningful with some triplets.
// Does ASSERT(nState > 1);
// 回調函數
virtual void XferReady(LPMSG lpmsg);
// called when source has one or more xfers ready
// This default method assumes XferMech = Native,
// gets each native (DIB) image and calls back to DibReceived.
virtual void CloseDsRequest(void);
// called when the open source asks to be 'closed'
// It is sufficient to disable the source on this request,
// but this default handler calls CloseSource.
virtual void DibReceived(HGLOBAL hDib);
// called by the default XferReady handler
// when it has successfully transferred a DIB.
// This default handler just calls GlobalFree(hDib).
virtual void StateChange(int nState);
// called after each Twain State transition.
// nState is the new State. When this callback
// occurs, the state transition has already happened.
// Note - first call is the transition to State 1
// which occurs at construction.
virtual void TwainError(TW_ERR e);
// Called when an unexpected TWAIN malfunction occurs.
// See TW_ERR declaration at beginning of this file.
protected:
void SetState(TW_STATE nS); // assume Twain in State nS
CWnd* DefWnd(CWnd* pWnd = NULL);
// Returns pWnd if it's non-null, otherwise it
// finds a safe non-null default substitute.
BOOL bTrace; // enable TRACE output
TW_STATE nState; // current state
TW_STATE nStartState; // starting state for some operation
TW_IDENTITY AppId; // application identity structure
TW_INT16 rc; // last result code
TW_USERINTERFACE twUI;
BOOL bShowUI;
TW_PENDINGXFERS pendingXfers;
HANDLE hDib; // bitmap returned by native transfer
CWnd* pDefWnd; // default window
};
#endif
// ctwain.cpp - implementation of a mfc TWAIN class
//進行掃描處理的類
#include "stdafx.h"
#include "ctwain.h"
#ifdef _DEBUG
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -