コード:
#include "DxLib.h"
#include <windowsx.h>
#include "wintab.h"
#define PACKETDATA (PK_X | PK_Y | PK_BUTTONS | PK_NORMAL_PRESSURE)
#define PACKETMODE PK_BUTTONS
#include "pktdef.h"
extern "C" {
#include "Utils.h"
}
static HANDLE hInst = NULL;
char* gpszProgramName = "DXPRSTEST";
static LOGCONTEXT glogContext = {0};
static HCTX hCtx = NULL;
static HCTX TabletInit(HWND hWnd);
static LRESULT CALLBACK DxMainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static POINT ptNew;
static UINT prsNew;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
hInst = hInstance;
if (!LoadWintab()) return 0;
if (!gpWTInfoA(0, 0, NULL)) goto L_exit_wt;
ChangeWindowMode(TRUE);
if (DxLib_Init() != 0) return 0;
SetDrawScreen(DX_SCREEN_BACK);
hCtx = TabletInit(GetMainWindowHandle());
if (!hCtx) goto L_exit_dx;
SetHookWinProc(DxMainWndProc);
// 初回アクティブ化
gpWTEnable(hCtx, TRUE);
gpWTOverlap(hCtx, TRUE);
while (ProcessMessage() == 0 && ScreenFlip() == 0 && ClearDrawScreen() == 0) {
POINT scrPoint = { ptNew.x, ptNew.y };
ScreenToClient(GetMainWindowHandle(), &scrPoint);
DrawCircle(scrPoint.x, scrPoint.y, prsNew, GetColor(127,127,127), TRUE);
DrawLine(0, scrPoint.y, 640, scrPoint.y, GetColor(255,255,255));
DrawLine(scrPoint.x, 0, scrPoint.x, 480, GetColor(255,255,255));
}
gpWTClose(hCtx);
L_exit_dx:
DxLib_End();
L_exit_wt:
UnloadWintab();
return 0;
}
///////////////////////////////////////////////////////////////////////////////
static HCTX TabletInit(HWND hWnd)
{
HCTX hctx = NULL;
UINT wDevice = 0;
UINT wExtX = 0;
UINT wExtY = 0;
UINT wWTInfoRetVal = 0;
AXIS TabletX = {0};
AXIS TabletY = {0};
// Set option to move system cursor before getting default system context.
glogContext.lcOptions |= CXO_SYSTEM;
// Open default system context so that we can get tablet data
// in screen coordinates (not tablet coordinates).
wWTInfoRetVal = gpWTInfoA(WTI_DEFSYSCTX, 0, &glogContext);
WACOM_ASSERT( wWTInfoRetVal == sizeof( LOGCONTEXT ) );
WACOM_ASSERT( glogContext.lcOptions & CXO_SYSTEM );
// modify the digitizing region
wsprintf(glogContext.lcName, "PrsTest Digitizing %x", hInst);
// We process WT_PACKET (CXO_MESSAGES) messages.
glogContext.lcOptions |= CXO_MESSAGES;
// What data items we want to be included in the tablet packets
glogContext.lcPktData = PACKETDATA;
// Which packet items should show change in value since the last
// packet (referred to as 'relative' data) and which items
// should be 'absolute'.
glogContext.lcPktMode = PACKETMODE;
// This bitfield determines whether or not this context will receive
// a packet when a value for each packet field changes. This is not
// supported by the Intuos Wintab. Your context will always receive
// packets, even if there has been no change in the data.
glogContext.lcMoveMask = PACKETDATA;
// Which buttons events will be handled by this context. lcBtnMask
// is a bitfield with one bit per button.
glogContext.lcBtnUpMask = glogContext.lcBtnDnMask;
// Set the entire tablet as active
wWTInfoRetVal = gpWTInfoA( WTI_DEVICES + 0, DVC_X, &TabletX );
WACOM_ASSERT( wWTInfoRetVal == sizeof( AXIS ) );
wWTInfoRetVal = gpWTInfoA( WTI_DEVICES, DVC_Y, &TabletY );
WACOM_ASSERT( wWTInfoRetVal == sizeof( AXIS ) );
glogContext.lcInOrgX = 0;
glogContext.lcInOrgY = 0;
glogContext.lcInExtX = TabletX.axMax;
glogContext.lcInExtY = TabletY.axMax;
// Guarantee the output coordinate space to be in screen coordinates.
glogContext.lcOutOrgX = GetSystemMetrics( SM_XVIRTUALSCREEN );
glogContext.lcOutOrgY = GetSystemMetrics( SM_YVIRTUALSCREEN );
glogContext.lcOutExtX = GetSystemMetrics( SM_CXVIRTUALSCREEN ); //SM_CXSCREEN );
// In Wintab, the tablet origin is lower left. Move origin to upper left
// so that it coincides with screen origin.
glogContext.lcOutExtY = -GetSystemMetrics( SM_CYVIRTUALSCREEN ); //SM_CYSCREEN );
// Leave the system origin and extents as received:
// lcSysOrgX, lcSysOrgY, lcSysExtX, lcSysExtY
// open the region
// The Wintab spec says we must open the context disabled if we are
// using cursor masks.
hctx = gpWTOpenA( hWnd, &glogContext, FALSE );
WacomTrace("HCTX: %i\n", hctx);
return hctx;
}
///////////////////////////////////////////////////////////////////////////////
static LRESULT CALLBACK DxMainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PACKET pkt;
switch (uMsg)
{
case WT_PACKET:
if (gpWTPacket((HCTX)lParam, wParam, &pkt))
{
if (HIWORD(pkt.pkButtons)==TBN_DOWN)
{
MessageBeep(0);
}
ptNew.x = pkt.pkX;
ptNew.y = pkt.pkY;
prsNew = pkt.pkNormalPressure;
}
break;
case WM_ACTIVATE:
/* if switching in the middle, disable the region */
if (hCtx) {
gpWTEnable(hCtx, GET_WM_ACTIVATE_STATE(wParam, lParam));
if (hCtx && GET_WM_ACTIVATE_STATE(wParam, lParam))
gpWTOverlap(hCtx, TRUE);
}
break;
}
return 0;
}