FB II Compiler

PG PRO

Debugging

Memory

System

Mathematics

Resources

Disk I/O

Windows

Controls

Menus

Mouse

Keyboard

Text

Fonts

Drawing

Sound

Clipboard

Printing

Communication

ASM

Made with FB

DRAWING

Center a PICT


In rare case, but sometimes pict's rect TL is not alway (0,0) , you know, so you'd better use CALL OFFSETRECT to make sure pict's rect TL set to (0,0). I just re-write your code. (NOT TESTED!)
This routine center the image (when window is larger than image, if not, the image will be cutted in window's size) and paint background in gray checker pattern. This designed for the window which has vertical and horizontal scroll bars, and if you want to sync them, add oX or oY to BUTTON value.

Osamu Shigematsu

LOCAL
"Lt-Gray"
  ` dc.w $9999,$9999,$9999
"Dk-Gray"
  ` dc.w $6666,$6666,$6666
LOCAL FN centerpict(pict&)
  DIM pRect;0,pT,pL,pB,pR
  DIM wRect;8
  DIM fRect;8
  DIM oldRgn&,wndRgn&,picRgn&
  pRect;8 = @pict&..picFrame.top% ' get the rectangle of pict handle
  CALL OFFSETRECT (pRect,-pL,-pT) ' make TL to zero
  oX = (WINDOW(_width) -1 - pR) / 2 ' calc horz offset as oX
  oY = (WINDOW(_height) -1 - pB) / 2 ' calc vert offset as oY
  CALL OFFSETRECT (pRect,oX,oY) ' center the image
  oldRgn& = FN NEWRGN
  CALL GETCLIP(oldRgn&) ' save old clip region for restore
  CALL SETRECT (wRect,0,0,WINDOW(_width) - 1,WINDOW(_height) - 1)
  CALL CLIPRECT (wRect) ' set new clip to windows content
  fRect;8 = @pRect
  CALL INSETRECT (fRect,-1,-1) ' inset to draw frame
  CALL FORECOLOR(_blackColor) ' reset colors and pen
  CALL BACKCOLOR(_whiteColor)
  CALL PENNORMAL
  CALL FRAMERECT(fRect) ' Draw frame
'
  CALL RGBFORECOLOR (#LINE "Lt-Gray")
  CALL RGBBACKCOLOR (#LINE "Dk-Gray")
  wndRgn& = FN NEWRGN
  picRgn& = FN NEWRGN
  CALL RECTRGN (wndRgn&,wRect%)
  CALL RECTRGN (picRgn&,fRect%)
  CALL DIFFRGN (wndRgn&,picRgn&,picRgn&) ' Calculate background region
  CALL DISPOSERGN (wndRgn&)
  CALL FILLRGN (picRgn&,#[REGISTER(A5)] + _gray + 4) ' fill it
  CALL DISPOSERGN (picRgn&)
  CALL FORECOLOR (_blackColor)
  CALL BACKCOLOR (_whiteColor)
'
  CALL DRAWPICTURE(pict&,pRect) 'Draw image to the pRect
  CALL SETCLIP (oldRgn&)
  CALL DISPOSERGN (oldRgn&)
  CALL VALIDRECT(wRect) ' Now Updated the window!
END FN