Accueil > > > LOUPE SIMPLE (WIN32)
LOUPE SIMPLE (WIN32)
Information sur la source
Description
Suite à une question du forum j'ai fait cette petite loupe qui affiche la zone sous la souris avec un facteur 2. Un timer raffraichit la loupe toutes les 50ms.
Source
- //***************************************************************************************
- // Loupe.cpp :
- //
- //***************************************************************************************
-
- #include <windows.h>
-
- //=======================================================================================
- // Variables globales.
- //=======================================================================================
- HINSTANCE g_hAppInstance = NULL; // instance de l'application
- HWND g_hWndMainFrame = NULL; // fenêtre principale de l'application
-
- //=======================================================================================
- // Fonctions du module.
- //=======================================================================================
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
- LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
-
-
- //***************************************************************************************
- // WinMain :
- //***************************************************************************************
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
- int nCmdShow)
- {
- // initialisation de l'application
- if(!InitInstance(hInstance, nCmdShow))
- return 0;
-
- // boucle de messages
- MSG msg;
- while(GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
-
- //***************************************************************************************
- // InitInstance :
- //***************************************************************************************
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- // sauvegarde instance de l'application
- g_hAppInstance = hInstance;
- char szWndClass[] = "LoupeWnd";
- char szWndTitle[] = "Loupe";
-
- // initialisation classe de fenêtre
- WNDCLASS wc;
- ZeroMemory(&wc, sizeof(WNDCLASS));
- wc.hInstance = g_hAppInstance;
- wc.lpfnWndProc = WndProc;
- wc.lpszClassName = szWndClass;
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.style = CS_HREDRAW|CS_VREDRAW;
- if(!RegisterClass(&wc))
- return FALSE;
-
- // création de la fenêtre principale
- g_hWndMainFrame = CreateWindowEx(WS_EX_TOPMOST, szWndClass, szWndTitle,
- WS_OVERLAPPED|WS_CAPTION|WS_THICKFRAME|WS_SYSMENU|WS_MINIMIZEBOX,
- 0, 0, 200, 200, NULL, NULL, g_hAppInstance, NULL);
- if(g_hWndMainFrame == NULL)
- return FALSE;
-
- // affichage et mise à jour
- ShowWindow(g_hWndMainFrame, nCmdShow);
- UpdateWindow(g_hWndMainFrame);
- return TRUE;
- }
-
- //***************************************************************************************
- // WndProc :
- //***************************************************************************************
- LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- // en fonction du message
- switch(uMsg)
- {
- case WM_CREATE :
- {
- // création timer
- SetTimer(hWnd, 0x100, 50, NULL);
- return 0;
- }
- case WM_DESTROY :
- {
- // suppression du timer, fin de l'application
- KillTimer(hWnd, 0x100);
- PostQuitMessage(0);
- return 0;
- }
- case WM_TIMER :
- {
- // mise à jour de la fenêtre
- InvalidateRect(hWnd, NULL, FALSE);
- UpdateWindow(hWnd);
- return 0;
- }
- case WM_PAINT :
- {
- // début du dessin
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hWnd, &ps);
-
- // taille zone cliente
- RECT rcClient;
- GetClientRect(hWnd, &rcClient);
- SIZE sizeDst;
- sizeDst.cx = rcClient.right-rcClient.left;
- sizeDst.cy = rcClient.bottom-rcClient.top;
-
- // création DC et bitmap en mémoire
- HDC hMemDC = CreateCompatibleDC(hdc);
- HBITMAP hMemBmp = CreateCompatibleBitmap(hdc, sizeDst.cx, sizeDst.cy);
- HBITMAP hOldMemBmp = (HBITMAP) SelectObject(hMemDC, hMemBmp);
-
- // effacement du fond en blanc
- HBRUSH hBrush = CreateSolidBrush(RGB(0xFF, 0xFF, 0xFF));
- FillRect(hMemDC, &rcClient, hBrush);
- DeleteObject(hBrush);
-
- // position de la souris et de la fenêtre
- POINT ptMouse;
- GetCursorPos(&ptMouse);
- RECT rcWnd;
- GetWindowRect(hWnd, &rcWnd);
-
- // faire un zoom de la zone sous la souris
- if(!PtInRect(&rcWnd, ptMouse))
- {
- // zone source
- SIZE sizeSrc;
- sizeSrc.cx = sizeDst.cx/2;
- sizeSrc.cy = sizeDst.cy/2;
- RECT rcSrc;
- rcSrc.left = ptMouse.x-sizeSrc.cx/2;
- rcSrc.top = ptMouse.y-sizeSrc.cy/2;
- rcSrc.right = rcSrc.left+sizeSrc.cx;
- rcSrc.bottom = rcSrc.top+sizeSrc.cy;
-
- // récupération DC de l'écran et recopie
- HDC hScreenDC = GetDC(NULL);
- StretchBlt(hMemDC, 0, 0, sizeDst.cx, sizeDst.cy, hScreenDC,
- rcSrc.left, rcSrc.top, sizeSrc.cx, sizeSrc.cy, SRCCOPY);
- ReleaseDC(NULL, hScreenDC);
- }
-
- // recopie du DC en mémoire, libération des ressources
- BitBlt(hdc, 0, 0, sizeDst.cx, sizeDst.cy, hMemDC, 0, 0, SRCCOPY);
- SelectObject(hMemDC, hOldMemBmp);
- DeleteObject(hMemBmp);
- DeleteDC(hMemDC);
-
- // fin du dessin
- EndPaint(hWnd, &ps);
- return 0;
- }
- }
-
- // traitement par défaut
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
//***************************************************************************************
// Loupe.cpp :
//
//***************************************************************************************
#include <windows.h>
//=======================================================================================
// Variables globales.
//=======================================================================================
HINSTANCE g_hAppInstance = NULL; // instance de l'application
HWND g_hWndMainFrame = NULL; // fenêtre principale de l'application
//=======================================================================================
// Fonctions du module.
//=======================================================================================
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
//***************************************************************************************
// WinMain :
//***************************************************************************************
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nCmdShow)
{
// initialisation de l'application
if(!InitInstance(hInstance, nCmdShow))
return 0;
// boucle de messages
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//***************************************************************************************
// InitInstance :
//***************************************************************************************
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
// sauvegarde instance de l'application
g_hAppInstance = hInstance;
char szWndClass[] = "LoupeWnd";
char szWndTitle[] = "Loupe";
// initialisation classe de fenêtre
WNDCLASS wc;
ZeroMemory(&wc, sizeof(WNDCLASS));
wc.hInstance = g_hAppInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = szWndClass;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.style = CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wc))
return FALSE;
// création de la fenêtre principale
g_hWndMainFrame = CreateWindowEx(WS_EX_TOPMOST, szWndClass, szWndTitle,
WS_OVERLAPPED|WS_CAPTION|WS_THICKFRAME|WS_SYSMENU|WS_MINIMIZEBOX,
0, 0, 200, 200, NULL, NULL, g_hAppInstance, NULL);
if(g_hWndMainFrame == NULL)
return FALSE;
// affichage et mise à jour
ShowWindow(g_hWndMainFrame, nCmdShow);
UpdateWindow(g_hWndMainFrame);
return TRUE;
}
//***************************************************************************************
// WndProc :
//***************************************************************************************
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// en fonction du message
switch(uMsg)
{
case WM_CREATE :
{
// création timer
SetTimer(hWnd, 0x100, 50, NULL);
return 0;
}
case WM_DESTROY :
{
// suppression du timer, fin de l'application
KillTimer(hWnd, 0x100);
PostQuitMessage(0);
return 0;
}
case WM_TIMER :
{
// mise à jour de la fenêtre
InvalidateRect(hWnd, NULL, FALSE);
UpdateWindow(hWnd);
return 0;
}
case WM_PAINT :
{
// début du dessin
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// taille zone cliente
RECT rcClient;
GetClientRect(hWnd, &rcClient);
SIZE sizeDst;
sizeDst.cx = rcClient.right-rcClient.left;
sizeDst.cy = rcClient.bottom-rcClient.top;
// création DC et bitmap en mémoire
HDC hMemDC = CreateCompatibleDC(hdc);
HBITMAP hMemBmp = CreateCompatibleBitmap(hdc, sizeDst.cx, sizeDst.cy);
HBITMAP hOldMemBmp = (HBITMAP) SelectObject(hMemDC, hMemBmp);
// effacement du fond en blanc
HBRUSH hBrush = CreateSolidBrush(RGB(0xFF, 0xFF, 0xFF));
FillRect(hMemDC, &rcClient, hBrush);
DeleteObject(hBrush);
// position de la souris et de la fenêtre
POINT ptMouse;
GetCursorPos(&ptMouse);
RECT rcWnd;
GetWindowRect(hWnd, &rcWnd);
// faire un zoom de la zone sous la souris
if(!PtInRect(&rcWnd, ptMouse))
{
// zone source
SIZE sizeSrc;
sizeSrc.cx = sizeDst.cx/2;
sizeSrc.cy = sizeDst.cy/2;
RECT rcSrc;
rcSrc.left = ptMouse.x-sizeSrc.cx/2;
rcSrc.top = ptMouse.y-sizeSrc.cy/2;
rcSrc.right = rcSrc.left+sizeSrc.cx;
rcSrc.bottom = rcSrc.top+sizeSrc.cy;
// récupération DC de l'écran et recopie
HDC hScreenDC = GetDC(NULL);
StretchBlt(hMemDC, 0, 0, sizeDst.cx, sizeDst.cy, hScreenDC,
rcSrc.left, rcSrc.top, sizeSrc.cx, sizeSrc.cy, SRCCOPY);
ReleaseDC(NULL, hScreenDC);
}
// recopie du DC en mémoire, libération des ressources
BitBlt(hdc, 0, 0, sizeDst.cx, sizeDst.cy, hMemDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, hOldMemBmp);
DeleteObject(hMemBmp);
DeleteDC(hMemDC);
// fin du dessin
EndPaint(hWnd, &ps);
return 0;
}
}
// traitement par défaut
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
MATRICE TEMPLATEMATRICE TEMPLATE par hjr2610
Cliquez pour lire la suite par hjr2610 RE : SAC A DOS RE : SAC A DOS par hadjkaddour
Cliquez pour lire la suite par hadjkaddour
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|