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

Delphi控件源码

开发平台:

Visual C++

  1. #include <windows.h>
  2. #include "appmain.h"
  3. #include "ctls.h"
  4. #define MAX_BUTTON_NUMBER 50
  5. typedef struct tag_CtlsInfo
  6. {          
  7.   //int StyleNo; 
  8.   int CurCtlCount;
  9.   int CurCtlStart,CurCtlEnd;
  10.   HBITMAP hBmpUpCtl,hBmpDnCtl,hBmpDsCtl;
  11.   CtlInfo Btns[MAX_BUTTON_NUMBER];
  12. }CtlsInfo;
  13. CtlsInfo CtlBtns[STYLE_MAX_COUNT];
  14. CtlInfo far *lpCurCtlInfo=NULL;  
  15. long CALLBACK __export CtlWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  16. {             
  17.   RECT rc;        
  18.   static int CurSelCtl =-1;
  19.   static int CurStyle =0;
  20.   PAINTSTRUCT ps;           
  21.   HPEN hPen;  
  22.   HBITMAP hBmp;
  23.   //static HBITMAP hBmpUpCtl[STYLE_MAX_COUNT],hBmpDnCtl[STYLE_MAX_COUNT],hBmpDsCtl[STYLE_MAX_COUNT];
  24.   static HBITMAP hBmpUpLeft,hBmpDnLeft,hBmpDsLeft,
  25.                  hBmpUpRight,hBmpDnRight,hBmpDsRight,
  26.                  hBmpArrow;  
  27.   //static int CurCtlStart[STYLE_MAX_COUNT],CurCtlEnd[STYLE_MAX_COUNT],CurCtlCount[STYLE_MAX_COUNT];
  28.   static int ctlWidth =0;
  29.   static int ctlXStart =0;
  30.   static BOOL isCtlMouseDown =0,isCtlMouseOut =FALSE,isLeft =0; 
  31.   static BOOL fCtlMouseOut =0;
  32.   int xStart =0;
  33.   HDC hMemDC,hDC;                       
  34.   POINT pt;                                            
  35.   BITMAP bmp;                
  36.   int i,j;
  37.   char stmp[100];
  38.   
  39.   switch(message)            
  40.   {                          
  41.     case WM_CREATE:
  42.       hBmpUpLeft = LoadBitmap(hInst,"BMP_UPLEFT");
  43.       hBmpDnLeft = LoadBitmap(hInst,"BMP_DNLEFT");
  44.       hBmpDsLeft = LoadBitmap(hInst,"BMP_DSLEFT");
  45.       hBmpUpRight = LoadBitmap(hInst,"BMP_UPRIGHT");
  46.       hBmpDnRight = LoadBitmap(hInst,"BMP_DNRIGHT");
  47.       hBmpDsRight = LoadBitmap(hInst,"BMP_DSRIGHT");
  48.       hBmpArrow = LoadBitmap(hInst,"BMP_ARROW");                   
  49.       if(!hBmpUpLeft || !hBmpUpRight || !hBmpArrow
  50.          || !hBmpDnLeft || !hBmpDsLeft || !hBmpDnRight || !hBmpDsRight)
  51.       {
  52.         ErrorMessage("Error Load Ctl Bitmap");
  53.         return -1;    
  54.       }  
  55.       
  56.       //// /////
  57.       for(i=0;i<2;i++)
  58.       {        
  59.         wsprintf(stmp,"BMP_UPSTYLE%d",i);
  60.         CtlBtns[i].hBmpUpCtl = LoadBitmap(hInst, stmp); 
  61.         wsprintf(stmp,"BMP_DNSTYLE%d",i);
  62.         CtlBtns[i].hBmpDnCtl = LoadBitmap(hInst, stmp); 
  63.         if(!CtlBtns[i].hBmpUpCtl || !CtlBtns[i].hBmpDnCtl)
  64.         {
  65.           ErrorMessage("Error Load Ctl Bitmap");
  66.           return -1;    
  67.         }  
  68.       }
  69.       xStart =0;
  70.       GetObject(hBmpArrow,sizeof(bmp),(LPSTR)&bmp);
  71.       ctlWidth = bmp.bmHeight-1;
  72.       xStart = bmp.bmWidth+5;                       
  73.       GetObject(hBmpUpLeft,sizeof(bmp),(LPSTR)&bmp);
  74.       xStart += bmp.bmWidth*2+10;
  75.       ctlXStart =xStart-bmp.bmWidth;
  76.       GetClientRect(hWnd,&rc);       
  77.       
  78.       for(i=0;i<2;i++)
  79.       {
  80.         GetObject(CtlBtns[i].hBmpUpCtl,sizeof(bmp),(LPSTR)&bmp);
  81.         CtlBtns[i].CurCtlStart =0;
  82.         CtlBtns[i].CurCtlCount =bmp.bmWidth/(bmp.bmHeight-1);
  83.         CtlBtns[i].CurCtlEnd =(rc.right-xStart-3)/(bmp.bmHeight-1);  
  84.         if(CtlBtns[i].CurCtlEnd>CtlBtns[i].CurCtlCount) CtlBtns[i].CurCtlEnd =CtlBtns[i].CurCtlCount;
  85.       }
  86.       for(j=0;j<10;j++)
  87.       {
  88.         CtlBtns[0].Btns[j].WndStyle =WndStyles[j];
  89.         CtlBtns[0].Btns[j].WndCount =0;
  90.         lstrcpy((LPSTR)CtlBtns[0].Btns[j].ClassName,ClassNames[j]);
  91.       }
  92.     break;
  93.     
  94.     case WM_SIZE: 
  95.       xStart =0;
  96.       GetObject(hBmpArrow,sizeof(bmp),(LPSTR)&bmp);
  97.       xStart = bmp.bmWidth+5;
  98.       GetObject(hBmpUpLeft,sizeof(bmp),(LPSTR)&bmp);
  99.       xStart += bmp.bmWidth*2+10;
  100.       for(i=0;i<2;i++)
  101.       {
  102.         GetObject(CtlBtns[i].hBmpUpCtl,sizeof(bmp),(LPSTR)&bmp);
  103.         if(gSlipped)
  104.         {
  105.           if(i==CurStyle)
  106.             j =CtlBtns[i].CurCtlStart; 
  107.           CtlBtns[i].CurCtlStart =CtlBtns[i].CurCtlEnd-(LOWORD(lParam)-xStart-3)/(bmp.bmHeight-1);
  108.           if(CtlBtns[i].CurCtlStart<0) CtlBtns[i].CurCtlStart =0;
  109.           if(xStart+3+(CtlBtns[i].CurCtlEnd-CtlBtns[i].CurCtlStart)*ctlWidth<(int)LOWORD(lParam))
  110.           {
  111.             CtlBtns[i].CurCtlEnd +=(LOWORD(lParam)
  112.                   -(xStart+3+(CtlBtns[i].CurCtlEnd-CtlBtns[i].CurCtlStart)*ctlWidth))/ctlWidth;
  113.             if(CtlBtns[i].CurCtlEnd>CtlBtns[i].CurCtlCount) CtlBtns[i].CurCtlEnd =CtlBtns[i].CurCtlCount;
  114.           }
  115.           if(i==CurStyle)
  116.           {
  117.             j =CtlBtns[i].CurCtlStart-j;
  118.             if(CurSelCtl >=0)
  119.               CurSelCtl -=j;
  120.           }    
  121.         }
  122.         else
  123.         {
  124.           if(xStart+3+(CtlBtns[i].CurCtlEnd-CtlBtns[i].CurCtlStart)*ctlWidth<(int)LOWORD(lParam))
  125.           {
  126.             if(i==CurStyle)
  127.               j =CtlBtns[i].CurCtlStart; 
  128.             CtlBtns[i].CurCtlStart -=(LOWORD(lParam)
  129.                   -(xStart+3+(CtlBtns[i].CurCtlEnd-CtlBtns[i].CurCtlStart)*ctlWidth))/ctlWidth;
  130.             if(CtlBtns[i].CurCtlStart<0) CtlBtns[i].CurCtlStart =0;
  131.             if(i==CurStyle)
  132.             {
  133.               j =CtlBtns[i].CurCtlStart-j;
  134.               if(CurSelCtl >=0)
  135.                 CurSelCtl -=j;
  136.             }    
  137.           }
  138.           CtlBtns[i].CurCtlEnd =(LOWORD(lParam)-xStart-3)/(bmp.bmHeight-1)+CtlBtns[i].CurCtlStart;
  139.           if(CtlBtns[i].CurCtlEnd>CtlBtns[i].CurCtlCount) CtlBtns[i].CurCtlEnd=CtlBtns[i].CurCtlCount;
  140.         }  
  141.       }
  142.       InvalidateRect(hWnd,NULL,TRUE);
  143.     return 0L;
  144.     
  145.     case WM_STYLECHANGED:
  146.       if(CurStyle ==(int)wParam) break;
  147.       CurSelCtl =-1;
  148.       CurStyle =(int)wParam;
  149.       //if(CurStyle>1) return 0L;                          
  150.       InvalidateRect(hWnd,NULL,TRUE);
  151.     return 0L;
  152.     
  153.     case WM_DRAWBUTTON:
  154.       if(wParam!=0)
  155.       {
  156.         //draw all the button: CurStyle -- BtnStart to BtnEnd
  157.         hDC = (HDC)wParam;
  158.         hMemDC = CreateCompatibleDC (hDC) ;   
  159.         xStart =0;
  160.         //if(hBmpArrow)
  161.         {
  162.           SelectObject(hMemDC,hBmpArrow);
  163.           GetObject(hBmpArrow,sizeof(bmp),(LPSTR)&bmp);
  164.           xStart =5;
  165.           BitBlt (hDC, xStart,5,
  166.                   bmp.bmWidth,bmp.bmHeight,hMemDC,0,0,SRCCOPY) ;
  167.           xStart =xStart+bmp.bmWidth+10;        
  168.         } 
  169.         //if(hBmpCtlLeft)
  170.         {
  171.           if(CtlBtns[CurStyle].CurCtlStart>0)
  172.             SelectObject(hMemDC,hBmpUpLeft);
  173.           else SelectObject(hMemDC,hBmpDsLeft);  
  174.           GetObject(hBmpUpLeft,sizeof(bmp),(LPSTR)&bmp);
  175.           BitBlt (hDC, xStart,5,
  176.                   bmp.bmWidth,bmp.bmHeight,hMemDC,0,0,SRCCOPY) ;
  177.           xStart +=bmp.bmWidth;
  178.         }
  179.         //if(hBmpCtl[CurStyle])
  180.         {         
  181.           SelectObject (hMemDC, CtlBtns[CurStyle].hBmpUpCtl) ;    
  182.           GetObject(CtlBtns[CurStyle].hBmpUpCtl,sizeof(bmp),(LPSTR)&bmp);
  183.           BitBlt (hDC, xStart,5,
  184.                   (CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)*(bmp.bmHeight-1),
  185.                   bmp.bmHeight,hMemDC,
  186.                   CtlBtns[CurStyle].CurCtlStart*(bmp.bmHeight-1),0,SRCCOPY) ;
  187.           if(CurSelCtl>=0
  188.              && CurSelCtl<CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)
  189.           {
  190.             SelectObject(hMemDC,CtlBtns[CurStyle].hBmpDnCtl);
  191.             BitBlt (hDC, ctlXStart+CurSelCtl*ctlWidth,5,
  192.                   ctlWidth,ctlWidth+1,hMemDC,
  193.                   (CurSelCtl+CtlBtns[CurStyle].CurCtlStart)*ctlWidth,0,SRCCOPY) ;
  194.           }            
  195.           else CurSelCtl =-1;
  196.           xStart += (CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)*(bmp.bmHeight-1);
  197.         }
  198.         //if(hBmpCtlRight)
  199.         {                                      
  200.           if(CtlBtns[CurStyle].CurCtlEnd<CtlBtns[CurStyle].CurCtlCount)
  201.             SelectObject(hMemDC,hBmpUpRight);
  202.           else SelectObject(hMemDC,hBmpDsRight);  
  203.           GetObject(hBmpUpRight,sizeof(bmp),(LPSTR)&bmp);
  204.           BitBlt (hDC, xStart,5,
  205.                   bmp.bmWidth,bmp.bmHeight,hMemDC,0,0,SRCCOPY) ;
  206.         }                       
  207.         DeleteDC (hMemDC) ;           
  208.       }                      
  209.       else if(wParam ==0)
  210.       {          
  211.         if(CurSelCtl <0
  212.            || CurSelCtl >=(CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart))
  213.         {
  214.           CurSelCtl =-1;
  215.           return 0L;    
  216.         }  
  217.         switch(lParam)
  218.         {
  219.           case 0L://hbmp=up
  220.             hBmp =CtlBtns[CurStyle].hBmpUpCtl;
  221.           break;
  222.           case 1L://hbmp=down  
  223.             hBmp =CtlBtns[CurStyle].hBmpDnCtl;
  224.           break;
  225.           case 2L: //hbmp=disable
  226.           break;
  227.           case 3L: //left up
  228.           break;
  229.           case 4L: //left down
  230.           break;
  231.           case 5L: //left disable
  232.           break;
  233.           case 6L: //right up
  234.           break;
  235.           case 7L: //right down
  236.           break;
  237.           case 8L: //right disable
  238.           break;
  239.           default:
  240.           return 0L;
  241.         }                               
  242.         //draw the button           
  243.         hDC=GetDC(hWnd);
  244.         hMemDC =CreateCompatibleDC(hDC);
  245.         SelectObject(hMemDC,hBmp);
  246.         if(lParam <3L)
  247.           BitBlt (hDC, ctlXStart+CurSelCtl*ctlWidth,5,
  248.                   ctlWidth,ctlWidth+1,hMemDC,
  249.                   (CurSelCtl+CtlBtns[CurStyle].CurCtlStart)*ctlWidth,0,SRCCOPY) ;
  250.         DeleteDC(hMemDC);
  251.         ReleaseDC(hWnd,hDC);
  252.       }
  253.     return 0L;           
  254.     
  255.     case WM_SETCURSOR:
  256.       //Set Cursor or Show Hint if in a button or Hide Hint if out a button;
  257.       SetCursor(LoadCursor(NULL,IDC_ARROW)); 
  258.       isInSlip = FALSE;
  259.     return TRUE;                                    
  260.     
  261.     case WM_LBUTTONDOWN:
  262.       pt=MAKEPOINT(lParam);                     
  263.       if(CtlBtns[CurStyle].CurCtlStart>0)
  264.       {
  265.         SetRect(&rc,ctlXStart-ctlWidth/2,5,ctlXStart,5+ctlWidth+1);
  266.         if(PtInRect(&rc,pt))
  267.         {                                      
  268.           isLeft =TRUE;
  269.           SendMessage(hWnd,WM_DRAWBUTTON,0,4L);
  270.           SetCapture(hWnd);
  271.           isCtlMouseDown =TRUE;
  272.           return 0L;
  273.         }
  274.       }  
  275.       if(CtlBtns[CurStyle].CurCtlEnd<CtlBtns[CurStyle].CurCtlCount)
  276.       {
  277.         SetRect(&rc,ctlXStart+(CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)*ctlWidth,5,
  278.                     ctlXStart+(CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)*ctlWidth+ctlWidth/2,
  279.                     5+ctlWidth+1);
  280.         if(PtInRect(&rc,pt))
  281.         {                                      
  282.           isLeft =FALSE;
  283.           SendMessage(hWnd,WM_DRAWBUTTON,0,7L);
  284.           SetCapture(hWnd);
  285.           isCtlMouseDown =TRUE;
  286.           return 0L;
  287.         }
  288.       }  
  289.       for(i=0;i<CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart;i++)
  290.       {
  291.         SetRect(&rc,ctlXStart+i*ctlWidth,5,
  292.               ctlXStart+(i+1)*ctlWidth,5+ctlWidth+1);
  293.         if (PtInRect(&rc, pt))
  294.           break;
  295.       }         
  296.       if(i+CtlBtns[CurStyle].CurCtlStart ==CtlBtns[CurStyle].CurCtlEnd) return 0L;
  297.       else
  298.       {          
  299.         SendMessage(hWnd,WM_DRAWBUTTON,0,0L);
  300.         CurSelCtl=i;//+CtlBtns[CurStyle].CurCtlStart;
  301.         SendMessage(hWnd,WM_DRAWBUTTON,0,1L);              
  302.         lpCurCtlInfo = (CtlInfo far *)&CtlBtns[CurStyle].Btns[CurSelCtl+CtlBtns[CurStyle].CurCtlStart];
  303.         //LoadString(hInst, 11000+CurStyle*100+i,(LPSTR)Cur_Ctl_Style, sizeof(Cur_Ctl_Style));
  304.       }
  305.     return 0L;
  306.     
  307.     case WM_MOUSEMOVE:   
  308.       if(!isCtlMouseDown) break;
  309.       pt=MAKEPOINT(lParam);                     
  310.       if(isLeft)
  311.         SetRect(&rc,ctlXStart-ctlWidth/2,5,ctlXStart,5+ctlWidth/2+1);
  312.       else
  313.         SetRect(&rc,ctlXStart+(CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)*ctlWidth,5,
  314.                     ctlXStart+(CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)*ctlWidth+ctlWidth/2,
  315.                     5+ctlWidth+1);
  316.       fCtlMouseOut = isCtlMouseOut;
  317.       if(PtInRect(&rc,pt))
  318.         isCtlMouseOut =FALSE;
  319.       else isCtlMouseOut =TRUE;
  320.       if(fCtlMouseOut !=isCtlMouseOut)
  321.       {
  322.         if(isCtlMouseOut)
  323.         {
  324.           if(isLeft)
  325.             SendMessage(hWnd,WM_DRAWBUTTON, 0,3L);
  326.           else SendMessage(hWnd,WM_DRAWBUTTON,0,6L);
  327.         }
  328.         else
  329.         {
  330.           if(isLeft)
  331.             SendMessage(hWnd,WM_DRAWBUTTON,0,4L);
  332.           else SendMessage(hWnd,WM_DRAWBUTTON,0,7L);
  333.         }  
  334.       }  
  335.     return 0L;
  336.     
  337.     case WM_LBUTTONUP:
  338.       if(!isCtlMouseDown) break;
  339.       ReleaseCapture();
  340.       isCtlMouseDown =FALSE;
  341.       if(!isCtlMouseOut)        
  342.       {                      
  343.         if(isLeft)
  344.         {
  345.           CtlBtns[CurStyle].CurCtlStart--;
  346.           CtlBtns[CurStyle].CurCtlEnd--;
  347.           if(CurSelCtl >=0)
  348.             CurSelCtl++;          /////
  349.         }
  350.         else
  351.         {
  352.           CtlBtns[CurStyle].CurCtlStart++;
  353.           CtlBtns[CurStyle].CurCtlEnd++;
  354.           if(CurSelCtl >=0)
  355.             CurSelCtl--;    /////
  356.         }
  357.         InvalidateRect(hWnd,NULL,TRUE);
  358.       }
  359.       else isCtlMouseOut =FALSE;
  360.     return 0L;
  361.     
  362.     case WM_PAINT:
  363.       GetClientRect(hWnd,&rc);
  364.       BeginPaint(hWnd,&ps); 
  365.       hPen = CreatePen(PS_SOLID,1,RGB(80,80,80));
  366.       SelectObject(ps.hdc,hPen);
  367.       //MoveTo(ps.hdc,0,rc.bottom-1);         
  368.       MoveTo(ps.hdc,rc.right-1,rc.bottom-1); 
  369.       LineTo(ps.hdc,rc.right-1,0);
  370.       SelectObject(ps.hdc,GetStockObject(WHITE_PEN));
  371.       DeleteObject(hPen);
  372.       //MoveTo(ps.hdc,0,1);
  373.       LineTo(ps.hdc,0,0); 
  374.       LineTo(ps.hdc,0,rc.bottom-1);    
  375.       SendMessage(hWnd,WM_DRAWBUTTON,(WPARAM)(HDC)ps.hdc,(LONG)MAKELONG(20,rc.bottom));
  376.       EndPaint(hWnd,&ps);
  377.     break;
  378.              
  379.     case WM_DESTROY:                               
  380.       for(i=0;i<2;i++)
  381.       {
  382.         if(CtlBtns[i].hBmpUpCtl) DeleteObject(CtlBtns[i].hBmpUpCtl);
  383.         if(CtlBtns[i].hBmpDnCtl) DeleteObject(CtlBtns[i].hBmpDnCtl);
  384.         if(CtlBtns[i].hBmpDsCtl) DeleteObject(CtlBtns[i].hBmpDsCtl);
  385.       }
  386.       if(hBmpUpLeft) DeleteObject(hBmpUpLeft);
  387.       if(hBmpUpRight) DeleteObject(hBmpUpRight);
  388.       if(hBmpDnLeft) DeleteObject(hBmpDnLeft);
  389.       if(hBmpDnRight) DeleteObject(hBmpDnRight);
  390.       if(hBmpDsLeft) DeleteObject(hBmpDsLeft);
  391.       if(hBmpDsRight) DeleteObject(hBmpDsRight);
  392.       if(hBmpArrow) DeleteObject(hBmpArrow);  
  393.     break;
  394.              
  395.     default:
  396.     break;
  397.   }
  398.   
  399.   return (DefWindowProc(hWnd, message, wParam, lParam));
  400. }