Page 1 of 1

Editor Gadget Background Image? Or Transparency?

Posted: Sat Oct 02, 2004 7:03 am
by Shannara
Does anybody know how know to either give the Editor Gadget a background image, or maybe have the gadget transparent so only the text will show?

Posted: Sun Oct 03, 2004 12:16 am
by Sparkie
It's not as easy as a transparent StringGadget as explained in this related thread, but it is doable. :)

Here's the skeleton of what works for me.

1. Create a ContainerGadget and CreatePatternBrush_() for it's background image (remember to DeleteObject_() on program End).
2. Create the EditorGadget as a child of the ContainerGadget
3. Create a WindowCallback() procedure. Here you catch the #WM_COMMAND wParam and look for the #EN_VSCROLL and #EN_CHANGE notifications, then do the InvalidateRect_() on both gadgets.
4. subclass the EditorGadget to catch #WM_VSCROLL and do the InvalidateRect_() on the ContainerGadget.

Here's a sample.

Image

Posted: Tue Oct 05, 2004 11:24 am
by PWS32
Hi Sparkie,

have you a codeexample ?

Best regards,
Peter

Posted: Wed Oct 06, 2004 2:09 am
by Shannara
Same here, looking for a code example, as your explaination lost me some where in there :)

Posted: Wed Oct 06, 2004 4:20 am
by Sparkie
**** Edited to make code PB 4.xx compatibale ****

I've been able to modify the code a bit to where the WindowCallback is no longer needed. I dug out the key parts from my original code and quickly tested it on WinXPhome with PB 3.91.

Code: Select all

UseJPEGImageDecoder() 

Global OldCallback 

; --> Get our background image 
imageBG$ = (OpenFileRequester("Select background", "c:\", "Image files (JPG, JPEG, BMP)|*.jpg;*.bmp", 0)) 

If imageBG$ <> "" 
  myImage = LoadImage(0, imageBG$) 
  ; --> create brush for our window background 
  containerBrush = CreatePatternBrush_(ImageID(0)) 
Else 
  ; --> no image selected so we use a hatched brush 
  myBrush.LOGBRUSH 
  myBrush\lbStyle = #BS_HATCHED 
  myBrush\lbColor = RGB(225, 255, 200) 
  myBrush\lbHatch = #HS_DIAGCROSS 
  containerBrush = CreateBrushIndirect_(myBrush) 
EndIf 

Procedure.l myScrollCallback(hwnd, msg, wParam, lParam) 
  
  result = CallWindowProc_(OldCallback, hwnd, msg, wParam, lParam) 
  
  Select msg 
    
    Case #WM_VSCROLL 
      ; --> Invalidate ContainerGadget using 
      ;     EditorGadget area, minus scrollbar 
      edRect.RECT\left = GadgetX(1) 
      edRect\right = GadgetX(1)+ GadgetWidth(1)-20 
      edRect\top = GadgetY(1) 
      edRect\bottom = (GadgetY(1)+ GadgetHeight(1)) 
      InvalidateRect_(GadgetID(0), edRect, 1) 
      
    Case #WM_KEYDOWN 
      ; --> Any character other than RETURN 
      ;      we only invalidate local caret area 
      If wParam <> #VK_RETURN 
        GetCaretPos_(@caretPos.POINT) 
        edRect.RECT\left = caretPos\x-3 
        edRect\right = caretPos\x+10 
        edRect\top = caretPos\y-3 
        edRect\bottom = caretPos\y+10 
        InvalidateRect_(GadgetID(0), edRect, 1) 
      Else 
        ; --> otherwise we invalidate the entire background 
        ;     because the RETURN key may be causing a scroll 
        ;     not picked up in our callback. Still looking for 
        ;     a better way to process this. 
        InvalidateRect_(GadgetID(0), 0, 1) 
      EndIf 
      
  EndSelect 
  
  ProcedureReturn result 
EndProcedure 

If OpenWindow(0, 0, 0, 500, 400, "EditorGadget w/Background Image", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
  
  ContainerGadget(0, 10, 10, 480, 380) 
  hEditor1 = EditorGadget(1, 0, 0, 480, 380) 
  CloseGadgetList() 
  
  ; --> subclass our EditorGadget for catching events 
  OldCallback = SetWindowLong_(hEditor1, #GWL_WNDPROC, @myScrollCallback()) 
  
  ; --> Set our background 
  SetClassLong_(GadgetID(0), #GCL_HBRBACKGROUND, containerBrush) 
  
  ; --> word wrap EditorGadget 
  SendMessage_(hEditor1, #EM_SETTARGETDEVICE, #Null, 0) 
  
  ; --> request EditorGadget to send messages 
  SendMessage_(hEditor1, #EM_SETEVENTMASK, 0, #EN_SELCHANGE |#ENM_CHANGE | #ENM_SCROLL) 
  
  ; --> make EditorGadget transparent 
  style = GetWindowLong_(hEditor1, #GWL_EXSTYLE) 
  newstyle = style | #WS_EX_TRANSPARENT 
  SetWindowLong_(hEditor1, #GWL_EXSTYLE, newstyle) 
  
  ; --> refresh EditorGadget on startup 
  InvalidateRect_(GadgetID(0), 0, 1) 
  UpdateWindow_(WindowID(0)) 
  
  Repeat 
    event = WaitWindowEvent() 
  Until event = #PB_Event_CloseWindow 
  DeleteObject_(containerBrush) 
EndIf 

Posted: Wed Oct 06, 2004 8:22 am
by PWS32
Great !!!! many thanks !