martes, 19 de agosto de 2008

Lupa

CHenry [chenry@lab.matcom.uh.cu]

Windows tiene un programa que hace algo similar, no obstante, nunca está demás ver una réplica de dicha utilidad, más cuando esta ha sido creada por un cubano, miembro de BlackHat y muchísimo más si pone a disposición de todos el código fuente.

/* Escrito por chenry para la comunidad de BH
Compilado con VS2003
*/
#include "stdafx.h"
#include "lupa.h"
#define MAX_LOADSTRING 100

HINSTANCE hInst;
TCHAR szTitle[MAX_LOADSTRING] = "Lupa by chenry for BH";
TCHAR szWindowClass[MAX_LOADSTRING] = "lupa";

ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
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)
{
  MSG msg;
  HACCEL hAccelTable;

   MyRegisterClass(hInstance);

  if (!InitInstance (hInstance, nCmdShow))
  {
    return FALSE;
  }

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

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

  return (int) msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
  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_LUPA);
  wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  wcex.lpszMenuName = (LPCTSTR)IDC_LUPA;
  wcex.lpszClassName = szWindowClass;
  wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

  return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
  HWND hWnd;

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

  hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_VSCROLL,
50,50,200,200, NULL, NULL, hInstance, NULL);

  if (!hWnd)
  {
    return FALSE;
  }

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

  return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  int wmId, wmEvent;
  PAINTSTRUCT ps;

  HDC hdc,hdcDesktop;
  static RECT paintRect;

  RECT clientRect;
  POINT punto;

  static bool isPainting;
  SCROLLINFO scroll;

  static int zoom;
  static bool onTimer=false;

  switch (message)
  {
    case WM_VSCROLL:
    {
      GetClientRect(hWnd,&clientRect);
      switch (LOWORD(wParam))
      {
        case SB_LINEUP:
        {
          if(zoom>1&&zoom<6)
          {
            zoom = zoom - 1;
            SetScrollPos(hWnd,SB_VERT,(zoom -1) * clientRect.bottom/5,TRUE);
          }
          return 0;
        }
        case SB_LINEDOWN:
        {
          if(zoom>0&&zoom<5)
          {
            zoom = zoom + 1;
            SetScrollPos(hWnd,SB_VERT,(zoom -1) * clientRect.bottom/5,TRUE);
          }
          return 0;
        }
      }

      return 0;
    }
    case WM_SIZE:
    {
      GetClientRect(hWnd,&clientRect);
      scroll.cbSize = sizeof(SCROLLINFO);
      scroll.fMask = SIF_ALL;
      scroll.nMin = 0;
      scroll.nMax = clientRect.bottom;
      scroll.nPage = clientRect.bottom/5;
      scroll.nPos = (zoom -1) * clientRect.bottom/5;
      SetScrollInfo(hWnd,SB_VERT,&scroll,TRUE);
      return 0;
    }
    case WM_LBUTTONDOWN:
    {
      hdcDesktop = GetDC(NULL);
      SetROP2(hdcDesktop, R2_NOT);

      Rectangle(hdcDesktop,paintRect.left,paintRect.top,paintRect.right,paintRect.bottom);
      DeleteDC(hdcDesktop);

      SetCapture(hWnd);
      isPainting = true;
      onTimer=false;

      return 0;
    }
    case WM_LBUTTONUP:
    {
      hdcDesktop = GetDC(NULL);
      SetROP2(hdcDesktop, R2_NOT);
      Rectangle(hdcDesktop,paintRect.left,paintRect.top,paintRect.right,paintRect.bottom);
      DeleteDC(hdcDesktop);

      InvalidateRect(hWnd,NULL,true);

      ReleaseCapture();
      isPainting = false;
      onTimer=true;

      return 0;
    }

    case WM_MOUSEMOVE:
    {
      if(isPainting)
      {
        GetCursorPos(&punto);

        hdcDesktop = GetDC(NULL);
        SetROP2(hdcDesktop, R2_NOT);
        Rectangle(hdcDesktop,paintRect.left,paintRect.top,paintRect.right,paintRect.bottom);
        DeleteDC(hdcDesktop);

        GetClientRect(hWnd,&clientRect);

        paintRect.left = punto.x - (clientRect.right - clientRect.left)/(2*zoom);
        paintRect.right = punto.x + (clientRect.right - clientRect.left)/(2*zoom);
        paintRect.top = punto.y - (clientRect.bottom - clientRect.top)/(2*zoom);
        paintRect.bottom = punto.y + (clientRect.bottom - clientRect.top)/(2*zoom);

        hdcDesktop = GetDC(NULL);
        SetROP2(hdcDesktop, R2_NOT);

        Rectangle(hdcDesktop,paintRect.left,paintRect.top,paintRect.right,paintRect.bottom);
        DeleteDC(hdcDesktop);

        InvalidateRect(hWnd,NULL,true);
      }
      return 0;
    }
    case WM_CREATE:
    {
      zoom = 5;
      GetClientRect(hWnd,&clientRect);
      scroll.cbSize = sizeof(SCROLLINFO);
      scroll.fMask = SIF_ALL;
      scroll.nMin = 0;
      scroll.nMax = clientRect.bottom;
      scroll.nPage = clientRect.bottom/5;
      scroll.nPos = (zoom -1) * clientRect.bottom/5;;
      SetScrollInfo(hWnd,SB_VERT,&scroll,TRUE);

      isPainting = false;
      SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
      SetTimer(hWnd,1,100,NULL);
      return 0;
    }
    case WM_TIMER:
    {
      if(onTimer)
      {
        hdc = GetDC(hWnd);

        hdcDesktop = GetDC(NULL);
        GetClientRect(hWnd,&clientRect);
        if(isPainting)
        StretchBlt(hdc,clientRect.left,clientRect.top,(clientRect.right - clientRect.left),(clientRect.bottom - clientRect.top),hdcDesktop,paintRect.left,paintRect.top,(paintRect.right - paintRect.left),(paintRect.bottom - paintRect.top),SRCINVERT);
        else
StretchBlt(hdc,clientRect.left,clientRect.top,(clientRect.right - clientRect.left),(clientRect.bottom - clientRect.top),hdcDesktop,paintRect.left,paintRect.top,(paintRect.right - paintRect.left),(paintRect.bottom - paintRect.top),SRCCOPY);

        DeleteDC(hdcDesktop);

        ReleaseDC(hWnd,hdc);

      }
      return 0;
    }
    case WM_COMMAND:
    {
      wmId = LOWORD(wParam);
      wmEvent = HIWORD(wParam);

// Parse the menu selections:
      switch (wmId)
      {
        case IDM_EXIT:
        DestroyWindow(hWnd);
        return 0;
        default:
        return DefWindowProc(hWnd, message, wParam, lParam);
      }
      return 0;
    }
    case WM_PAINT:
    {
      hdc = BeginPaint(hWnd, &ps);

      hdcDesktop = GetDC(NULL);
      GetClientRect(hWnd,&clientRect);
      if(isPainting)
      StretchBlt(hdc,clientRect.left,clientRect.top,(clientRect.right - clientRect.left),(clientRect.bottom - clientRect.top),hdcDesktop,paintRect.left,paintRect.top,(paintRect.right - paintRect.left),(paintRect.bottom - paintRect.top),SRCINVERT);
      else
      StretchBlt(hdc,clientRect.left,clientRect.top,(clientRect.right - clientRect.left),(clientRect.bottom - clientRect.top),hdcDesktop,paintRect.left,paintRect.top,(paintRect.right - paintRect.left),(paintRect.bottom - paintRect.top),SRCCOPY);

      DeleteDC(hdcDesktop);

      EndPaint(hWnd, &ps);
      return 0;
    }
    case WM_DESTROY:
    {
      PostQuitMessage(0);
      return 0;
    }
    default:
    return DefWindowProc(hWnd, message, wParam, lParam);
  }
  return 0;
}

void DrawBoxOutline (HWND hwnd, RECT rect)
{
  HDC hdc ;
  hdc = GetDC (NULL) ;
  SetROP2 (hdc, R2_NOT) ;
  SelectObject (hdc, GetStockObject (NULL_BRUSH)) ;
  Rectangle (hdc, rect.left, rect.top, rect.right, rect.bottom) ;
  ReleaseDC (hwnd, hdc) ;
}

Archivos relacionados

Lupa.zip


Artículos relacionados


No hay comentarios: