home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 10.ddi / SHELLAPI.ZIP / SHELLAPI.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  5.5 KB  |  264 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2. //
  3. // shellapi.cpp
  4.  
  5. #include <owl.h>
  6. #include <shellapi.h>
  7. #include <stdio.h>
  8. #include <dir.h>
  9. #include <string.h>
  10.  
  11. class TMyApp : public TApplication
  12. {
  13.  
  14. public:
  15.     TMyApp(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  16.             LPSTR lpCmdLine, int nCmdShow)
  17.         : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  18.     {
  19.     };
  20.  
  21.   virtual void InitMainWindow();
  22.  
  23. };
  24.  
  25. _CLASSDEF(TDropTargetWin)
  26. class TDropTargetWin : public TWindow
  27. {
  28.  
  29. public:
  30.   TDropTargetWin(PTWindowsObject AParent, LPSTR ATitle)
  31.     : TWindow(AParent, ATitle)
  32.   {
  33.   };
  34.  
  35. protected:
  36.  
  37.   virtual void SetupWindow();
  38.  
  39.   virtual void WMDropFiles(RTMessage Msg) = [ WM_FIRST + WM_DROPFILES ];
  40.  
  41.     // Override this function in derived classes to change behavior:
  42.  
  43.   virtual void DropAFile(char *fileName, int x, int y );
  44.  
  45. };
  46.  
  47. _CLASSDEF(TIconWindow)
  48. class TIconWindow : public TWindow
  49. {
  50. public:
  51.     TIconWindow(PTWindowsObject AParent, LPSTR ATitle, int x, int y);
  52.     ~TIconWindow();
  53.  
  54. protected:
  55.     virtual void WMQueryDragIcon(RTMessage) = [ WM_FIRST + WM_QUERYDRAGICON ];
  56.     virtual void WMQueryOpen    (RTMessage) = [ WM_FIRST + WM_QUERYOPEN ];
  57.     virtual void WMSysCommand   (RTMessage) = [ WM_FIRST + WM_SYSCOMMAND ];
  58.  
  59.     virtual void  Paint( HDC, PAINTSTRUCT& );
  60.     virtual void  GetWindowClass(WNDCLASS _FAR &);
  61.     virtual LPSTR GetClassName();
  62.  
  63. private:
  64.     HICON   hIcon;
  65.     BOOL    bDefIcon;
  66.     LPSTR    pPath;
  67.     int     x,y;
  68.  
  69. };
  70.  
  71. //---------------------------------------------------------------
  72. // ---- Code starts here -----
  73. //---------------------------------------------------------------
  74.  
  75.  
  76. TIconWindow::TIconWindow(PTWindowsObject AParent, LPSTR ATitle, int _x, int _y)
  77.     : TWindow(AParent,"unknown")
  78. {
  79.  
  80.     Attr.Style |= (WS_MINIMIZE | WS_CHILD);
  81.  
  82.     // Set Title to file name
  83.  
  84.     char    fn[MAXFILE];
  85.     char    pTemp[255];
  86.  
  87.     _fstrcpy(pTemp,ATitle);
  88.  
  89.     *fn = 0;
  90.  
  91.     fnsplit( pTemp, NULL, NULL, fn, NULL );
  92.  
  93.     if( *fn )
  94.     {
  95.         delete Title;
  96.         Title = _fstrdup(fn);
  97.     }
  98.  
  99.     // Set pPath to full path for execution
  100.  
  101.     pPath = _fstrdup(ATitle);
  102.  
  103.  
  104.     // Get icon from executable
  105.  
  106.     HANDLE h = FindExecutable( pPath, ".\\", pTemp );
  107.  
  108.     if( WORD(h) <= 32 )
  109.         hIcon = 0;
  110.     else
  111.         hIcon = ExtractIcon(GetModule()->hInstance,pTemp,0);
  112.  
  113.     if( WORD(hIcon) <= 1 )
  114.     {
  115.         hIcon = LoadIcon( NULL, IDI_QUESTION );
  116.         bDefIcon = 1;
  117.     }
  118.     else
  119.     {
  120.         bDefIcon = 0;
  121.     }
  122.  
  123.     // Set x/y position of drop (in Parent coordinates - not used in this app)
  124.  
  125.     x = _x;
  126.     y = _y;
  127.  
  128. }
  129.  
  130. TIconWindow::~TIconWindow()
  131. {
  132.     if( bDefIcon )
  133.     {
  134.         // Don't call FreeResource() on the '?' icon
  135.  
  136.         FreeResource(hIcon);
  137.     }
  138.  
  139.     delete pPath;
  140. }
  141.  
  142. void TIconWindow::GetWindowClass(WNDCLASS _FAR & wc)
  143. {
  144.     TWindow::GetWindowClass(wc);
  145.  
  146.     // If we don't set our class's icon to NULL, USER won't let us
  147.     // paint our own...
  148.  
  149.     wc.hIcon = NULL;
  150. }
  151.  
  152. LPSTR TIconWindow::GetClassName()
  153. {
  154.     return( "TIconWindow" );
  155. }
  156.  
  157. void TIconWindow::WMQueryOpen(RTMessage Msg)
  158. {
  159.     ShellExecute
  160.         (
  161.             HWindow,
  162.             NULL,
  163.             pPath,
  164.             "",
  165.             ".\\",
  166.             SW_SHOWNORMAL
  167.          );
  168.  
  169.     Msg.Result = 0L;
  170. }
  171.  
  172. void TIconWindow::WMQueryDragIcon(RTMessage Msg)
  173. {
  174.     Msg.Result = DWORD(hIcon);
  175. }
  176.  
  177. void TIconWindow::WMSysCommand(RTMessage Msg)
  178. {
  179.         // Capturing these variations on WM_SYSCOMMAND prevent an
  180.         // annoying 'beep' on single clicks on the icon
  181.  
  182.     switch(Msg.WParam & 0xFFF0)
  183.     {
  184.         case SC_MOUSEMENU:
  185.         case SC_KEYMENU:
  186.             Msg.Result = 0L;
  187.             break;
  188.         default:
  189.             DefWndProc(Msg);
  190.     }
  191. }
  192.  
  193. void TIconWindow::Paint(HDC hDC, PAINTSTRUCT& )
  194. {
  195.         // This is the 'new' way to draw you own icon, as opposed
  196.         // to WM_PAINTICON in Win3.0
  197.  
  198.     DefWindowProc(HWindow, WM_ICONERASEBKGND, WORD(hDC), 0L );
  199.     DrawIcon(hDC, 0, 0, hIcon );
  200. }
  201.  
  202. void TDropTargetWin::SetupWindow()
  203. {
  204.     TWindow::SetupWindow();
  205.  
  206.         // Let Shell.dll flip the WS_EX_ACCEPTFILES style bit
  207.         // for this window
  208.  
  209.     DragAcceptFiles(HWindow,TRUE);
  210. }
  211.  
  212. void TDropTargetWin::WMDropFiles(RTMessage Msg)
  213. {
  214.     POINT   ptDrop;
  215.     HDROP   hDrop = HDROP(Msg.WParam);
  216.  
  217.     DragQueryPoint( hDrop, &ptDrop );
  218.  
  219.         // By passing in exactly these parameters, we get the
  220.         // number of files (and directories) being dropped
  221.  
  222.     int nNumDropped = DragQueryFile( hDrop, -1U, NULL, 0 );
  223.  
  224.     char *p = new char[255];
  225.  
  226.     for( int i = 0; i < nNumDropped; i++)
  227.     {
  228.         // This time we pass in the 'real' parameters and SHELL.DLL
  229.         // will fill in the path to the file (or directory)
  230.  
  231.         DragQueryFile( hDrop, i, p, 255 );
  232.  
  233.         DropAFile( p, ptDrop.x, ptDrop.y );
  234.     }
  235.  
  236.     delete p;
  237.  
  238.     DragFinish( hDrop );
  239. }
  240.  
  241. void TDropTargetWin::DropAFile( char *fileName, int x, int y)
  242. {
  243.     PTIconWindow pIW = new TIconWindow( this, fileName, x, y );
  244.  
  245.     GetApplication()->MakeWindow(pIW);
  246. }
  247.  
  248.  
  249. void TMyApp::InitMainWindow()
  250. {
  251.   MainWindow = new TDropTargetWin(NULL, Name);
  252. }
  253.  
  254.  
  255. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  256.   LPSTR lpCmdLine, int nCmdShow)
  257. {
  258.   TMyApp MyApp("Sample ObjectWindows Program/Shell API",
  259.                 hInstance, hPrevInstance,
  260.                 lpCmdLine, nCmdShow);
  261.   MyApp.Run();
  262.   return MyApp.Status;
  263. }
  264.