Click here to Skip to main content
15,891,708 members
Articles / Programming Languages / C++

A Copy Utility using TWAIN

Rate me:
Please Sign up or sign in to vote.
4.54/5 (10 votes)
14 Jul 20042 min read 100K   6.8K   47  
Example for a simple encapsulation of the TWAIN interface
///////////////////////////////////////////////////////////////////////////////
//
//		CTwainEventHandler
//		------------------
//		Subclassing (main) window for Twain messages
//
////Holger Kloos, 2003/////////////////////////////////////////////////////////


#include "stdafx.h"
#include "TwainEventHandler.h"


//  CTwainEventHandler
//	------------------


CTwainEventHandler::CTwainEventHandler()
{
	m_pMsgLink = NULL;
	m_OrgWndProc = NULL;
	m_hWnd = NULL;
}

CTwainEventHandler::~CTwainEventHandler()
{
}	

BOOL CTwainEventHandler::Connect(IMsgLink*	pMsgLink)
{
	ASSERT(m_OrgWndProc == NULL);
	ASSERT(m_hWnd);

	m_pMsgLink = pMsgLink;

	if (GetWindowLong(m_hWnd, GWL_USERDATA) == 0)
		SetWindowLong(m_hWnd, GWL_USERDATA, (DWORD)this);
	else
		return FALSE;

	m_OrgWndProc = (WNDPROC)SetWindowLong(m_hWnd, GWL_WNDPROC, (DWORD)WindowProc);

	return m_OrgWndProc != NULL;
}
	
BOOL CTwainEventHandler::DisConnect()
{
	if (!m_OrgWndProc)
		return FALSE;

	BOOL bRes = SetWindowLong(m_hWnd, GWL_WNDPROC, (DWORD)m_OrgWndProc) != NULL;
	SetWindowLong(m_hWnd, GWL_USERDATA, 0);
	m_OrgWndProc = NULL;

	return bRes;
}

LRESULT CTwainEventHandler::OnWndMsg(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) const
{
	if (m_pMsgLink)
	{
		MSG msg;

		msg.time = GetMessageTime();

		DWORD nPts = GetMessagePos();
		POINTS pts = MAKEPOINTS(nPts);
		msg.pt.x = pts.x;	msg.pt.y = pts.y;
		
		msg.hwnd = hwnd;
		msg.message = uMsg;
		msg.wParam = wParam;
		msg.lParam = lParam;
		if (m_pMsgLink->OnWinMsg(&msg))
			return 0;
	}
	return CallWindowProc(m_OrgWndProc, hwnd, uMsg, wParam, lParam);
}

LRESULT CALLBACK CTwainEventHandler::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	CTwainEventHandler* pThis = reinterpret_cast <CTwainEventHandler*>((void*)GetWindowLong(hwnd, GWL_USERDATA));
	if (pThis)
		return pThis->OnWndMsg(hwnd, uMsg, wParam, lParam);

	ASSERT(0);

	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions