MessageRequesterEx(): spice up your messagerequester a bit

Share your advanced PureBasic knowledge/code with the community.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

MessageRequesterEx(): spice up your messagerequester a bit

Post 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")
Last edited by netmaestro on Thu Jan 10, 2008 3:36 pm, edited 5 times in total.
BERESHEIT
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Nifty!
Dare2 cut down to size
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Cool.

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

cheers

umm - flags?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4791
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4791
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post 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.
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Post 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.
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Post 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)
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Post by yrreti »

That was the ticket! :D

Thanks very much LuCiFeR[SD]
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

This has been pretty much superseded by ManagedMessageBox 2.0 found here: http://www.purebasic.fr/english/viewtopic.php?t=32553
BERESHEIT
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Post 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.
Post Reply