📄 mouse.cpp
字号:
// mouse.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "mouse.h"
#include <windows.h>
#include <ddraw.h>
//-----------------------------------------------------------------------------
// Local definitions
//-----------------------------------------------------------------------------
#define NAME TEXT("OverlayMouseWndClass")
#define TITLE TEXT("Mouse")
#define CURSOR_WIDTH 32
#define CURSOR_HEIGHT 32
static TCHAR CursorImg[] = TEXT("IDB_BITMAP1");
static RECT rs;
static RECT rd;
//-----------------------------------------------------------------------------
// Global data
//-----------------------------------------------------------------------------
LPDIRECTDRAW4 g_pDD = NULL; // DirectDraw object
LPDIRECTDRAWSURFACE4 g_pDDSPrimary = NULL; // Primary Surface.
LPDIRECTDRAWSURFACE4 g_pDDSOverlay = NULL; // The overlay primary.
// Overlay position and velocity data.
int g_nOverlayXPos, g_nOverlayYPos;
int g_nOverlayXVel, g_nOverlayYVel;
int g_nOverlayWidth, g_nOverlayHeight;
DWORD g_dwOverlayXPositionAlignment;
// Our instance handle.
HINSTANCE g_hInstance;
// These are the pixel formats this app supports. Most display adapters
// with overlay support will recognize one or more of these formats.
// We start with YUV format, then work down to RGB. (All 16 bpp.)
static DDPIXELFORMAT ddpfOverlayFormats[] = {
{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x7C00, 0x03e0, 0x001F, 0}, // 16-bit RGB 5:5:5
{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0xF800, 0x07e0, 0x001F, 0} , // 16-bit RGB 5:6:5
{sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('Y','U','Y','V'),0,0,0,0,0}, // YUYV
{sizeof(DDPIXELFORMAT), DDPF_FOURCC, MAKEFOURCC('U','Y','V','Y'),0,0,0,0,0} // UYVY
};
#define PF_TABLE_SIZE 4
// Forward declarations of functions included in this code module:
BOOL InitInstance (HINSTANCE, int);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
static HRESULT InitFail(HWND hWnd, HRESULT hRet, LPCTSTR szError);
static void ReleaseAllObjects(void);
static HRESULT LoadCursorImages();
static BOOL LoadImageOntoSurface(LPDIRECTDRAWSURFACE4 lpdds, LPCTSTR lpstrResID);
static BOOL CopyBitmapToYUVSurface(LPDIRECTDRAWSURFACE4 lpDDSurf, HBITMAP hbm);
static void MoveMouse(int xPos,int yPos);
static HRESULT RestoreAllSurfaces();
///////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
g_hInstance = hInstance;
// Perform application initialization:
if (!InitInstance (hInstance,SW_HIDE /*nCmdShow*/))
{
return FALSE;
}
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//
// FUNCTION: InitInstance(HANDLE, 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;
WNDCLASS wc;
DDSURFACEDESC2 ddsd;
DDCAPS ddcaps;
HRESULT hRet;
LPDIRECTDRAW pDD;
DWORD dwUpdateFlags = 0;
DDOVERLAYFX ovfx;
// Set up and register window class.
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MOUSE));
wc.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_CURSOR));
wc.hbrBackground = (HBRUSH )GetStockObject(NULL_PEN);
wc.lpszMenuName = NULL;
wc.lpszClassName = NAME;
RegisterClass(&wc);
// Create a window.
hWnd = CreateWindowEx(WS_EX_TOPMOST,
NAME,
TITLE,
WS_POPUP,
0,
0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL,
NULL,
hInstance,
NULL);
if (!hWnd)
return FALSE;
// We never show the window, only set focus to it.
//ShowWindow(hWnd, SW_SHOW);
//UpdateWindow(hWnd);
SetFocus(hWnd);
// Create the main DirectDraw object
hRet = DirectDrawCreate(NULL, &pDD, NULL);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, TEXT("DirectDrawCreate FAILED"));
// Fetch DirectDraw4 interface.
hRet = pDD->QueryInterface(IID_IDirectDraw4, (LPVOID *) & g_pDD);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, TEXT("QueryInterface FAILED"));
pDD->Release();
pDD = NULL;
// Get normal mode.
hRet = g_pDD->SetCooperativeLevel(hWnd, DDSCL_NORMAL);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, TEXT("SetCooperativeLevel FAILED"));
// Get a primary surface interface pointer (only needed for init.)
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSPrimary, NULL);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, TEXT("CreateSurface FAILED"));
// See if we can support overlays.
memset(&ddcaps, 0, sizeof(ddcaps));
ddcaps.dwSize = sizeof(ddcaps);
hRet = g_pDD->GetCaps(&ddcaps,NULL);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, TEXT("GetCaps FAILED"));
if (!(ddcaps.dwCaps & DDCAPS_OVERLAY))
return InitFail(hWnd, hRet, TEXT("Overlays are not supported in hardware!"));
// Get alignment info to compute our overlay surface size.
rs.left = 0;
rs.top = 0;
rs.right = CURSOR_WIDTH;
rs.bottom =CURSOR_HEIGHT;
if (ddcaps.dwCaps & DDCAPS_ALIGNSIZESRC && ddcaps.dwAlignSizeSrc)
rs.right += rs.right % ddcaps.dwAlignSizeSrc;
// Create the overlay flipping surface. We will attempt the pixel formats
// in our table one at a time until we find one that jives.
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_FLIP | DDSCAPS_COMPLEX |
DDSCAPS_VIDEOMEMORY;
ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_BACKBUFFERCOUNT |
DDSD_PIXELFORMAT;
ddsd.dwWidth = rs.right;
ddsd.dwHeight = rs.bottom;
ddsd.dwBackBufferCount = 2;
int i = 0;
do {
ddsd.ddpfPixelFormat = ddpfOverlayFormats[i];
hRet = g_pDD->CreateSurface(&ddsd, &g_pDDSOverlay, NULL);
} while (hRet != DD_OK && (++i < PF_TABLE_SIZE));
if (hRet != DD_OK)
return InitFail(hWnd, hRet, TEXT("Unable to create overlay surface!"));
// Load the images.
if (LoadCursorImages() != DD_OK)
return InitFail(hWnd, hRet, TEXT("Unable to load images to overlay surface!"));
// Finish setting up the overlay.
// int StretchFactor1000 = ddcaps.dwMinOverlayStretch > 1000 ? ddcaps.dwMinOverlayStretch : 1000;
rd.left=GetSystemMetrics(SM_CXSCREEN)/2;
rd.top=GetSystemMetrics(SM_CYSCREEN)/2;
// Adding 999 takes care of integer truncation problems.
rd.right =rd.left+rs.right- rs.left ;
rd.bottom = rd.top +rs.bottom -rs.top;
if (ddcaps.dwCaps & DDCAPS_ALIGNSIZEDEST && ddcaps.dwAlignSizeDest)
rd.right = (int)((rd.right + ddcaps.dwAlignSizeDest - 1)/ ddcaps.dwAlignSizeDest) *
ddcaps.dwAlignSizeDest;
// Set the flags we'll send to UpdateOverlay
dwUpdateFlags = DDOVER_SHOW;
// Does the overlay hardware support source color keying?
// If so, we can hide the black background around the image.
// This probably won't work with YUV formats
memset(&ovfx, 0, sizeof(ovfx));
ovfx.dwSize = sizeof(ovfx);
if (ddcaps.dwCKeyCaps & DDCKEYCAPS_SRCOVERLAY)
{
dwUpdateFlags |= DDOVER_KEYSRCOVERRIDE | DDOVER_DDFX;
// Create an overlay FX structure so we can specify a source color key.
// This information is ignored if the DDOVER_SRCKEYOVERRIDE flag
// isn't set.
ovfx.dckSrcColorkey.dwColorSpaceLowValue=0; // black as the color key
ovfx.dckSrcColorkey.dwColorSpaceHighValue=0;
}
// Update the overlay parameters.
hRet = g_pDDSOverlay->UpdateOverlay(&rs, g_pDDSPrimary, &rd, dwUpdateFlags, &ovfx);
if (hRet != DD_OK)
return InitFail(hWnd, hRet, TEXT("Unable to show overlay surface!"));
// Set a bunch of position and velocity module vars.
g_nOverlayXPos = 0;
g_nOverlayYPos = 0;
g_nOverlayXVel = 0;
g_nOverlayYVel = 0;
g_nOverlayWidth = rd.right - rd.left;
g_nOverlayHeight = rd.bottom - rd.top;
// Set the "destination position alignment" global so we won't have to
// keep calling GetCaps() everytime we move the overlay surface.
if (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYDEST)
g_dwOverlayXPositionAlignment = ddcaps.dwAlignBoundaryDest;
else
g_dwOverlayXPositionAlignment = 0;
// Create a timer to flip the pages.
//return DD_OK;
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// 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)
{
switch (message)
{
case WM_CREATE:
break;
case WM_KEYDOWN:
// Handle any non-accelerated key commands
switch (wParam)
{
case VK_ESCAPE:
case VK_F12:
ReleaseAllObjects();
PostMessage(hWnd, WM_CLOSE, 0, 0);
return 0L;
}
break;
case WM_PAINT:
break;
case WM_MOUSEMOVE:
MoveMouse(LOWORD(lParam),HIWORD(lParam) );
break;
/* case WM_DESTROY:
// CommandBar_Destroy(hwndCB);
ReleaseAllObjects();
PostQuitMessage(0);
break;*/
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//-----------------------------------------------------------------------------
// Name: InitFail()
// Desc: This function is called if an initialization function fails
//-----------------------------------------------------------------------------
#define PREFIX TEXT("MOSQUITO: ")
#define PREFIX_LEN 10
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -