Page 1 of 1

MessageRequesterEx(): spice up your messagerequester a bit

Posted: Thu Jan 10, 2008 5:43 am
by netmaestro
Here we make a PB messagerequester accept a background color or optional background image, custom text color and screen position. Usage is easy, just fill a structure as shown and call it. Could have bugs or leaks, I wrote it pretty quickly.

Code: Select all

;================================================================= 
; Program:           MessageRequesterEx library command 
; Author:            Lloyd Gallant (netmaestro) 
; Date:              January 9, 2008 
; Target OS:         Microsoft Windows All 
; Target Compiler:   PureBasic 4.0 and later 
; License:           Free, unrestricted, no warranty 
;================================================================= 

Enumeration 
  #MB_COLOR 
  #MB_IMAGE 
EndEnumeration 

Structure MBDATA 
  x.l             ; In, desired x position on screen 
  y.l             ; In, desired y position on screen 
  bkContent.l     ; In, long value representing imageid or colorref depending on type 
  bkContentType.l ; In, specifies whether bkContent is an image or a colorref 
  txtColor.l      ; In, specifies desired text color for message box 
  staticBrush.l   ; reserved, do not use 
  bkImageID.l     ; reserved, do not use 
  cx.l            ; reserved, do not use 
  cy.l            ; reserved, do not use 
EndStructure 

Global hook, mbd.MBDATA 

Procedure dlgProc(hwnd, msg, wparam, lparam) 
  oldproc = GetProp_(hwnd, "oldproc") 
  Select msg 
    Case #WM_CTLCOLORSTATIC 
      SetBkMode_(wparam, #TRANSPARENT) 
      SetTextColor_(wParam, mbd\txtColor) 
      ProcedureReturn mbd\staticBrush 
    Case #WM_NCDESTROY 
      RemoveProp_(hwnd, "oldproc") 
      DeleteObject_(mbd\staticBrush) 
      FreeImage(mbd\bkImageID) 
  EndSelect 
  ProcedureReturn CallWindowProc_(oldproc, hwnd, msg, wparam, lparam) 
EndProcedure 

Procedure MBProc(nCode, wParam, lParam) 
  Static hwndmain 
  Select nCode 
    Case #HCBT_CREATEWND 
      *pcbt.CBT_CREATEWND = lParam 
      *pcs.CREATESTRUCT = *pcbt\lpcs 
      Select *pcs\lpszClass 
        Case 32770    
          hwndmain = wParam 
          *pcs\x = mbd\x    
          *pcs\y = mbd\y    
          mbd\cx = *pcs\cx - 2*GetSystemMetrics_(#SM_CXFIXEDFRAME) 
          mbd\cy = *pcs\cy - 2*GetSystemMetrics_(#SM_CYFIXEDFRAME)-GetSystemMetrics_(#SM_CYCAPTION) 
          SetProp_(wparam, "oldproc", SetWindowLong_(wparam,#GWL_WNDPROC,@dlgProc())) 
      EndSelect 
    Case #HCBT_ACTIVATE 
      If wParam = hwndmain 
        Select mbd\bkContentType 
          Case #MB_COLOR 
            img = CreateImage(#PB_Any, mbd\cx, mbd\cy, 32) 
            StartDrawing(ImageOutput(img)) 
              Box(0,0,mbd\cx, mbd\cy, mbd\bkContent) 
            StopDrawing() 
            mbd\bkImageID = img 
          Case #MB_IMAGE 
            ResizeImage(mbd\bkContent,mbd\cx,mbd\cy) 
            mbd\bkImageID = mbd\bkContent 
        EndSelect 
        CreateGadgetList(wParam) 
        ig = ImageGadget(#PB_Any, 0,0,0,0,ImageID(mbd\bkImageID)) 
        SetWindowPos_(GadgetID(ig), #HWND_BOTTOM,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE) 
        SetWindowLong_(GadgetID(ig),#GWL_STYLE,GetWindowLong_(GadgetID(ig),#GWL_STYLE)|#WS_CLIPSIBLINGS) 
        DisableGadget(ig, #True) 
        mbd\staticBrush = CreatePatternBrush_(ImageID(mbd\bkImageID)) 
        ProcedureReturn 0 
      EndIf 
  EndSelect 
  ProcedureReturn CallNextHookEx_(hook, nCode, wParam, lParam) 
 EndProcedure 

Procedure MessageRequesterEx(title$, text$, flags=0) 
  hook = SetWindowsHookEx_(#WH_CBT, @MBProc(), #Null, GetCurrentThreadId_()) 
  result = MessageRequester(title$, text$, flags) 
  UnhookWindowsHookEx_(hook) 
  ProcedureReturn result 
EndProcedure 
And a little test prog:

Code: Select all

IncludeFile "MessageRequesterEx.pbi"

; Make a colored message box at 300,300

With mbd
  \x = 300
  \y = 300
  \bkContent = #White
  \bkContentType = #MB_COLOR
  \txtColor = #Blue
EndWith
MessageRequesterEx("Notice", "The rain in Spain falls mainly on the plain")

; make an image-background message box at 100,100

CreateImage(2, 220,80,32)
StartDrawing(ImageOutput(2))
  Box(0,0,220,80,#White)
  Circle(60,40,40,RGB(225,255,225))
  Circle(150,40,40,RGB(225,255,225))
StopDrawing()

With mbd
  \x = 100
  \y = 100
  \bkContent = 2
  \bkContentType = #MB_IMAGE
  \txtColor = #Blue
EndWith
MessageRequesterEx("Notice", "The rain in Spain falls mainly on the plain")

Posted: Thu Jan 10, 2008 5:47 am
by Dare
Nifty!

Posted: Thu Jan 10, 2008 12:47 pm
by rsts
Cool.

I like that part about the rain, too. :)

cheers

umm - flags?

Posted: Thu Jan 10, 2008 12:57 pm
by netmaestro
Thanks! Flags is one of the parameters for the standard requester, it's not one of mine. It's needed for stuff like #PB_MessageRequester_YesNo options, etc.

Posted: Thu Jan 10, 2008 1:18 pm
by Fangbeast
Is it easy to add your own image? I don't know anything about image functions (as you can see by my crappy interfaces).

Just curious.

Posted: Thu Jan 10, 2008 3:11 pm
by netmaestro
Sure, just make a datasection and use IncludeBinary for the image and then do a CatchImage to get it usable. The resulting #image can then be fed to the structure in bkContent, specify #MB_IMAGE for bkContentType and you're off and running.

A note about images, you will want to space out your text for shorter messages that will be using the same image as longer ones to avoid the image getting resized too far out of proportion. The function will resize it automatically to fit the requester, but the closer it is to its original size, the better it's going to look.

Posted: Thu Jan 10, 2008 10:48 pm
by Fangbeast
Thank you netmaestro. When a bit of a cool change comes tonight, I can try it. Right now though, I am in danger of the equipment blowing up and me becoming sir crispy bits.

Posted: Sun Dec 21, 2008 10:19 pm
by yrreti
Hi netmaestro

If you've got a few minutes. I'm having a problem using the code above with PB4.3 in the Procedure MBProc(nCode, wParam, lParam).
And being the Wizard you are, you could probably come up with an easy solution.
It works fine with PB4.20, but gives the error:
[ERROR]There is no current GadgetList
on the second line shown in the code below.

I know that CreateGadgetList is now not needed. But it still gives the error with or without it being commented out.

Code: Select all

;CreateGadgetList(wParam) ;now depreciated.
ig = ImageGadget(#PB_Any, 0,0,0,0,ImageID(mbd\bkImageID)) ;errors out
I'd like to be able to use this code in my programs, as I like the fact that I can have a white background instead
of the normal drab gray one that the normal MessageRequestor has.
I know I could just do the whole program in PB4.20. But as PB4.3 is now the one being used primarily, it makes
sense to keep my code up to date to avoid future problems.
Thank you very much for this code and any suggestions you might have to solve this.

Posted: Sun Dec 21, 2008 10:51 pm
by LuCiFeR[SD]
yrreti wrote:Hi netmaestro

If you've got a few minutes. I'm having a problem using the code above with PB4.3 in the Procedure MBProc(nCode, wParam, lParam).
And being the Wizard you are, you could probably come up with an easy solution.
It works fine with PB4.20, but gives the error:
[ERROR]There is no current GadgetList
on the second line shown in the code below.

I know that CreateGadgetList is now not needed. But it still gives the error with or without it being commented out.

Code: Select all

;CreateGadgetList(wParam) ;now depreciated.
ig = ImageGadget(#PB_Any, 0,0,0,0,ImageID(mbd\bkImageID)) ;errors out
I'd like to be able to use this code in my programs, as I like the fact that I can have a white background instead
of the normal drab gray one that the normal MessageRequestor has.
I know I could just do the whole program in PB4.20. But as PB4.3 is now the one being used primarily, it makes
sense to keep my code up to date to avoid future problems.
Thank you very much for this code and any suggestions you might have to solve this.
Change CreateGadgetList(wParam) to UseGadgetList(wParam)

Posted: Sun Dec 21, 2008 11:44 pm
by yrreti
That was the ticket! :D

Thanks very much LuCiFeR[SD]

Posted: Mon Dec 22, 2008 12:38 am
by netmaestro
This has been pretty much superseded by ManagedMessageBox 2.0 found here: http://www.purebasic.fr/english/viewtopic.php?t=32553

Posted: Tue Dec 23, 2008 1:37 pm
by yrreti
Hi netmaestro

Sorry I couldn't answer back right away, but thanks for the referral to
ManagedMessageBox 2.0. I had tried using that one also. But it hadn't
been upgraded for 4.3 at the time, and so I had tried using this one instead.
But ManagedMessageBox 2.0 works fine now too, and really looks nice.
I like using this code in my programs, as I like the fact that I can have a
white background instead of the normal drab gray one that the normal
MessageRequestor has.

Thank you for sharing this nice code with us.