FORMPROC.C
上传用户:needlamp
上传日期:2007-01-07
资源大小:78k
文件大小:11k
源码类别:

Delphi控件源码

开发平台:

Visual C++

  1. #include <windows.h>
  2. #include "appmain.h"
  3. #include "formproc.h"
  4. long CALLBACK __export FormWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  5. {
  6.   static BOOL isMouseDown =FALSE,isChildMouseDown =FALSE,isRecSizing =FALSE;
  7.   static POINT pt0,pt1;
  8.   POINT pt;
  9.   static POINT ptFrame;
  10.   HDC hDC;
  11.   HWND hwnd;             
  12.   RECT rc,rcForm;      
  13.   int i,j;
  14.   HWND hWndCurSel;              
  15.   char tmp[150];
  16.   int CtlNo;
  17.   
  18.   switch(message)
  19.   {
  20.     case WM_CREATE: 
  21.       GetWindowRect(hWnd,&rc);
  22.       ptFrame.x =ptFrame.y =0;
  23.       ClientToScreen(hWnd,&ptFrame);
  24.       ptFrame.x -=rc.left;
  25.       ptFrame.y -=rc.top;
  26.       SetWindowWord(hWnd,CTLNO,0);
  27.     return 0L;
  28.     
  29.     case WM_SETFOCUS:
  30.       if(GetParent(hWndRecSize[0])!=hWnd)
  31.       {
  32.         for(i=0;i<8;i++){
  33.           SetParent(hWndRecSize[i],hWnd); 
  34.           ShowWindow(hWndRecSize[i],SW_HIDE);  
  35.         }
  36.         hWndCurSel =(HWND)GetWindowWord(hWnd,CUR_SELWND);  
  37.         //if(hWndCurSel)
  38.           //SetRecPos()
  39.       }
  40.     break;
  41.     
  42.     case WM_SETCURSOR:
  43.       if(IsChild(hWnd,(HWND)wParam))
  44.       {
  45.         SetCursor(LoadCursor(NULL,IDC_SIZE));
  46.         return TRUE;
  47.       }
  48.     break;
  49.     
  50.     case WM_ACTIVATEAPP:                               
  51.       if(wParam) SetActiveWindow(hWnd);
  52.     break;
  53.     
  54.     case WM_SYSCOMMAND:
  55.       if(wParam ==SC_KEYMENU)
  56.       { 
  57.         hWndPrev =hWnd;
  58.         SetFocus(hWndMain);
  59.         SendMessage(hWndMain,WM_SYSCOMMAND,wParam,lParam);
  60.         return 0L;
  61.       }
  62.     break;
  63.     
  64.     case WM_RECSIZING:
  65.       isRecSizing =TRUE;
  66.       switch(wParam)
  67.       {
  68.         case 0:
  69.         break;
  70.         case 1:
  71.         break;
  72.       }
  73.     break;
  74.     
  75.     case WM_PARENTNOTIFY:  
  76.       if(wParam ==WM_LBUTTONDOWN)
  77.       {
  78.         pt =pt0 =MAKEPOINT(lParam);
  79.         CtlNo =GetWindowWord(hWnd,CTLNO);
  80.         for(i=0;i<CtlNo;i++)
  81.         {
  82.           hwnd =GetDlgItem(hWnd,100+i);
  83.           if(hwnd)
  84.           {
  85.             GetWindowRect(hwnd,&rc); 
  86.             pt1.x =rc.left;
  87.             pt1.y =rc.top;
  88.             ScreenToClient(hWnd,&pt1);
  89.             rc.right =rc.right-rc.left+pt1.x;
  90.             rc.bottom =rc.bottom-rc.top+pt1.y;
  91.             rc.left =pt1.x;
  92.             rc.top =pt1.y;  
  93.             if(PtInRect(&rc,pt0)) 
  94.             {
  95.               hWndCurSel =hwnd;
  96.               SetWindowWord(hWnd, CUR_SELWND,hWndCurSel);
  97.               pt0.x -=pt1.x;
  98.               pt0.y -=pt1.y;
  99.               pt1 =pt;                
  100.               hDC =GetWindowDC(hWnd);
  101.               GetClientRect(hwnd,&rc); 
  102.               SelectObject(hDC, GetStockObject(NULL_BRUSH));
  103.               SelectObject(hDC, GetStockObject(WHITE_PEN));
  104.               SetROP2(hDC, R2_NOT);
  105.               Rectangle(hDC,pt1.x-pt0.x+ptFrame.x,pt1.y-pt0.y+ptFrame.y,
  106.                 pt1.x-pt0.x+rc.right+ptFrame.x,pt1.y-pt0.y+rc.bottom+ptFrame.y); 
  107.               ReleaseDC(hWnd,hDC);  
  108.               if(GetParent(hWndRecSize[0])!=hWnd)
  109.                 for(j=0;j<8;j++) SetParent(hWndRecSize[j],hWnd); 
  110.               for(j=0;j<8;j++)
  111.               {                             
  112.                 switch(j)
  113.                 {
  114.                   case 0:
  115.                     //SetWindowPos(hWndRecSize[j],
  116.                   break;
  117.                   case 1:
  118.                     //SetWindowPos(hWndRecSize[j],
  119.                   break;
  120.                   case 2:
  121.                     //SetWindowPos(hWndRecSize[j],
  122.                   break;
  123.                   case 3:
  124.                     //SetWindowPos(hWndRecSize[j],
  125.                   break;
  126.                   case 4:
  127.                     //SetWindowPos(hWndRecSize[j],
  128.                   break;
  129.                   case 5:
  130.                     //SetWindowPos(hWndRecSize[j],
  131.                   break;
  132.                   case 6:
  133.                     //SetWindowPos(hWndRecSize[j],
  134.                   break;
  135.                   case 7:
  136.                     //SetWindowPos(hWndRecSize[j],
  137.                   break;
  138.                 }               
  139.                 ShowWindow(hWndRecSize[j],SW_SHOW);
  140.               }  
  141.               SetFocus(hWnd);
  142.               break;
  143.             }
  144.           }          
  145.         }
  146.       }
  147.     break;
  148.     
  149.     case WM_MOUSEACTIVATE:    
  150.       if(HIWORD(lParam) ==WM_LBUTTONDOWN && LOWORD(lParam)==HTCLIENT && GetFocus()==hWnd)
  151.       {
  152.         SetCapture(hWnd);
  153.         if(isRecSizing) 
  154.         {
  155.           isChildMouseDown =FALSE;
  156.           isRecSizing =FALSE;
  157.         }
  158.         else
  159.           isChildMouseDown =TRUE;
  160.         return MA_NOACTIVATEANDEAT;
  161.       }              
  162.     break;
  163.     
  164.     case WM_LBUTTONDOWN: 
  165.       isMouseDown =TRUE; 
  166.       pt1 =pt0 =MAKEPOINT(lParam);   
  167.       SetCapture(hWnd);
  168.     return 0L;
  169.     
  170.     case WM_MOUSEMOVE:     
  171.       if(!isMouseDown && !isChildMouseDown) break;
  172.       hDC =GetWindowDC(hWnd);   
  173.       hWndCurSel =(HWND)GetWindowWord(hWnd,CUR_SELWND);
  174.       SelectObject(hDC, GetStockObject(NULL_BRUSH));
  175.       SelectObject(hDC, GetStockObject(WHITE_PEN));
  176.       SetROP2(hDC, R2_NOT);
  177.       if(isMouseDown)                  
  178.         Rectangle(hDC, pt0.x+ptFrame.x,pt0.y+ptFrame.y,pt1.x+ptFrame.x,pt1.y+ptFrame.y);
  179.       else
  180.       {                 
  181.         if(hWndCurSel)
  182.         {
  183.           GetClientRect(hWndCurSel,&rc);
  184.           Rectangle(hDC,pt1.x-pt0.x+ptFrame.x,pt1.y-pt0.y+ptFrame.y,
  185.                 pt1.x-pt0.x+rc.right+ptFrame.x,pt1.y-pt0.y+rc.bottom+ptFrame.y); 
  186.         }        
  187.       }
  188.       pt1 =MAKEPOINT(lParam);
  189.       if(isMouseDown)
  190.         Rectangle(hDC, pt0.x+ptFrame.x,pt0.y+ptFrame.y,pt1.x+ptFrame.x,pt1.y+ptFrame.y);
  191.       else if(hWndCurSel)
  192.         Rectangle(hDC,pt1.x-pt0.x+ptFrame.x,pt1.y-pt0.y+ptFrame.y,
  193.            pt1.x-pt0.x+rc.right+ptFrame.x,pt1.y-pt0.y+rc.bottom+ptFrame.y); 
  194.         
  195.       ReleaseDC(hWnd,hDC);
  196.     return 0L;
  197.         
  198.     case WM_LBUTTONUP: 
  199.       ReleaseCapture();
  200.       if(!isMouseDown && !isChildMouseDown) return 0L;
  201.       pt1 =MAKEPOINT(lParam); 
  202.       hWndCurSel =GetWindowWord(hWnd,CUR_SELWND);
  203.       hDC =GetWindowDC(hWnd);
  204.       SetROP2(hDC, R2_NOT);
  205.       SelectObject(hDC, GetStockObject(NULL_BRUSH));
  206.       SelectObject(hDC, GetStockObject(WHITE_PEN));
  207.       if(isChildMouseDown && hWndCurSel)
  208.       {
  209.         GetClientRect(hWndCurSel,&rc);
  210.         Rectangle(hDC,pt1.x-pt0.x+ptFrame.x,pt1.y-pt0.y+ptFrame.y,
  211.            pt1.x-pt0.x+rc.right+ptFrame.x,pt1.y-pt0.y+rc.bottom+ptFrame.y); 
  212.         GetClientRect(hWndCurSel,&rc); 
  213.         if(pt0.x-pt1.x>rc.right) pt1.x =pt0.x;
  214.         if(pt0.y-pt1.y>rc.bottom) pt1.y =pt0.y; 
  215.         GetClientRect(hWnd,&rcForm);
  216.         if(pt1.x-pt0.x>rcForm.right) pt1.x =pt0.x+rcForm.right-rc.right;
  217.         if(pt1.y-pt0.y>rcForm.bottom) pt1.y =pt0.y+rcForm.bottom-rc.bottom;
  218.         SetWindowPos(hWndCurSel,0,pt1.x-pt0.x,pt1.y-pt0.y,0,0,SWP_NOSIZE |SWP_NOZORDER);
  219.         InvalidateRect(hWndCurSel,NULL,FALSE);
  220.         isChildMouseDown =FALSE;   
  221.         ReleaseDC(hWnd,hDC);                  
  222.         pt0.x =pt1.x-pt0.x;
  223.         pt0.y =pt1.y-pt0.y;  
  224.         pt1.x =pt0.x+rc.right;
  225.         pt1.y =pt0.y+rc.bottom;
  226.         //return TRUE;
  227.       }
  228.       else
  229.       if(isMouseDown)
  230.       {                           
  231.         isMouseDown =FALSE; 
  232.         CtlNo =GetWindowWord(hWnd,CTLNO);
  233.         Rectangle(hDC, pt0.x+ptFrame.x,pt0.y+ptFrame.y,pt1.x+ptFrame.x,pt1.y+ptFrame.y);
  234.         if(lpCurCtlInfo)
  235.         {
  236.           wsprintf((LPSTR)tmp,"%s%d",(LPSTR)lpCurCtlInfo->ClassName,lpCurCtlInfo->WndCount+1);
  237.           if(pt1.x<pt0.x)
  238.           {
  239.             pt.x =pt1.x;
  240.             pt1.x =pt0.x;
  241.             pt0.x =pt.x;
  242.           }  
  243.           if(pt1.y<pt0.y)
  244.           {
  245.             pt.y =pt1.y;
  246.             pt1.y =pt0.y;
  247.             pt0.y =pt.y;
  248.           }
  249.           hwnd =CreateWindow(lpCurCtlInfo->ClassName,tmp,WS_CHILD | WS_VISIBLE | lpCurCtlInfo->WndStyle,
  250.             pt0.x, pt0.y, pt1.x-pt0.x, pt1.y-pt0.y,
  251.             hWnd, 100+CtlNo,hInst,NULL);     
  252.           if(hwnd)
  253.           {
  254.             SetWindowWord(hWnd,CTLNO,CtlNo+1);
  255.             lpCurCtlInfo->WndCount++;
  256.           }
  257.           hWndCurSel=hwnd;
  258.         }
  259.         ReleaseDC(hWnd,hDC);       
  260.       }    
  261.       if(GetParent(hWndRecSize[0])!=hWnd)
  262.         for(i=0;i<8;i++) SetParent(hWndRecSize[i],hWnd); 
  263.       if(hWndCurSel)
  264.       for(i=0;i<8;i++)
  265.       {                             
  266.         ShowWindow(hWndRecSize[i],SW_SHOW);                              
  267.         switch(i)
  268.         {
  269.           case 0:
  270.             SetWindowPos(hWndRecSize[i],HWND_TOP,pt0.x-3,pt0.y-3,0,0,SWP_NOSIZE|SWP_NOACTIVATE);
  271.           break;
  272.           case 1:
  273.             SetWindowPos(hWndRecSize[i],HWND_TOP,(pt1.x+pt0.x)/2,pt0.y-3,0,0,SWP_NOSIZE|SWP_NOACTIVATE);
  274.           break;
  275.           case 2:
  276.             SetWindowPos(hWndRecSize[i],HWND_TOP,pt1.x-2,pt0.y-3,0,0,SWP_NOSIZE|SWP_NOACTIVATE);
  277.           break;
  278.           case 3:
  279.             SetWindowPos(hWndRecSize[i],HWND_TOP,pt1.x-2,(pt0.y+pt1.y)/2,0,0,SWP_NOSIZE|SWP_NOACTIVATE);
  280.           break;
  281.           case 4:
  282.             SetWindowPos(hWndRecSize[i],HWND_TOP,pt1.x-2,pt1.y-2,0,0,SWP_NOSIZE|SWP_NOACTIVATE);
  283.           break;
  284.           case 5:
  285.             SetWindowPos(hWndRecSize[i],HWND_TOP,(pt1.x+pt0.x)/2,pt1.y-2,0,0,SWP_NOSIZE|SWP_NOACTIVATE);
  286.           break;
  287.           case 6:
  288.             SetWindowPos(hWndRecSize[i],HWND_TOP,pt0.x-3,pt1.y-2,0,0,SWP_NOSIZE|SWP_NOACTIVATE);
  289.           break;
  290.           case 7:
  291.             SetWindowPos(hWndRecSize[i],HWND_TOP,pt0.x-3,(pt0.y+pt1.y)/2,0,0,SWP_NOSIZE|SWP_NOACTIVATE);
  292.           break;
  293.         }      
  294.         //InvalidateRect(hWndRecSize[i],NULL,TRUE);         
  295.       }
  296.     return 0L;
  297.     
  298.     case WM_CLOSE:
  299.     break;
  300.     
  301.     default:
  302.     break;
  303.   }  
  304.   return (DefWindowProc(hWnd, message, wParam, lParam));
  305. }
  306. long CALLBACK __export RecSizeWndProc(HWND hWnd, UINT message, 
  307.                           WPARAM wParam, LPARAM lParam)
  308. {             
  309.   HCURSOR hCursor;
  310.                                         
  311.   switch(message)
  312.   {
  313.     case WM_SETCURSOR:
  314.       switch(GetWindowWord(hWnd,0))
  315.       {
  316.         case 0:                                 
  317.         case 4:
  318.           hCursor=LoadCursor(NULL,IDC_SIZENWSE);
  319.         break;
  320.         case 1:                              
  321.         case 5:
  322.           hCursor=LoadCursor(NULL,IDC_SIZENS);
  323.         break;
  324.         case 2:                                                    
  325.         case 6:
  326.           hCursor=LoadCursor(NULL,IDC_SIZENESW);
  327.         break;
  328.         case 3:
  329.         case 7:
  330.           hCursor=LoadCursor(NULL,IDC_SIZEWE);
  331.         break;
  332.         default: return FALSE;
  333.       }
  334.       SetCursor(hCursor);
  335.       return TRUE;
  336.     case WM_MOUSEMOVE:
  337.     return 0L;
  338.     case WM_LBUTTONDOWN:
  339.       SendMessage(GetParent(hWnd),WM_RECSIZING,hWnd,0L); 
  340.     return 0L;   
  341.   }
  342.   return (DefWindowProc(hWnd, message, wParam, lParam));
  343. }