CTLS.C
资源名称:cbuilder.zip [点击查看]
上传用户:needlamp
上传日期:2007-01-07
资源大小:78k
文件大小:14k
源码类别:
Delphi控件源码
开发平台:
Visual C++
- #include <windows.h>
- #include "appmain.h"
- #include "ctls.h"
- #define MAX_BUTTON_NUMBER 50
- typedef struct tag_CtlsInfo
- {
- //int StyleNo;
- int CurCtlCount;
- int CurCtlStart,CurCtlEnd;
- HBITMAP hBmpUpCtl,hBmpDnCtl,hBmpDsCtl;
- CtlInfo Btns[MAX_BUTTON_NUMBER];
- }CtlsInfo;
- CtlsInfo CtlBtns[STYLE_MAX_COUNT];
- CtlInfo far *lpCurCtlInfo=NULL;
- long CALLBACK __export CtlWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- RECT rc;
- static int CurSelCtl =-1;
- static int CurStyle =0;
- PAINTSTRUCT ps;
- HPEN hPen;
- HBITMAP hBmp;
- //static HBITMAP hBmpUpCtl[STYLE_MAX_COUNT],hBmpDnCtl[STYLE_MAX_COUNT],hBmpDsCtl[STYLE_MAX_COUNT];
- static HBITMAP hBmpUpLeft,hBmpDnLeft,hBmpDsLeft,
- hBmpUpRight,hBmpDnRight,hBmpDsRight,
- hBmpArrow;
- //static int CurCtlStart[STYLE_MAX_COUNT],CurCtlEnd[STYLE_MAX_COUNT],CurCtlCount[STYLE_MAX_COUNT];
- static int ctlWidth =0;
- static int ctlXStart =0;
- static BOOL isCtlMouseDown =0,isCtlMouseOut =FALSE,isLeft =0;
- static BOOL fCtlMouseOut =0;
- int xStart =0;
- HDC hMemDC,hDC;
- POINT pt;
- BITMAP bmp;
- int i,j;
- char stmp[100];
- switch(message)
- {
- case WM_CREATE:
- hBmpUpLeft = LoadBitmap(hInst,"BMP_UPLEFT");
- hBmpDnLeft = LoadBitmap(hInst,"BMP_DNLEFT");
- hBmpDsLeft = LoadBitmap(hInst,"BMP_DSLEFT");
- hBmpUpRight = LoadBitmap(hInst,"BMP_UPRIGHT");
- hBmpDnRight = LoadBitmap(hInst,"BMP_DNRIGHT");
- hBmpDsRight = LoadBitmap(hInst,"BMP_DSRIGHT");
- hBmpArrow = LoadBitmap(hInst,"BMP_ARROW");
- if(!hBmpUpLeft || !hBmpUpRight || !hBmpArrow
- || !hBmpDnLeft || !hBmpDsLeft || !hBmpDnRight || !hBmpDsRight)
- {
- ErrorMessage("Error Load Ctl Bitmap");
- return -1;
- }
- //// /////
- for(i=0;i<2;i++)
- {
- wsprintf(stmp,"BMP_UPSTYLE%d",i);
- CtlBtns[i].hBmpUpCtl = LoadBitmap(hInst, stmp);
- wsprintf(stmp,"BMP_DNSTYLE%d",i);
- CtlBtns[i].hBmpDnCtl = LoadBitmap(hInst, stmp);
- if(!CtlBtns[i].hBmpUpCtl || !CtlBtns[i].hBmpDnCtl)
- {
- ErrorMessage("Error Load Ctl Bitmap");
- return -1;
- }
- }
- xStart =0;
- GetObject(hBmpArrow,sizeof(bmp),(LPSTR)&bmp);
- ctlWidth = bmp.bmHeight-1;
- xStart = bmp.bmWidth+5;
- GetObject(hBmpUpLeft,sizeof(bmp),(LPSTR)&bmp);
- xStart += bmp.bmWidth*2+10;
- ctlXStart =xStart-bmp.bmWidth;
- GetClientRect(hWnd,&rc);
- for(i=0;i<2;i++)
- {
- GetObject(CtlBtns[i].hBmpUpCtl,sizeof(bmp),(LPSTR)&bmp);
- CtlBtns[i].CurCtlStart =0;
- CtlBtns[i].CurCtlCount =bmp.bmWidth/(bmp.bmHeight-1);
- CtlBtns[i].CurCtlEnd =(rc.right-xStart-3)/(bmp.bmHeight-1);
- if(CtlBtns[i].CurCtlEnd>CtlBtns[i].CurCtlCount) CtlBtns[i].CurCtlEnd =CtlBtns[i].CurCtlCount;
- }
- for(j=0;j<10;j++)
- {
- CtlBtns[0].Btns[j].WndStyle =WndStyles[j];
- CtlBtns[0].Btns[j].WndCount =0;
- lstrcpy((LPSTR)CtlBtns[0].Btns[j].ClassName,ClassNames[j]);
- }
- break;
- case WM_SIZE:
- xStart =0;
- GetObject(hBmpArrow,sizeof(bmp),(LPSTR)&bmp);
- xStart = bmp.bmWidth+5;
- GetObject(hBmpUpLeft,sizeof(bmp),(LPSTR)&bmp);
- xStart += bmp.bmWidth*2+10;
- for(i=0;i<2;i++)
- {
- GetObject(CtlBtns[i].hBmpUpCtl,sizeof(bmp),(LPSTR)&bmp);
- if(gSlipped)
- {
- if(i==CurStyle)
- j =CtlBtns[i].CurCtlStart;
- CtlBtns[i].CurCtlStart =CtlBtns[i].CurCtlEnd-(LOWORD(lParam)-xStart-3)/(bmp.bmHeight-1);
- if(CtlBtns[i].CurCtlStart<0) CtlBtns[i].CurCtlStart =0;
- if(xStart+3+(CtlBtns[i].CurCtlEnd-CtlBtns[i].CurCtlStart)*ctlWidth<(int)LOWORD(lParam))
- {
- CtlBtns[i].CurCtlEnd +=(LOWORD(lParam)
- -(xStart+3+(CtlBtns[i].CurCtlEnd-CtlBtns[i].CurCtlStart)*ctlWidth))/ctlWidth;
- if(CtlBtns[i].CurCtlEnd>CtlBtns[i].CurCtlCount) CtlBtns[i].CurCtlEnd =CtlBtns[i].CurCtlCount;
- }
- if(i==CurStyle)
- {
- j =CtlBtns[i].CurCtlStart-j;
- if(CurSelCtl >=0)
- CurSelCtl -=j;
- }
- }
- else
- {
- if(xStart+3+(CtlBtns[i].CurCtlEnd-CtlBtns[i].CurCtlStart)*ctlWidth<(int)LOWORD(lParam))
- {
- if(i==CurStyle)
- j =CtlBtns[i].CurCtlStart;
- CtlBtns[i].CurCtlStart -=(LOWORD(lParam)
- -(xStart+3+(CtlBtns[i].CurCtlEnd-CtlBtns[i].CurCtlStart)*ctlWidth))/ctlWidth;
- if(CtlBtns[i].CurCtlStart<0) CtlBtns[i].CurCtlStart =0;
- if(i==CurStyle)
- {
- j =CtlBtns[i].CurCtlStart-j;
- if(CurSelCtl >=0)
- CurSelCtl -=j;
- }
- }
- CtlBtns[i].CurCtlEnd =(LOWORD(lParam)-xStart-3)/(bmp.bmHeight-1)+CtlBtns[i].CurCtlStart;
- if(CtlBtns[i].CurCtlEnd>CtlBtns[i].CurCtlCount) CtlBtns[i].CurCtlEnd=CtlBtns[i].CurCtlCount;
- }
- }
- InvalidateRect(hWnd,NULL,TRUE);
- return 0L;
- case WM_STYLECHANGED:
- if(CurStyle ==(int)wParam) break;
- CurSelCtl =-1;
- CurStyle =(int)wParam;
- //if(CurStyle>1) return 0L;
- InvalidateRect(hWnd,NULL,TRUE);
- return 0L;
- case WM_DRAWBUTTON:
- if(wParam!=0)
- {
- //draw all the button: CurStyle -- BtnStart to BtnEnd
- hDC = (HDC)wParam;
- hMemDC = CreateCompatibleDC (hDC) ;
- xStart =0;
- //if(hBmpArrow)
- {
- SelectObject(hMemDC,hBmpArrow);
- GetObject(hBmpArrow,sizeof(bmp),(LPSTR)&bmp);
- xStart =5;
- BitBlt (hDC, xStart,5,
- bmp.bmWidth,bmp.bmHeight,hMemDC,0,0,SRCCOPY) ;
- xStart =xStart+bmp.bmWidth+10;
- }
- //if(hBmpCtlLeft)
- {
- if(CtlBtns[CurStyle].CurCtlStart>0)
- SelectObject(hMemDC,hBmpUpLeft);
- else SelectObject(hMemDC,hBmpDsLeft);
- GetObject(hBmpUpLeft,sizeof(bmp),(LPSTR)&bmp);
- BitBlt (hDC, xStart,5,
- bmp.bmWidth,bmp.bmHeight,hMemDC,0,0,SRCCOPY) ;
- xStart +=bmp.bmWidth;
- }
- //if(hBmpCtl[CurStyle])
- {
- SelectObject (hMemDC, CtlBtns[CurStyle].hBmpUpCtl) ;
- GetObject(CtlBtns[CurStyle].hBmpUpCtl,sizeof(bmp),(LPSTR)&bmp);
- BitBlt (hDC, xStart,5,
- (CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)*(bmp.bmHeight-1),
- bmp.bmHeight,hMemDC,
- CtlBtns[CurStyle].CurCtlStart*(bmp.bmHeight-1),0,SRCCOPY) ;
- if(CurSelCtl>=0
- && CurSelCtl<CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)
- {
- SelectObject(hMemDC,CtlBtns[CurStyle].hBmpDnCtl);
- BitBlt (hDC, ctlXStart+CurSelCtl*ctlWidth,5,
- ctlWidth,ctlWidth+1,hMemDC,
- (CurSelCtl+CtlBtns[CurStyle].CurCtlStart)*ctlWidth,0,SRCCOPY) ;
- }
- else CurSelCtl =-1;
- xStart += (CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)*(bmp.bmHeight-1);
- }
- //if(hBmpCtlRight)
- {
- if(CtlBtns[CurStyle].CurCtlEnd<CtlBtns[CurStyle].CurCtlCount)
- SelectObject(hMemDC,hBmpUpRight);
- else SelectObject(hMemDC,hBmpDsRight);
- GetObject(hBmpUpRight,sizeof(bmp),(LPSTR)&bmp);
- BitBlt (hDC, xStart,5,
- bmp.bmWidth,bmp.bmHeight,hMemDC,0,0,SRCCOPY) ;
- }
- DeleteDC (hMemDC) ;
- }
- else if(wParam ==0)
- {
- if(CurSelCtl <0
- || CurSelCtl >=(CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart))
- {
- CurSelCtl =-1;
- return 0L;
- }
- switch(lParam)
- {
- case 0L://hbmp=up
- hBmp =CtlBtns[CurStyle].hBmpUpCtl;
- break;
- case 1L://hbmp=down
- hBmp =CtlBtns[CurStyle].hBmpDnCtl;
- break;
- case 2L: //hbmp=disable
- break;
- case 3L: //left up
- break;
- case 4L: //left down
- break;
- case 5L: //left disable
- break;
- case 6L: //right up
- break;
- case 7L: //right down
- break;
- case 8L: //right disable
- break;
- default:
- return 0L;
- }
- //draw the button
- hDC=GetDC(hWnd);
- hMemDC =CreateCompatibleDC(hDC);
- SelectObject(hMemDC,hBmp);
- if(lParam <3L)
- BitBlt (hDC, ctlXStart+CurSelCtl*ctlWidth,5,
- ctlWidth,ctlWidth+1,hMemDC,
- (CurSelCtl+CtlBtns[CurStyle].CurCtlStart)*ctlWidth,0,SRCCOPY) ;
- DeleteDC(hMemDC);
- ReleaseDC(hWnd,hDC);
- }
- return 0L;
- case WM_SETCURSOR:
- //Set Cursor or Show Hint if in a button or Hide Hint if out a button;
- SetCursor(LoadCursor(NULL,IDC_ARROW));
- isInSlip = FALSE;
- return TRUE;
- case WM_LBUTTONDOWN:
- pt=MAKEPOINT(lParam);
- if(CtlBtns[CurStyle].CurCtlStart>0)
- {
- SetRect(&rc,ctlXStart-ctlWidth/2,5,ctlXStart,5+ctlWidth+1);
- if(PtInRect(&rc,pt))
- {
- isLeft =TRUE;
- SendMessage(hWnd,WM_DRAWBUTTON,0,4L);
- SetCapture(hWnd);
- isCtlMouseDown =TRUE;
- return 0L;
- }
- }
- if(CtlBtns[CurStyle].CurCtlEnd<CtlBtns[CurStyle].CurCtlCount)
- {
- SetRect(&rc,ctlXStart+(CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)*ctlWidth,5,
- ctlXStart+(CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)*ctlWidth+ctlWidth/2,
- 5+ctlWidth+1);
- if(PtInRect(&rc,pt))
- {
- isLeft =FALSE;
- SendMessage(hWnd,WM_DRAWBUTTON,0,7L);
- SetCapture(hWnd);
- isCtlMouseDown =TRUE;
- return 0L;
- }
- }
- for(i=0;i<CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart;i++)
- {
- SetRect(&rc,ctlXStart+i*ctlWidth,5,
- ctlXStart+(i+1)*ctlWidth,5+ctlWidth+1);
- if (PtInRect(&rc, pt))
- break;
- }
- if(i+CtlBtns[CurStyle].CurCtlStart ==CtlBtns[CurStyle].CurCtlEnd) return 0L;
- else
- {
- SendMessage(hWnd,WM_DRAWBUTTON,0,0L);
- CurSelCtl=i;//+CtlBtns[CurStyle].CurCtlStart;
- SendMessage(hWnd,WM_DRAWBUTTON,0,1L);
- lpCurCtlInfo = (CtlInfo far *)&CtlBtns[CurStyle].Btns[CurSelCtl+CtlBtns[CurStyle].CurCtlStart];
- //LoadString(hInst, 11000+CurStyle*100+i,(LPSTR)Cur_Ctl_Style, sizeof(Cur_Ctl_Style));
- }
- return 0L;
- case WM_MOUSEMOVE:
- if(!isCtlMouseDown) break;
- pt=MAKEPOINT(lParam);
- if(isLeft)
- SetRect(&rc,ctlXStart-ctlWidth/2,5,ctlXStart,5+ctlWidth/2+1);
- else
- SetRect(&rc,ctlXStart+(CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)*ctlWidth,5,
- ctlXStart+(CtlBtns[CurStyle].CurCtlEnd-CtlBtns[CurStyle].CurCtlStart)*ctlWidth+ctlWidth/2,
- 5+ctlWidth+1);
- fCtlMouseOut = isCtlMouseOut;
- if(PtInRect(&rc,pt))
- isCtlMouseOut =FALSE;
- else isCtlMouseOut =TRUE;
- if(fCtlMouseOut !=isCtlMouseOut)
- {
- if(isCtlMouseOut)
- {
- if(isLeft)
- SendMessage(hWnd,WM_DRAWBUTTON, 0,3L);
- else SendMessage(hWnd,WM_DRAWBUTTON,0,6L);
- }
- else
- {
- if(isLeft)
- SendMessage(hWnd,WM_DRAWBUTTON,0,4L);
- else SendMessage(hWnd,WM_DRAWBUTTON,0,7L);
- }
- }
- return 0L;
- case WM_LBUTTONUP:
- if(!isCtlMouseDown) break;
- ReleaseCapture();
- isCtlMouseDown =FALSE;
- if(!isCtlMouseOut)
- {
- if(isLeft)
- {
- CtlBtns[CurStyle].CurCtlStart--;
- CtlBtns[CurStyle].CurCtlEnd--;
- if(CurSelCtl >=0)
- CurSelCtl++; /////
- }
- else
- {
- CtlBtns[CurStyle].CurCtlStart++;
- CtlBtns[CurStyle].CurCtlEnd++;
- if(CurSelCtl >=0)
- CurSelCtl--; /////
- }
- InvalidateRect(hWnd,NULL,TRUE);
- }
- else isCtlMouseOut =FALSE;
- return 0L;
- case WM_PAINT:
- GetClientRect(hWnd,&rc);
- BeginPaint(hWnd,&ps);
- hPen = CreatePen(PS_SOLID,1,RGB(80,80,80));
- SelectObject(ps.hdc,hPen);
- //MoveTo(ps.hdc,0,rc.bottom-1);
- MoveTo(ps.hdc,rc.right-1,rc.bottom-1);
- LineTo(ps.hdc,rc.right-1,0);
- SelectObject(ps.hdc,GetStockObject(WHITE_PEN));
- DeleteObject(hPen);
- //MoveTo(ps.hdc,0,1);
- LineTo(ps.hdc,0,0);
- LineTo(ps.hdc,0,rc.bottom-1);
- SendMessage(hWnd,WM_DRAWBUTTON,(WPARAM)(HDC)ps.hdc,(LONG)MAKELONG(20,rc.bottom));
- EndPaint(hWnd,&ps);
- break;
- case WM_DESTROY:
- for(i=0;i<2;i++)
- {
- if(CtlBtns[i].hBmpUpCtl) DeleteObject(CtlBtns[i].hBmpUpCtl);
- if(CtlBtns[i].hBmpDnCtl) DeleteObject(CtlBtns[i].hBmpDnCtl);
- if(CtlBtns[i].hBmpDsCtl) DeleteObject(CtlBtns[i].hBmpDsCtl);
- }
- if(hBmpUpLeft) DeleteObject(hBmpUpLeft);
- if(hBmpUpRight) DeleteObject(hBmpUpRight);
- if(hBmpDnLeft) DeleteObject(hBmpDnLeft);
- if(hBmpDnRight) DeleteObject(hBmpDnRight);
- if(hBmpDsLeft) DeleteObject(hBmpDsLeft);
- if(hBmpDsRight) DeleteObject(hBmpDsRight);
- if(hBmpArrow) DeleteObject(hBmpArrow);
- break;
- default:
- break;
- }
- return (DefWindowProc(hWnd, message, wParam, lParam));
- }