Class canicur pour les curseur animé & clasic (.ani, .cur)

Soyez le premier à donner votre avis sur cette source.

Snippet vu 8 812 fois - Téléchargée 37 fois

Contenu du snippet

Cette classe permet de gérer les différent type de curseurs à afficher dans un programme C++.

Elle permet l'importation des curseur inclus lors de la compilation mais aussi dans les fichiers exe & dll exterieure.

Source / Exemple :


/////////////////////////////////////////////////////////////////////////////
// CAniCur.h 

//class DLL_Porting CAniCur : public CStatic
class CAniCur
{
// Construction/destruction
public:
    CAniCur();
    virtual ~CAniCur();

// Attributes
public:

// Operations
public:

    void SetCursor(HCURSOR hCursor);
    HCURSOR GetCursor() const;

    HCURSOR LoadCursorDefault();

    HCURSOR LoadCursorEx(char *strDir, UINT nResID);

    HCURSOR LoadCursor(UINT nResID);
    HCURSOR LoadCursor(HINSTANCE hInstance, UINT nResID);
    
	HCURSOR LoadAnimatedCursorEx(char *strDir, UINT nResID);

    HCURSOR LoadAnimatedCursor(UINT nResID);
    HCURSOR LoadAnimatedCursor(HINSTANCE hInstance, UINT nResID);

    void SetDefaultCursor();
    
    //BOOL OnSetCursor();
  
// Overrides
	
// Implementation
protected:
   
// Protected attributes
protected:
    HCURSOR  m_hCursor;        // Cursor for AniCur

    // Generated message map functions
};

/////////////////////////////////////////////////////////////////////////////
// CAniCur

CAniCur::CAniCur()
{
    m_hCursor = NULL;          // No cursor as yet
}

/////////////////////////////////////////////////////////////////////////////
// CAniCur message handlers
/*
BOOL CAniCur::OnSetCursor() 
{
    if(m_hCursor)
    {
        ::SetCursor(m_hCursor);
        return TRUE;
    }
    return FALSE;
}

  • /
///////////////////////////////////////////////////////////////////////////// // CAniCur operations void CAniCur::SetCursor(HCURSOR hCursor) { m_hCursor = hCursor; if(m_hCursor == NULL) LoadCursorDefault(); } HCURSOR CAniCur::GetCursor() const { return m_hCursor; } ///////////////////////////////////////////////////////////////////////////// // CAniCur implementation // The following appeared in Paul DiLascia's Jan 1998 MSJ articles. // It loads a "hand" cursor from the winhlp32.exe module HCURSOR CAniCur::LoadCursorDefault() { if(m_hCursor == NULL) // No cursor handle - load our own { // Get the windows directory CString strWndDir; GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH); strWndDir.ReleaseBuffer(); strWndDir += _T("\\winhlp32.exe"); // This retrieves cursor #106 from winhlp32.exe, which is a hand pointer HMODULE hModule = ::LoadLibrary(strWndDir); if(hModule) { m_hCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106)); //if(hCursor) m_hCursor = CopyCursor(hCursor); FreeLibrary(hModule); } } return m_hCursor; } HCURSOR CAniCur::LoadCursorEx(char *strDir, UINT nResID) { HMODULE hModule = ::LoadLibrary( strDir ); if(hModule) { m_hCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(nResID)); FreeLibrary(hModule); } if(m_hCursor == NULL) LoadCursorDefault(); return m_hCursor; } HCURSOR CAniCur::LoadCursor(UINT nResID) { m_hCursor = (HCURSOR) ::LoadCursor(NULL, (LPCTSTR)nResID); if(m_hCursor == NULL) LoadCursorDefault(); return m_hCursor; } HCURSOR CAniCur::LoadCursor(HINSTANCE hInstance, UINT nResID) { m_hCursor = (HCURSOR) ::LoadCursor(hInstance, (LPCTSTR)nResID); if(m_hCursor == NULL) LoadCursorDefault(); return m_hCursor; } HCURSOR CAniCur::LoadAnimatedCursorEx(char *strDir, UINT nResID) { HMODULE hModule = ::LoadLibrary( strDir ); if(hModule) { m_hCursor = LoadAnimatedCursor(hModule, (UINT)MAKEINTRESOURCE(nResID)); FreeLibrary(hModule); } if(m_hCursor == NULL) LoadCursorDefault(); return m_hCursor; } HCURSOR CAniCur::LoadAnimatedCursor(UINT nResID) { HRSRC hRes=FindResource(NULL,MAKEINTRESOURCE(nResID),"ANICURSORS"); DWORD dwSize=SizeofResource(NULL,hRes); HGLOBAL hGlob=LoadResource(NULL,hRes); LPBYTE pBytes=(LPBYTE)LockResource(hGlob); m_hCursor=(HCURSOR)CreateIconFromResource(pBytes,dwSize,FALSE,0x00030000); if(m_hCursor == NULL) LoadCursorDefault(); return m_hCursor; } HCURSOR CAniCur::LoadAnimatedCursor(HINSTANCE hInstance, UINT nResID) { /* HRSRC hRes=FindResource(AfxGetInstanceHandle(),MAKEINTRESOURCE(nResID),"ANICURSORS"); DWORD dwSize=SizeofResource(AfxGetInstanceHandle(),hRes); HGLOBAL hGlob=LoadResource(AfxGetInstanceHandle(),hRes); LPBYTE pBytes=(LPBYTE)LockResource(hGlob); m_hCursor=(HCURSOR)CreateIconFromResource(pBytes,dwSize,FALSE,0x00030000);
  • /
HRSRC hRes=FindResource(hInstance,MAKEINTRESOURCE(nResID),"ANICURSORS"); DWORD dwSize=SizeofResource(hInstance,hRes); HGLOBAL hGlob=LoadResource(hInstance,hRes); LPBYTE pBytes=(LPBYTE)LockResource(hGlob); m_hCursor=(HCURSOR)CreateIconFromResource(pBytes,dwSize,FALSE,0x00030000); if(m_hCursor == NULL) LoadCursorDefault(); return m_hCursor; } CAniCur::~CAniCur() { //if allocated, fee memory if(m_hCursor) DestroyCursor(m_hCursor); }

Conclusion :


#######################################

Dans le programme voici un exemple d'utilisation :

// Global Variables:

#define CURSEUR_ANIMATED 1
#define CURSEUR_STATIC 0
// 0 : Static , 1 : Animated
int ChoixCurseur = CURSEUR_ANIMATED;
CAniCur Cur;

// cursor handle (hCurs3)
HINSTANCE hinst = NULL; // handle to current instance

// Yin-shaped cursor AND mask

BYTE ANDmaskCursor[] =
{
0xFF, 0xFC, 0x3F, 0xFF, // line 1
0xFF, 0xC0, 0x1F, 0xFF, // line 2
0xFF, 0x00, 0x3F, 0xFF, // line 3
0xFE, 0x00, 0xFF, 0xFF, // line 4

0xF7, 0x01, 0xFF, 0xFF, // line 5
0xF0, 0x03, 0xFF, 0xFF, // line 6
0xF0, 0x03, 0xFF, 0xFF, // line 7
0xE0, 0x07, 0xFF, 0xFF, // line 8

0xC0, 0x07, 0xFF, 0xFF, // line 9
0xC0, 0x0F, 0xFF, 0xFF, // line 10
0x80, 0x0F, 0xFF, 0xFF, // line 11
0x80, 0x0F, 0xFF, 0xFF, // line 12

0x80, 0x07, 0xFF, 0xFF, // line 13
0x00, 0x07, 0xFF, 0xFF, // line 14
0x00, 0x03, 0xFF, 0xFF, // line 15
0x00, 0x00, 0xFF, 0xFF, // line 16

0x00, 0x00, 0x7F, 0xFF, // line 17
0x00, 0x00, 0x1F, 0xFF, // line 18
0x00, 0x00, 0x0F, 0xFF, // line 19
0x80, 0x00, 0x0F, 0xFF, // line 20

0x80, 0x00, 0x07, 0xFF, // line 21
0x80, 0x00, 0x07, 0xFF, // line 22
0xC0, 0x00, 0x07, 0xFF, // line 23
0xC0, 0x00, 0x0F, 0xFF, // line 24

0xE0, 0x00, 0x0F, 0xFF, // line 25
0xF0, 0x00, 0x1F, 0xFF, // line 26
0xF0, 0x00, 0x1F, 0xFF, // line 27
0xF8, 0x00, 0x3F, 0xFF, // line 28

0xFE, 0x00, 0x7F, 0xFF, // line 29
0xFF, 0x00, 0xFF, 0xFF, // line 30
0xFF, 0xC3, 0xFF, 0xFF, // line 31
0xFF, 0xFF, 0xFF, 0xFF // line 32
};

// Yin-shaped cursor XOR mask

BYTE XORmaskCursor[] =
{
0x00, 0x00, 0x00, 0x00, // line 1
0x00, 0x03, 0xC0, 0x00, // line 2
0x00, 0x3F, 0x00, 0x00, // line 3
0x00, 0xFE, 0x00, 0x00, // line 4

0x0E, 0xFC, 0x00, 0x00, // line 5
0x07, 0xF8, 0x00, 0x00, // line 6
0x07, 0xF8, 0x00, 0x00, // line 7
0x0F, 0xF0, 0x00, 0x00, // line 8

0x1F, 0xF0, 0x00, 0x00, // line 9
0x1F, 0xE0, 0x00, 0x00, // line 10
0x3F, 0xE0, 0x00, 0x00, // line 11
0x3F, 0xE0, 0x00, 0x00, // line 12

0x3F, 0xF0, 0x00, 0x00, // line 13
0x7F, 0xF0, 0x00, 0x00, // line 14
0x7F, 0xF8, 0x00, 0x00, // line 15
0x7F, 0xFC, 0x00, 0x00, // line 16

0x7F, 0xFF, 0x00, 0x00, // line 17
0x7F, 0xFF, 0x80, 0x00, // line 18
0x7F, 0xFF, 0xE0, 0x00, // line 19
0x3F, 0xFF, 0xE0, 0x00, // line 20

0x3F, 0xC7, 0xF0, 0x00, // line 21
0x3F, 0x83, 0xF0, 0x00, // line 22
0x1F, 0x83, 0xF0, 0x00, // line 23
0x1F, 0x83, 0xE0, 0x00, // line 24

0x0F, 0xC7, 0xE0, 0x00, // line 25
0x07, 0xFF, 0xC0, 0x00, // line 26
0x07, 0xFF, 0xC0, 0x00, // line 27
0x01, 0xFF, 0x80, 0x00, // line 28

0x00, 0xFF, 0x00, 0x00, // line 29
0x00, 0x3C, 0x00, 0x00, // line 30
0x00, 0x00, 0x00, 0x00, // line 31
0x00, 0x00, 0x00, 0x00 // line 32
};

// Create a custom cursor at run time.

HCURSOR hCurs3 = CreateCursor( hinst, // app. instance
19, // horizontal position of hot spot
2, // vertical position of hot spot
32, // cursor width
32, // cursor height
ANDmaskCursor, // AND mask
XORmaskCursor ); // XOR mask

HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);

// FUNCTION PROTOTYPES

int DoWMCommand(WORD wParam, HWND hWnd);
void DoLButtonDown(HWND hWnd, LPARAM lParam);
void DoLButtonUp(HWND hWnd, LPARAM lParam);
void DoMouseMove(HWND hWnd, LPARAM lParam);
void DoPaint(HWND hWnd);

// GLOBAL VARIABLES

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32DRAW, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

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

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WIN32DRAW);

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

return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
//::Cur.SetDefaultCursor();

WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WIN32DRAW);

if(::ChoixCurseur == CURSEUR_ANIMATED)
wcex.hCursor = ::Cur.LoadAnimatedCursor(hInstance,IDC_blocnote);
else
wcex.hCursor = ::Cur.LoadCursor(hInstance,IDC_3Darrow);

//wcex.hCursor = ::Cur.LoadCursorEx("AniCurSample.exe",IDC_3Darrow);
//wcex.hCursor = ::Cur.LoadAnimatedCursorEx("AniCurSample.exe",IDC_blocnote);//Default();//hCurs3;

wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCTSTR)IDC_WIN32DRAW;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_WIN32DRAW_SMALL);

return RegisterClassEx(&wcex);
}
//-------------------------
// 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;

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

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

if(!hWnd)
{
return FALSE;
}

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

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)
{
int wmId, wmEvent;
//PAINTSTRUCT ps;
//HDC hdc;

switch(message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch(wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;

case IDM_EXIT:
DestroyWindow(hWnd);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;

case WM_LBUTTONDOWN:
DoLButtonDown(hWnd,lParam);
break;

case WM_LBUTTONUP:
DoLButtonUp(hWnd,lParam);
break;

case WM_MOUSEMOVE:
DoMouseMove(hWnd,lParam);
break;

case WM_PAINT:
//hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
DoPaint(hWnd);
//EndPaint(hWnd, &ps);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
Ajouter un commentaire Commentaire
Messages postés
933
Date d'inscription
dimanche 1 avril 2001
Statut
Modérateur
Dernière intervention
9 janvier 2012
2
Un petit ZIP serai bien ne crois-tu pas ?

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.