WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Mouse Drag and Highlight - Display Image


Question:

Not sure if I've posted this question before, but is it possible to have a program prompt a user to select an area of a window. Meaning, can Winbatch tell someone to click the upper left area of a window and then Drag to the lower right and show the highlighted area that the user has selected, by creating a box around the selected area? Similar to what you might see when editing a picture in some picture editing program.

What I am trying to do is allow for a user to select any window/program and then drag and highlight a section of that window and then save that section by using the snapshot and the pixie extender.

Answer:

WinBatch has no built in functionality, to handle this.

However using API calls it can be done for a BoxesUp window and possibly for a dynamic dialog. Here's a possible solution, with the selection code worked out.

;Written By George
; LOADIMAGE.WBT (6KB) 
#definefunction udfPlace(cX, cY, Units, Quad)
   select Units
      case 0   ; Pixels
         scrX = winmetrics(0) 
         scrY = winmetrics(1) 
         break
         
      case 1   ; Virtual
         scrX = 1000
         scrY = 1000
         break
         
      case 2   ; Dialog
         scrX = winmetrics(0)/winmetrics(-6)
         scrY = winmetrics(1)/winmetrics(-5)
         break

   endselect
   
   if cX>scrX then cX = scrX
   if cY>scrY then cY = scrY
   xQuad = scrX/2
   yQuad = scrY/2
   select Quad
      case 0   ; Center
         X1 = (scrX-cX)/2
         Y1 = (scrY-cY)/2
         X2 = X1+cX
         Y2 = Y1+cY
         break
         
      case 1   ; Upper right
         if cX>xQuad
            X1 = scrX-cX
            X2 = scrX
         else
            X1 = xQuad + (xQuad-cX)/2
            X2 = X1+cX
         endif   

         if cY>yQuad
            Y1 = 0
            Y2 = cY
         else
            Y1 = (yQuad-cY)/2
            Y2 = Y1+cY
         endif   
         break
         
      case 2   ; Upper left
         if cX>xQuad
            X1 = 0
            X2 = cX
         else
            X1 = (xQuad-cX)/2
            X2 = X1+cX
         endif   

         if cY>yQuad
            Y1 = 0
            Y2 = cY
         else
            Y1 = (yQuad-cY)/2
            Y2 = Y1+cY
         endif   
         break

      case 3   ; Lower left
         if cX>xQuad
            X1 = 0
            X2 = cX
         else
            X1 = (xQuad-cX)/2
            X2 = X1+cX
         endif   

         if cY>yQuad
            Y1 = scrY-yQuad
            Y2 = scrY
         else
            Y1 = yQuad + (yQuad-cY)/2
            Y2 = Y1+cY
         endif   
         break

      case 4   ; Lower right
         if cX>xQuad
            X1 = scrX-cX
            X2 = scrX
         else
            X1 = xQuad + (xQuad-cX)/2
            X2 = X1+cX
         endif   

         if cY>yQuad
            Y1 = scrY-yQuad
            Y2 = scrY
         else
            Y1 = yQuad + (yQuad-cY)/2
            Y2 = Y1+cY
         endif   
         break

   endselect
   
   return strcat(X1, ',', Y1, ',', X2, ',', Y2)
            
#endfunction

#definefunction udfPix2Virt(Pixels, IsX)
   if IsX then return Pixels*1000/winmetrics(0)
   return Pixels*1000/winmetrics(1)
#endfunction


; From the help file.
#definefunction udfSnapShot(File)
;returns the size of buffer needed for a subsequent BinaryAlloc, 
;but doesn't attempt to place the contents of clipboard into a buffer
size=BinaryClipGet(0,8)

;allocates a data buffer
bb=BinaryAlloc(size)

;read file format type CF_DIB
BinaryClipGet(bb,8)

; need to add first 14 bytes to make it a BMP file format
bmpdatasize=14
bb2=BinaryAlloc(size + bmpdatasize)

;The characters identifying the bitmap.
;'BM' - Windows 3.1x, 95, NT, ...
BinaryPokeStr(bb2, 0, "BM") 
;Complete file size in bytes.
BinaryPoke4(bb2, 2, size+bmpdatasize)

;Reserved
BinaryPoke4(bb2,6,0)

;Data offset
headersize=BinaryPeek4(bb,0)
dataoffset = headersize+bmpdatasize

BinaryPoke4(bb2,10,dataoffset)
BinaryCopy(bb2,bmpdatasize,bb,0,size)
BinaryWrite(bb2, File)
BinaryFree(bb)
BinaryFree(bb2)

#endfunction

;------------------------------------------------------------------------------------------
; Main
addextender("wwimg34i.dll")
intcontrol(12, 4, 0, 0, 0)

WbtDir = filepath(IntControl(1004, 0, 0, 0, 0))
dirchange(WbtDir)

User32 = strcat(dirwindows(1), 'User32.dll')
Gdi32 = strcat(dirwindows(1), 'Gdi32.dll')
Image = '%WbtDir%ss.bmp'
NewImage = '%WbtDir%crop.bmp'

RECT = binaryalloc(16)
hWnd = dllhwnd('')
hDC = dllcall(User32, long:"GetDC", long:hWnd)

Wins = WinItemize()
AskList = ''
aCnts = itemcount(Wins, @tab)
for aCnt = 1 to aCnts
   Win = itemextract(aCnt, Wins, @tab)
   if (winstate(Win)!=-1 && winstate(Win)!=1)
      if !itemlocate(Win, AskList, @tab) then AskList = iteminsert(Win, -1, AskList, @tab)
   endif
next
trgWnd = AskItemList('Select Window', AskList, @tab, @sorted, @single)

winactivate(trgWnd)
ss = snapshot(3)
udfSnapShot(Image)

adjX = 2*winmetrics(7)
adjY = winmetrics(4) + 2*winmetrics(6)
XX = udfPix2Virt(itemextract(1, imginfo(Image), @tab), @true)
YY = udfPix2Virt(itemextract(2, imginfo(Image), @tab), @false)

boxesup(udfPlace(XX+adjX, YY+adjY, 1, 0), @normal)
boxbitmap(1, '0,0,1000,1000', Image, 3)

Drawing = @false
lastPt = 0

   while mouseinfo(4)!=4
   endwhile
   Pt1 = mouseinfo(5)

   while mouseinfo(5)==pt1
   endwhile

   while mouseinfo(4)==4
      pt2 = mouseinfo(5)
      ; Erase the last rectangle.
      if Drawing && pt2!=lastPt then dllcall(User32, long:"DrawFocusRect", long:hDC, lpbinary:RECT)
      if pt2!=lastPt
         X1 = winmetrics(0)*min(itemextract(1, pt1, ' '), itemextract(1, pt2, ' '))/1000
         Y1 = winmetrics(1)*min(itemextract(2, pt1, ' '), itemextract(2, pt2, ' '))/1000
         X2 = winmetrics(0)*max(itemextract(1, pt1, ' '), itemextract(1, pt2, ' '))/1000
         Y2 = winmetrics(1)*max(itemextract(2, pt1, ' '), itemextract(2, pt2, ' '))/1000
         binarypoke4(RECT, 0, X1)
         binarypoke4(RECT, 4, Y1)
         binarypoke4(RECT, 8, X2)
         binarypoke4(RECT,12, Y2)

         dllcall(User32, long:"DrawFocusRect", long:hDC, lpbinary:RECT)
         Drawing = @true
         lastPt = Pt2
      endif
   endwhile
        
ImgCrop(Image, NewImage, X2-X1, Y2-Y1, 0, 0)
boxdrawrect(1, '0,0,1000,1000', 2)
boxbitmap(1, udfPlace(X2-X1, Y2-Y1, 1, 0), NewImage, 3)


display(10, "DrawFocusRect", "All done!")

:Cancel
if isdefined(RECT) then binaryfree(RECT)
if isdefined(hDC) then dllcall(User32, long:"ReleaseDC", long:hWnd, long:hDC)

boxdestroy(1)

Article ID:   W16479
File Created: 2005:02:18:12:20:54
Last Updated: 2005:02:18:12:20:54