Editor Gadget Background Image? Or Transparency?

Just starting out? Need help? Post your questions and find answers here.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Editor Gadget Background Image? Or Transparency?

Post 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?
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
PWS32
User
User
Posts: 85
Joined: Sat May 10, 2003 1:02 pm
Location: Germany

Post by PWS32 »

Hi Sparkie,

have you a codeexample ?

Best regards,
Peter
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Same here, looking for a code example, as your explaination lost me some where in there :)
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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 
Last edited by Sparkie on Fri Dec 14, 2007 11:02 pm, edited 1 time in total.
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
PWS32
User
User
Posts: 85
Joined: Sat May 10, 2003 1:02 pm
Location: Germany

Post by PWS32 »

Great !!!! many thanks !
Post Reply