home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / getbitmp.zip / GETBITMP.C < prev    next >
Text File  |  1988-01-05  |  16KB  |  476 lines

  1. /*  Getbitmp.c
  2.     Les consultants génicom
  3.     Copyright (c) Les consultants génicom 1987 1988
  4.     Programmé Par Bernard de Champlain              */
  5.  
  6. #include "windows.h"
  7. #include "winexp.h"
  8. #include "io.h"
  9. #include "getbitmp.h"
  10.  
  11. HANDLE hInst;
  12. HBITMAP hBitmap = NULL;
  13. BOOL AutoAbort = TRUE, SaveEnable = FALSE;
  14. FARPROC lpprocAbout, lpprocSize, lpprocSave;
  15. short Horizontal, Vertical, BltMode;
  16.  
  17. char szAppName[20];
  18. char szAbout[20];
  19. char szWindowTitle[40];
  20. char szCliplock[40];
  21. char szNotBit[40];
  22. char szClipClose[40];
  23. char szClipMemory[40];
  24.  
  25. long FAR PASCAL GetbitmpWndProc(HWND, unsigned, WORD, LONG);
  26.  
  27. BOOL FAR PASCAL GetbitmpAbout(hDlg, message, wParam, lParam)
  28. HWND hDlg;
  29. unsigned message;
  30. WORD wParam;
  31. LONG lParam;
  32. {
  33.     switch (message)
  34.     {
  35.     case WM_LBUTTONDOWN:
  36.     case WM_LBUTTONUP:
  37.     case WM_MBUTTONDOWN:
  38.     case WM_MBUTTONUP:
  39.     case WM_RBUTTONDOWN:
  40.     case WM_RBUTTONUP:
  41.     case WM_KILLFOCUS:
  42.     case WM_KEYDOWN:
  43.         if (!AutoAbort)
  44.             break;
  45.  
  46.     case WM_COMMAND:
  47.         if (AutoAbort)
  48.             ReleaseCapture();
  49.         EndDialog(hDlg, TRUE);
  50.         return(TRUE);
  51.         break;
  52.  
  53.     case WM_INITDIALOG:
  54.         if (AutoAbort)
  55.             SetCapture(hDlg);
  56.         return(TRUE);
  57.         break;
  58.     }
  59.     return(FALSE);
  60. }
  61.  
  62. BOOL FAR PASCAL GetbitmpSize(hDlg, message, wParam, lParam)
  63. HWND hDlg;
  64. unsigned message;
  65. WORD wParam;
  66. LONG lParam;
  67. {
  68.     unsigned char Buf[10];
  69.     int sprintf(unsigned char *, unsigned char *, );
  70.     short atoi(unsigned char *);
  71.  
  72.     switch (message)
  73.     {
  74.     case WM_INITDIALOG:
  75.         sprintf(Buf, "%4d", Horizontal);
  76.         SetDlgItemText(hDlg, IDHORIZ, (LPSTR)Buf);
  77.         sprintf(Buf, "%4d", Vertical);
  78.         SetDlgItemText(hDlg, IDVERTI, (LPSTR)Buf);
  79.         EnableWindow(GetDlgItem(hDlg, IDOK), TRUE);
  80.         if (BltMode == WHITEONBLACK)
  81.             CheckRadioButton(hDlg, IDOR, IDCUT, IDOR);
  82.         else
  83.             if (BltMode == BLACKONWHITE)
  84.                 CheckRadioButton(hDlg, IDOR, IDCUT, IDAND);
  85.             else
  86.                 CheckRadioButton(hDlg, IDOR, IDCUT, IDCUT);
  87.         break;
  88.  
  89.     case WM_COMMAND:
  90.         switch (wParam)
  91.         {
  92.         case IDOK:
  93.             if (IsWindowEnabled(GetDlgItem(hDlg, IDOK))) {
  94.                 GetDlgItemText(hDlg, (unsigned)IDHORIZ, (LPSTR)Buf, 5);
  95.                 Horizontal = atoi(Buf);
  96.                 GetDlgItemText(hDlg, (unsigned)IDVERTI, (LPSTR)Buf, 5);
  97.                 Vertical = atoi(Buf);
  98.                 if (IsDlgButtonChecked(hDlg, IDOR))
  99.                     BltMode = WHITEONBLACK;
  100.                 else
  101.                     if (IsDlgButtonChecked(hDlg, IDAND))
  102.                         BltMode = BLACKONWHITE;
  103.                     else
  104.                         BltMode = COLORONCOLOR;
  105.                 EndDialog(hDlg, TRUE);
  106.             }
  107.             break;
  108.  
  109.         case IDCANCEL:
  110.             EndDialog(hDlg, FALSE);
  111.             break;
  112.  
  113.         case IDHORIZ:
  114.         case IDVERTI:
  115.             if (HIWORD(lParam) == EN_KILLFOCUS) {
  116.                 GetDlgItemText(hDlg, (unsigned)wParam, (LPSTR)Buf, 5);
  117.                 sprintf(Buf, "%4d", atoi(Buf));
  118.                 SetDlgItemText(hDlg, (unsigned)wParam, (LPSTR)Buf);
  119.             }
  120.             if (HIWORD(lParam) == EN_CHANGE) {
  121.                 GetDlgItemText(hDlg, IDHORIZ, (LPSTR)Buf, 5);
  122.                 GetDlgItemText(hDlg, IDVERTI, (LPSTR)&Buf[5], 5);
  123.                 EnableWindow(GetDlgItem(hDlg, IDOK),
  124.                       (BOOL)(atoi(Buf) && atoi(&Buf[5])));
  125.             }
  126.             break;
  127.  
  128.         case IDOR:
  129.         case IDAND:
  130.         case IDCUT:
  131.             CheckRadioButton(hDlg, IDOR, IDCUT, (int)wParam);
  132.             break;
  133.  
  134.         default:
  135.             return(FALSE);
  136.         }
  137.         break;
  138.  
  139.     default:
  140.         return(FALSE);
  141.     }
  142.     return(TRUE);
  143. }
  144.  
  145. BOOL FAR PASCAL GetbitmpSave(hDlg, message, wParam, lParam)
  146. HWND hDlg;
  147. unsigned message;
  148. WORD wParam;
  149. LONG lParam;
  150. {
  151.     char *ResultBuf, *Scan;
  152.     int len;
  153.  
  154.     switch (message)
  155.     {
  156.     case WM_INITDIALOG:
  157.         /* Initialize Path field with current directory */
  158.         DlgDirList(hDlg, (LPSTR)0, 0, IDPATH, 0);
  159.         EnableWindow(GetDlgItem(hDlg, IDOK), FALSE);
  160.         break;
  161.  
  162.     case WM_COMMAND:
  163.         switch (wParam)
  164.         {
  165.         case IDOK:
  166.             if (IsWindowEnabled(GetDlgItem(hDlg, IDOK))) {
  167.                 len = 4+GetWindowTextLength(GetDlgItem(hDlg, IDEDIT));
  168.                 if (ResultBuf = (char *)LocalAlloc(LPTR, (WORD)++len)) {
  169.                     GetDlgItemText(hDlg, IDEDIT, (LPSTR)ResultBuf, len);
  170.                     for (Scan = ResultBuf; *Scan && *Scan != '.'; Scan++);
  171.                     if (!*Scan)
  172.                         lstrcpy((LPSTR)Scan, (LPSTR)".BMP");
  173.                     AnsiUpper((LPSTR)ResultBuf);
  174.                 }
  175.                 EndDialog(hDlg, (int)ResultBuf);
  176.             }
  177.             break;
  178.  
  179.         case IDCANCEL:
  180.             EndDialog(hDlg, NULL);
  181.             break;
  182.  
  183.         case IDEDIT:
  184.             if (HIWORD(lParam) == EN_CHANGE)
  185.                 EnableWindow(GetDlgItem(hDlg, IDOK), (BOOL)(SendMessage(
  186.                     GetDlgItem(hDlg, IDEDIT), WM_GETTEXTLENGTH, 0, 0L)));
  187.             break;
  188.  
  189.         default:
  190.             return(FALSE);
  191.         }
  192.         break;
  193.  
  194.     default:
  195.         return(FALSE);
  196.     }
  197.     return(TRUE);
  198. }
  199.  
  200. BOOL GetClip(hWnd)
  201. HWND hWnd;
  202. {
  203.     BOOL Retry = FALSE;
  204.     HBITMAP hClipBits;
  205.     BITMAP ClipBits;
  206.     HANDLE hBits;
  207.     LPSTR lpBits;
  208.  
  209.     if (OpenClipboard(hWnd)) {
  210.         if (hClipBits = (HBITMAP)GetClipboardData(CF_BITMAP)) {
  211.             GetObject((HANDLE)hClipBits, sizeof(BITMAP), (LPSTR)&ClipBits);
  212.             if (hBitmap)
  213.                 DeleteObject((HANDLE)hBitmap);
  214.             if ((hBitmap = CreateBitmapIndirect((BITMAP FAR *)&ClipBits)) &&
  215.                 (hBits = GlobalAlloc(GHND,
  216.                    (DWORD)(ClipBits.bmWidthBytes * ClipBits.bmHeight)))) {
  217.                 lpBits = GlobalLock(hBits);
  218.                 GetBitmapBits(hClipBits,
  219.                     (long)ClipBits.bmWidthBytes * ClipBits.bmHeight, lpBits);
  220.                 SetBitmapBits(hBitmap,
  221.                    (DWORD)ClipBits.bmWidthBytes * ClipBits.bmHeight, lpBits);
  222.                 GlobalUnlock(hBits);
  223.                 GlobalFree(hBits);
  224.                 Horizontal = ClipBits.bmWidth;
  225.                 Vertical = ClipBits.bmHeight;
  226.                 BltMode = BLACKONWHITE;
  227.                 if (!SaveEnable) {
  228.                     SaveEnable = TRUE;
  229.                     EnableMenuItem(GetMenu(hWnd), IDSIZE, MF_ENABLED);
  230.                     EnableMenuItem(GetMenu(hWnd), IDSAVE, MF_ENABLED);
  231.                     DrawMenuBar(hWnd);
  232.                 }
  233.             }
  234.             else {
  235.                 Retry = (BOOL)(MessageBox(hWnd, (LPSTR)szClipMemory, (LPSTR)NULL,
  236.                                       MB_RETRYCANCEL | MB_ICONHAND) == IDRETRY);
  237.                 if (SaveEnable) {
  238.                     SaveEnable = FALSE;
  239.                     EnableMenuItem(GetMenu(hWnd), IDSIZE, MF_GRAYED);
  240.                     EnableMenuItem(GetMenu(hWnd), IDSAVE, MF_GRAYED);
  241.                     DrawMenuBar(hWnd);
  242.                 }
  243.             }
  244.         }
  245.         else
  246.             Retry = (BOOL)(MessageBox(hWnd, (LPSTR)szNotBit, (LPSTR)NULL,
  247.                                       MB_RETRYCANCEL | MB_ICONHAND) == IDRETRY);
  248.         if (!CloseClipboard())
  249.             Retry = (BOOL)(MessageBox(hWnd, (LPSTR)szClipClose, (LPSTR)NULL,
  250.                                       MB_RETRYCANCEL | MB_ICONHAND) == IDRETRY);
  251.     }
  252.     else
  253.         return((BOOL)(MessageBox(hWnd, (LPSTR)szCliplock, (LPSTR)NULL,
  254.                MB_RETRYCANCEL | MB_ICONHAND) == IDRETRY));
  255.  
  256.     return(Retry);
  257. }
  258.  
  259. void GetbitmpCommand(hWnd, id)
  260. HWND hWnd;
  261. WORD id;
  262. {
  263.     switch (id)
  264.     {
  265.     case IDCAPT:
  266.         while(GetClip(hWnd));
  267.         break;
  268.  
  269.     case IDSIZE:
  270.         DialogBox(hInst, MAKEINTRESOURCE(SIZEBOX), hWnd, lpprocSize);
  271.         break;
  272.  
  273.     case IDSAVE: {
  274.             char *FileName;
  275.             OFSTRUCT ofStruct;
  276.             int hFile;
  277.             HDC hDC, hSrcDC, hDstDC;
  278.             BOOL Abort = FALSE;
  279.             BITMAP Bitmap;
  280.             HBITMAP hBitmapDst = NULL, hBitmapSave = hBitmap;
  281.             HANDLE hBits;
  282.             LPSTR lpBits;
  283.  
  284.             if (FileName = (char *)DialogBox(hInst, MAKEINTRESOURCE(SAVEBOX),
  285.                                              hWnd, lpprocSave)) {
  286.                 if ((hFile = OpenFile((LPSTR)FileName, (LPOFSTRUCT)&ofStruct,
  287.                                              OF_WRITE | OF_CREATE)) != -1) {
  288.                     GetObject((HANDLE)hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
  289.                     if (Horizontal!=Bitmap.bmWidth || Vertical!=Bitmap.bmHeight) {
  290.                         hDC = GetDC(hWnd);
  291.                         hSrcDC = CreateCompatibleDC(hDC);
  292.                         hDstDC = CreateCompatibleDC(hDC);
  293.                         ReleaseDC(hWnd, hDC);
  294.  
  295.                         SelectObject(hSrcDC, (HANDLE)hBitmap);
  296.                         if (hBitmapSave = hBitmapDst =
  297.                             CreateCompatibleBitmap(hSrcDC, Horizontal, Vertical)) {
  298.                             SelectObject(hDstDC, (HANDLE)hBitmapDst);
  299.                             SetStretchBltMode(hDstDC, BltMode);
  300.                             StretchBlt(hDstDC, 0, 0, Horizontal, Vertical,
  301.                                 hSrcDC, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight,
  302.                                 SRCCOPY);
  303.                         }
  304.                         else {
  305.                             MessageBox(hWnd, (LPSTR)szClipMemory, (LPSTR)NULL,
  306.                                        MB_OK | MB_ICONHAND);
  307.                             Abort = TRUE;
  308.                         }
  309.                         DeleteDC(hDstDC);
  310.                         DeleteDC(hSrcDC);
  311.                     }
  312.                     if (!Abort) {
  313.                         GetObject((HANDLE)hBitmapSave, sizeof(BITMAP),
  314.                             (LPSTR)&Bitmap);
  315.                         if (hBits = GlobalAlloc(GHND,
  316.                            (DWORD)(Bitmap.bmWidthBytes * Bitmap.bmHeight))) {
  317.                             lpBits = GlobalLock(hBits);
  318.                             GetBitmapBits(hBitmapSave,
  319.                                (long)Bitmap.bmWidthBytes*Bitmap.bmHeight,lpBits);
  320.                             _lwrite(hFile, (LPSTR)"\002", 2);
  321.                             _lwrite(hFile, (LPSTR)&Bitmap, 14);
  322.                             _lwrite(hFile, lpBits, (int)(Bitmap.bmWidthBytes *
  323.                                                          Bitmap.bmHeight));
  324.                             _lclose(hFile);
  325.                             GlobalUnlock(hBits);
  326.                             GlobalFree(hBits);
  327.                         }
  328.                         else
  329.                             MessageBox(hWnd, (LPSTR)szClipMemory, (LPSTR)NULL,
  330.                                        MB_OK | MB_ICONHAND);
  331.                         if (hBitmapDst)
  332.                             DeleteObject((HANDLE)hBitmapDst);
  333.                     }
  334.                 }
  335.                 LocalFree((HANDLE)FileName);
  336.             }
  337.         break;
  338.         }
  339.     }
  340. }
  341.  
  342. BOOL GetbitmpInit(hInstance)
  343. HANDLE hInstance;
  344. {
  345.     PWNDCLASS   pGetbitmpClass;
  346.  
  347.     /* Loading from string table */
  348.     LoadString(hInstance, IDSNAME, (LPSTR)szAppName, 20);
  349.     LoadString(hInstance, IDSABOUT, (LPSTR)szAbout, 20);
  350.     LoadString(hInstance, IDSTITLE, (LPSTR)szWindowTitle, 40);
  351.     LoadString(hInstance, IDSCLIPBD, (LPSTR)szCliplock, 40);
  352.     LoadString(hInstance, IDSCLIPBM, (LPSTR)szNotBit, 40);
  353.     LoadString(hInstance, IDSCLIPCL, (LPSTR)szClipClose, 40);
  354.     LoadString(hInstance, IDSCLIPMM, (LPSTR)szClipMemory, 40);
  355.  
  356.     pGetbitmpClass = (PWNDCLASS)LocalAlloc(LPTR, sizeof(WNDCLASS));
  357.  
  358.     pGetbitmpClass->hCursor        = LoadCursor(NULL, IDC_ARROW);
  359.     pGetbitmpClass->hIcon          = LoadIcon(hInstance, (LPSTR)szAppName);
  360.     pGetbitmpClass->lpszMenuName   = (LPSTR)szAppName;
  361.     pGetbitmpClass->hInstance      = hInstance;
  362.     pGetbitmpClass->lpszClassName  = (LPSTR)szAppName;
  363.     pGetbitmpClass->hbrBackground  = (HBRUSH)COLOR_WINDOW+1;
  364.     pGetbitmpClass->style          = CS_HREDRAW | CS_VREDRAW;
  365.     pGetbitmpClass->lpfnWndProc    = GetbitmpWndProc;
  366.  
  367.     if (!RegisterClass((LPWNDCLASS)pGetbitmpClass))
  368.         return(FALSE);   /* Initialization failed */
  369.  
  370.     LocalFree((HANDLE)pGetbitmpClass);
  371.     return(TRUE);    /* Initialization succeeded */
  372. }
  373.  
  374. int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  375. HANDLE hInstance, hPrevInstance;
  376. LPSTR lpszCmdLine;
  377. int cmdShow;
  378. {
  379.     MSG   msg;
  380.     HWND  hWnd;
  381.     HMENU hMenu;
  382.  
  383.     if (!hPrevInstance) {
  384.         if (!GetbitmpInit(hInstance))
  385.             return(FALSE);
  386.         }
  387.     else {
  388.         /* Copy data from previous instance */
  389.         GetInstanceData(hPrevInstance, (PSTR)szAppName, 20);
  390.         GetInstanceData(hPrevInstance, (PSTR)szAbout, 20);
  391.         GetInstanceData(hPrevInstance, (PSTR)szWindowTitle, 40);
  392.         GetInstanceData(hPrevInstance, (PSTR)szCliplock, 40);
  393.         GetInstanceData(hPrevInstance, (PSTR)szNotBit, 40);
  394.         GetInstanceData(hPrevInstance, (PSTR)szClipClose, 40);
  395.         GetInstanceData(hPrevInstance, (PSTR)szClipMemory, 40);
  396.         }
  397.  
  398.     hWnd = CreateWindow((LPSTR)szAppName,
  399.                         (LPSTR)szWindowTitle,
  400.                         WS_TILEDWINDOW,
  401.                         0,    /*  x - ignored for tiled windows */
  402.                         0,    /*  y - ignored for tiled windows */
  403.                         0,    /* cx - ignored for tiled windows */
  404.                         0,    /* cy - ignored for tiled windows */
  405.                         (HWND)NULL,        /* no parent */
  406.                         (HMENU)NULL,       /* use class menu */
  407.                         (HANDLE)hInstance, /* handle to window instance */
  408.                         (LPSTR)NULL
  409.                        );
  410.  
  411.     hInst = hInstance;
  412.     lpprocAbout = MakeProcInstance((FARPROC)GetbitmpAbout, hInstance);
  413.     lpprocSize = MakeProcInstance((FARPROC)GetbitmpSize, hInstance);
  414.     lpprocSave = MakeProcInstance((FARPROC)GetbitmpSave, hInstance);
  415.  
  416.     hMenu = GetSystemMenu(hWnd, FALSE);
  417.     ChangeMenu(hMenu, 0, NULL, 999, MF_APPEND | MF_SEPARATOR);
  418.     ChangeMenu(hMenu, 0, (LPSTR)szAbout, IDSABOUT, MF_APPEND | MF_STRING);
  419.     ShowWindow(hWnd, cmdShow);
  420.     DialogBox(hInst, MAKEINTRESOURCE(ABOUTBOX), hWnd, lpprocAbout);
  421.     AutoAbort = FALSE;
  422.  
  423.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  424.         TranslateMessage((LPMSG)&msg);
  425.         DispatchMessage((LPMSG)&msg);
  426.     }
  427.  
  428.     return((int)msg.wParam);
  429. }
  430.  
  431. LONG FAR PASCAL GetbitmpWndProc(hWnd, message, wParam, lParam)
  432. HWND hWnd;
  433. unsigned message;
  434. WORD wParam;
  435. LONG lParam;
  436. {
  437.     switch (message)
  438.     {
  439.     case WM_SYSCOMMAND:
  440.         switch (wParam)
  441.         {
  442.         case IDSABOUT:
  443.             DialogBox(hInst, MAKEINTRESOURCE(ABOUTBOX), hWnd, lpprocAbout);
  444.             break;
  445.         default:
  446.             return DefWindowProc(hWnd, message, wParam, lParam);
  447.             break;
  448.         }
  449.         break;
  450.  
  451.     case WM_CREATE:
  452.         EnableMenuItem(GetMenu(hWnd), IDSIZE, MF_GRAYED);
  453.         EnableMenuItem(GetMenu(hWnd), IDSAVE, MF_GRAYED);
  454.         break;
  455.  
  456.     case WM_PAINT:
  457.         ValidateRect(hWnd, (LPRECT)NULL);
  458.         break;
  459.  
  460.     case WM_COMMAND:
  461.         GetbitmpCommand(hWnd, wParam);
  462.         break;
  463.  
  464.     case WM_DESTROY:
  465.         if (hBitmap)
  466.             DeleteObject((HANDLE)hBitmap);
  467.         PostQuitMessage(0);
  468.         break;
  469.  
  470.     default:
  471.         return DefWindowProc(hWnd, message, wParam, lParam);
  472.         break;
  473.     }
  474.     return(0L);
  475. }
  476.