DISP.C
上传用户:s6549606
上传日期:2015-11-11
资源大小:12002k
文件大小:8k
- #include <windows.h>
- ////////////////////////////////////////////////////////////////
- #define SCROLL_RATIO 50
- //---------------------------------------------------------------------
- void FitNewSize (HWND hWnd);
- void SizeProc (HWND hWnd);
- void ScrollProc (HWND hWnd, int message, WORD wPos, WORD wScrollType);
- void DisProc(HWND hWnd,HGLOBAL hbi);
- //---------------------------------------------------------------------
- extern int imgheight,imgwidth;
- extern BOOL HSCROLL,VSCROLL;
- //---------------------------------------------------------------------
- void FitNewSize (HWND hWnd)
- {
- RECT DeskRect,rect;
- int NewX,NewY,NewWidth,NewHeight;
- int HBorder,VBorder;
- POINT pt;
- int cxWindow,cyWindow,cxRange,cyRange;
- GetWindowRect(GetDesktopWindow(), &DeskRect);
-
- HBorder=GetSystemMetrics (SM_CXBORDER)/2;
- VBorder=GetSystemMetrics (SM_CYBORDER)/2+GetSystemMetrics (SM_CYMENU)+GetSystemMetrics (SM_CYCAPTION);
-
- if((imgwidth+HBorder)<(DeskRect.right-DeskRect.left))
- {
- NewWidth=imgwidth+HBorder;
- NewX=( (DeskRect.right-DeskRect.left) - (imgwidth+HBorder) ) /2;
- HSCROLL=FALSE;
- }
- else
- {
- NewX=(DeskRect.right-DeskRect.left)/10;
- NewWidth=NewX*8;
- HSCROLL=TRUE;
- }
-
- if((imgheight+VBorder)<(DeskRect.bottom-DeskRect.top))
- {
- NewHeight=imgheight+VBorder;
- NewY=( (DeskRect.bottom-DeskRect.top) - (imgheight+VBorder) ) /2;
- VSCROLL=FALSE;
- }
- else
- {
- NewY=(DeskRect.bottom-DeskRect.top)/10;
- NewHeight=NewY*8;
- VSCROLL=TRUE;
- }
-
- if(HSCROLL)
- {
- NewHeight+= GetSystemMetrics (SM_CYHSCROLL);
- NewY-=GetSystemMetrics (SM_CYHSCROLL)/2;
- }
- if(VSCROLL)
- {
- NewWidth+= GetSystemMetrics (SM_CXVSCROLL);
- NewX-=GetSystemMetrics (SM_CXVSCROLL)/2;
- }
-
- SendMessage(GetDesktopWindow(),WM_PAINT,0,0);
-
- GetWindowRect(hWnd,(LPRECT)&DeskRect);
- pt.x=DeskRect.left-1;
- pt.y=DeskRect.top-1;
- MoveWindow(hWnd,NewX,NewY,NewWidth,NewHeight,TRUE);
- SendMessage(WindowFromPoint(pt),WM_PAINT,0,0);
- GetClientRect(hWnd,(LPRECT)&rect);
-
- cxWindow = rect.right - rect.left;
- cyWindow = rect.bottom - rect.top;
- cxRange = imgwidth - cxWindow - 1;
- cyRange = imgheight - cyWindow - 1;
-
- if(HSCROLL)
- {
- SetScrollRange (hWnd, SB_HORZ, 0, cxRange, FALSE);
- SetScrollPos(hWnd,SB_HORZ,0,TRUE);
- }
- else
- SetScrollRange (hWnd, SB_HORZ, 0, 0, FALSE);
- if(VSCROLL)
- {
- SetScrollRange (hWnd, SB_VERT, 0, cyRange, FALSE);
- SetScrollPos(hWnd,SB_VERT,0,TRUE);
- }
- else
- SetScrollRange (hWnd, SB_VERT, 0, 0, FALSE);
- }
- ////////////////////////////////////////////////////////////////////////
- /*
- void SizeProc(HWND hWnd)
- {
- RECT ClientRect;
- BOOL scrolled=FALSE;
-
- GetClientRect (hWnd, (LPRECT)&ClientRect);
-
- if(imgwidth<=(ClientRect.right-ClientRect.left))
- SetScrollRange (hWnd, SB_HORZ, 0, 0, TRUE);
- else
- {
- SetScrollRange (hWnd, SB_HORZ, 0, 100, TRUE);
- scrolled=TRUE;
- }
-
- if(imgheight<=(ClientRect.bottom-ClientRect.top))
- SetScrollRange (hWnd, SB_VERT, 0, 0, TRUE);
- else
- {
- SetScrollRange (hWnd, SB_VERT, 0, 100, TRUE);
- scrolled=TRUE;
- }
- if(scrolled)
- {
- InvalidateRect(hWnd,NULL,FALSE);
- UpdateWindow(hWnd);
- }
- }
- */
- ///////////////////////////////////////////////////////////////////////////
- void ScrollProc (HWND hWnd, int message, WORD wPos, WORD wScrollType)
- {
- int xBar;
- int nMin;
- int nMax;
- int dx;
- int nOneUnit;
- int cxClient;
- int nHorzOrVert;
- RECT rect;
-
- GetClientRect (hWnd, &rect);
- if (message == WM_HSCROLL)
- {
- nHorzOrVert = SB_HORZ;
- cxClient = rect.right - rect.left;
- }
- else
- {
- nHorzOrVert = SB_VERT;
- cxClient = rect.bottom - rect.top;
- }
- nOneUnit = cxClient / SCROLL_RATIO;
- if (!nOneUnit)
- nOneUnit = 1;
- xBar = GetScrollPos (hWnd, nHorzOrVert);
- GetScrollRange (hWnd, nHorzOrVert, &nMin, &nMax);
- switch (wScrollType)
- {
- case SB_LINEDOWN: // One line right.
- dx = nOneUnit;
- break;
- case SB_LINEUP: // One line left.
- dx = -nOneUnit;
- break;
- case SB_PAGEDOWN: // One page right.
- dx = cxClient/4;
- break;
- case SB_PAGEUP: // One page left.
- dx = -cxClient/4;
- break;
- case SB_THUMBPOSITION: // Absolute position.
- dx = wPos - xBar;
- break;
- default: // No change.
- dx = 0;
- break;
- }
- if (dx)
- {
- xBar += dx;
- if (xBar < nMin)
- {
- dx -= xBar-nMin;
- xBar = nMin;
- }
- if (xBar > nMax)
- {
- dx -= xBar - nMax;
- xBar = nMax;
- }
- if (dx)
- {
- SetScrollPos (hWnd, nHorzOrVert, xBar, TRUE);
- if (nHorzOrVert == SB_HORZ)
- ScrollWindow (hWnd, -dx, 0, NULL, NULL);
- else
- ScrollWindow (hWnd, 0, -dx , NULL, NULL);
-
- UpdateWindow (hWnd);
- }
- }
- }
- //---------------------------------------------------------------------
- void DisProc(HWND hWnd,HGLOBAL hbi)
- {
- HDC hDC;
- PAINTSTRUCT ps;
- int xScroll, yScroll;
- int dx,dy;
- RECT rectClient, rc;
- LPSTR lpDIBHdr, lpDIBBits;
- if (!hbi)
- return;
- hDC = BeginPaint (hWnd, &ps);
-
- xScroll = GetScrollPos (hWnd, SB_HORZ);
- yScroll = GetScrollPos (hWnd, SB_VERT);
- GetClientRect (hWnd, &rectClient);
- rc.left = xScroll;
- rc.top = yScroll;
- rc.right = xScroll + rectClient.right - rectClient.left;
- rc.bottom = yScroll + rectClient.bottom - rectClient.top;
-
- if (rc.right > imgwidth)
- {
- dx = imgwidth - rc.right;
- rc.right += dx;
- rectClient.right+=dx;
- }
- if (rc.bottom > imgheight)
- {
- dy = imgheight - rc.bottom;
- rc.bottom += dy;
- rectClient.bottom+=dy;
- }
- lpDIBHdr = GlobalLock (hbi);
- lpDIBBits = lpDIBHdr+sizeof(BITMAPINFOHEADER);
-
- SetStretchBltMode (hDC, COLORONCOLOR);
-
- SetDIBitsToDevice (hDC,
- rectClient.left,
- rectClient.top,
- rectClient.right-rectClient.left,
- rectClient.bottom-rectClient.top,
- rc.left,
- imgheight-rc.bottom,
- 0,
- imgheight,
- (char far *)lpDIBBits,
- (LPBITMAPINFO) lpDIBHdr,
- DIB_RGB_COLORS);
- GlobalUnlock (hbi);
- EndPaint (hWnd, &ps);
- /*
- HDC hDC;
- PAINTSTRUCT ps;
- int xScroll, yScroll;
- RECT rectClient, rc;
- LPSTR lpDIBHdr, lpDIBBits;
- int rectw,recth;
- if (!hbi)
- return;
-
- xScroll=yScroll=0;
- GetClientRect (hWnd, &rectClient);
- rectw=rectClient.right-rectClient.left;
- recth=rectClient.bottom-rectClient.top;
- hDC = BeginPaint (hWnd, &ps);
-
- if(HSCROLL)
- {
- xScroll = GetScrollPos (hWnd, SB_HORZ);
- }
-
- if(VSCROLL)
- {
- yScroll = GetScrollPos (hWnd, SB_VERT);
- }
- rc.right = rc.left+rectw;
- rc.bottom = rc.top+recth;
- lpDIBHdr = GlobalLock (hbi);
- lpDIBBits = lpDIBHdr+sizeof(BITMAPINFOHEADER);
-
- SetStretchBltMode (hDC, COLORONCOLOR);
-
- SetDIBitsToDevice (hDC,
- rectClient.left,
- rectClient.top,
- rectClient.right-rectClient.left,
- rectClient.bottom-rectClient.top,
- rc.left,
- imgheight-rc.bottom,
- 0,
- imgheight,
- (char far *)lpDIBBits,
- (LPBITMAPINFO) lpDIBHdr,
- DIB_RGB_COLORS);
- GlobalUnlock (hbi);
- EndPaint (hWnd, &ps);
- */
- }