MessageRequester Positioning

Just starting out? Need help? Post your questions and find answers here.
User avatar
Jacobus
Enthusiast
Enthusiast
Posts: 139
Joined: Wed Nov 16, 2005 7:51 pm
Location: France
Contact:

MessageRequester Positioning

Post by Jacobus »

Hello,
Is this behavior normal?
If you move the main window, the "MessageRequester" stays in the center of the screen. However, the WindowID() flag should reposition the MessageRequester to the center of the main window, right?
(PB 6.21B4 x86/x64 - Win 10-11)

Code: Select all

If OpenWindow(0, 0, 0, 500, 300, "Example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
   ButtonGadget  (1, 20, 130, 460, 30, "MessageRequester") 
   ButtonGadget  (2, 0, 270, 500, 30, "Close")
   
   Repeat
     Event = WaitWindowEvent()
     
     Select Event
     
       Case #PB_Event_Gadget
         Select EventGadget()
           Case 1
             MessageRequester("Position","I am stubborn and I decided to stay in the center!",#PB_MessageRequester_Info, WindowID(0))
           Case 2 
              CloseWindow(0)
              End              
         EndSelect
            
     EndSelect
   Until Event = #PB_Event_CloseWindow
 EndIf
PureBasicien tu es, PureBasicien tu resteras.
User avatar
jacdelad
Addict
Addict
Posts: 2000
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: MessageRequester Positioning

Post by jacdelad »

The message requester is always shown in the middle of the screen. You need some complicated Windows APIs to move it and I wouldn't recommend it.
https://stackoverflow.com/questions/528 ... x-location
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
Jacobus
Enthusiast
Enthusiast
Posts: 139
Joined: Wed Nov 16, 2005 7:51 pm
Location: France
Contact:

Re: MessageRequester Positioning

Post by Jacobus »

Ok, thanks for the reply. In these conditions it is better to create a new window for certain messages that must accompany the parent window, rather than a MessageRequester.
PureBasicien tu es, PureBasicien tu resteras.
Axolotl
Addict
Addict
Posts: 819
Joined: Wed Dec 31, 2008 3:36 pm

Re: MessageRequester Positioning

Post by Axolotl »

Well, maybe that's not quite how M$ would like it, but it's doable with this.

(more example codes are on this forum, this was the first I found)
Re: Additional flag for MessageRequester to center requester on app window

Or if you are on windows and not looking for crossplattform, you can use this from (the very talented) Zapman:
An alternative MessageRequester [Windows]

BTW: I like it better when the MessageRequester is centered on the associated window and not on the screen. But that's my taste.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
Jacobus
Enthusiast
Enthusiast
Posts: 139
Joined: Wed Nov 16, 2005 7:51 pm
Location: France
Contact:

Re: MessageRequester Positioning

Post by Jacobus »

Axolotl wrote: Tue Apr 08, 2025 4:12 pm BTW: I like it better when the MessageRequester is centered on the associated window and not on the screen. But that's my taste.
For me too, this is why I thought it was possible with the parent window flag, like creating secondary windows that follow the parent window.
This should be an option to be added in a future version of PB.
PureBasicien tu es, PureBasicien tu resteras.
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: MessageRequester Positioning

Post by doctorized »

I use the following code. Some code parts came from this forum.

Code: Select all

UsePNGImageDecoder()
Enumeration
	#MsgWindow = $FFF
	#Msg_cont_title
	#Msg_canvas_title
	#Msg_txt_title
	#Msg_img_close
	#Msg_cont_message
	#Msg_canvas_message_back
	#Msg_img_message_back
	#Msg_txt_message
	#Msg_img_main
	#Msg_OK
	#Msg_Yes
	#Msg_No
	#Font
EndEnumeration

Procedure LftMouseDown()
  Shared MouseDown, OffsetX, OffsetY
  MouseDown=#True
  offsetX=WindowMouseX(#MsgWindow)/DesktopResolutionX()
  OffsetY=WindowMouseY(#MsgWindow)/DesktopResolutionY()
EndProcedure

Procedure MouseMve()
  Shared MouseDown, OffsetX, OffsetY
  If MouseDown And GetGadgetAttribute(#Msg_canvas_title,#PB_Canvas_Buttons)&#PB_Canvas_LeftButton
    ResizeWindow(#MsgWindow, DesktopMouseX()/DesktopResolutionX()-OffsetX, DesktopMouseY()/DesktopResolutionY()-OffsetY, #PB_Ignore, #PB_Ignore)
  EndIf
EndProcedure

Procedure.i GetTextHeight(text.s, Width.i, Font.i)
  Protected TempGadget.i, txtHeight.i, TextLine.s, NbLine.i, I.i
  TempGadget = TextGadget(#PB_Any, 0, 0, Width, 20, text)
  SetGadgetFont(TempGadget,FontID(Font))
  txtHeight = GadgetHeight(TempGadget, #PB_Gadget_RequiredSize) - 2   ; -2 for the TextGadget Border
  NbLine = 1
  If GadgetWidth(TempGadget, #PB_Gadget_RequiredSize) > Width - 2
    For I = 1 To CountString(text, " ") + 1
      If TextLine = "" : TextLine = StringField(text, I, " ") : Else : TextLine + " " + StringField(text, I, " ") : EndIf
      SetGadgetText(TempGadget,  TextLine)
      If GadgetWidth(TempGadget, #PB_Gadget_RequiredSize) > Width - 2
        NbLine + 1
        If TextLine = StringField(text, I, " ")
          TextLine = ""
        Else
          TextLine = StringField(text, I, " ")
        EndIf
      EndIf
    Next
    If TextLine = "" : NbLine - 1 : EndIf
  EndIf
  FreeGadget(TempGadget)
  ProcedureReturn NbLine * txtHeight ;+ 2
EndProcedure

Procedure.i GetTextWidth(text.s, Font.i)
	Protected TempGadget.i, txtWidth
	TempGadget  = TextGadget(#PB_Any, 0, 0, 20, 20, text)
	SetGadgetFont(TempGadget,FontID(Font))
	txtWidth = GadgetWidth(TempGadget, #PB_Gadget_RequiredSize) ;- 2
	FreeGadget(TempGadget)
	ProcedureReturn txtWidth
EndProcedure

Procedure.i MessageRequesterEx(Title.s, Message.s, Flags.i=0)
	Protected GdtWidth.i, GdtHeight.i, WndWidth.i, WndHeight.i, Icon.i
	Protected DPI.f = DesktopResolutionX()
	Debug RSet(Bin(Flags),8,"0")
	Debug (Flags >> 6) & 1
	Debug (Flags >> 5) & 1
	Debug (Flags >> 4) & 1
	If (Flags >> 6) & 1 = 1;INFORMATION
		Icon = CatchImage(#PB_Any, ?InfoStart, ?InfoEnd - ?InfoStart)
	ElseIf (Flags >> 4) & 3 = 2;QUESTION
		Icon = CatchImage(#PB_Any, ?QuestionStart, ?QuestionEnd - ?QuestionStart)
	ElseIf (Flags >> 4) & 3 = 3;WARNING
		Icon = CatchImage(#PB_Any, ?WarningStart, ?WarningEnd - ?WarningStart)
	ElseIf (Flags >> 4) & 1 = 1;ERROR
		Icon = CatchImage(#PB_Any, ?ErrorStart, ?ErrorEnd - ?ErrorStart)
	EndIf
	Window = GetActiveWindow()
	LoadFont(#Font,"Arial",16)
	If Window < 1
		OpenWindow(#MsgWindow, 0, 0, 200, 200, "", #PB_Window_ScreenCentered|#PB_Window_BorderLess)
	Else
		OpenWindow(#MsgWindow, 0, 0, 200, 200, "", #PB_Window_WindowCentered|#PB_Window_BorderLess,WindowID(window))
	EndIf
	;SetGadgetFont(#PB_Default,FontID(#Font))
	GdtWidth = GetTextWidth(Message, #Font) 
	If GdtWidth > 400: GdtWidth = 400: EndIf
	If GdtWidth < 100: GdtWidth = 100: EndIf
	GdtHeight = Gettextheight(Message,GdtWidth, #Font)
	;Debug GdtHeight
	;Debug GdtWidth
	If Icon = 0
		WndWidth = GdtWidth + 42
		WndHeight = GdtHeight + 119
		txtX = 20
		If WndWidth < 260: WndWidth = 260: GdtWidth = 218 : EndIf
	Else
		txtX = 100
		WndWidth = GdtWidth+132;100 (txtX) + 30 (txt right) + 2 (lines)
		WndHeight = GdtHeight+119;32 (title) + 15 (txtY) + 15 (txt bottom) + 55 (button space, 35 +10Y + 10 bottom) + 2 (lines)
		If WndHeight < 189: WndHeight = 189: EndIf; 192 = 32 + 15 + 72 + 15 + 55
		If WndWidth < 260: WndWidth = 260: EndIf
	EndIf
	X = WindowX(#MsgWindow)
	Y = WindowY(#MsgWindow)
	ResizeWindow(#MsgWindow,X-(WndWidth-200)/2,Y-(WndHeight-200)/2,WndWidth,WndHeight)
	ContainerGadget(#Msg_cont_title,1,1,WndWidth-2,32)
	;i = CreateImage(#PB_Any,GdtWidth+38,GdtHeight+78,24,$FFF4EF)
	;ImageGadget(#Msg_img_back,0,0,GdtWidth+38,GdtHeight+78,ImageID(i))
	CanvasGadget(#Msg_canvas_title, 0,0,WndWidth-34,32)
	If StartDrawing(CanvasOutput(#Msg_canvas_title))
		Box(0,0,WndWidth-2,32,$FFF4EF)
		DrawingFont(FontID(#Font))
		Height = TextHeight(Title)
		DrawText(15,(32-Height)/2,Title,1,$FFF4EF)
	  StopDrawing()
	EndIf
	CloseIcon = CatchImage(#PB_Any, ?CloseMsgStart, ?CloseMsgEnd - ?CloseMsgStart)
	ImageGadget(#Msg_img_close,WndWidth-33,0,32,32,ImageID(CloseIcon))
	CloseGadgetList()

	If Icon = 0
		ContHeight = GdtHeight + 30
	Else
		If (GdtHeight + 30) > 102; 15 (txtX) + 72 (imgW) + 15 (img bottom)
			ContHeight = GdtHeight + 30
		Else
			ContHeight = 102
		EndIf
	EndIf

	ContainerGadget(#Msg_cont_message,1,34,WndWidth-2,ContHeight)
	i = CreateImage(#PB_Any,WndWidth-2,ContHeight,24,$FFFFFF)
	ImageGadget(#Msg_img_message_back,0,0,WndWidth-2,ContHeight,ImageID(i))
	DisableGadget(#Msg_img_message_back,#True)
	If Icon <> 0;no icon
		ImageGadget(#Msg_img_main,20,15,72,72,ImageID(Icon))
	EndIf
	TextGadget(#Msg_txt_message,txtX, 15,GdtWidth,GdtHeight,Message)
	SetGadgetFont(#Msg_txt_message,FontID(#Font))
	CloseGadgetList()
	SetGadgetColor(#Msg_txt_message,#PB_Gadget_BackColor,$FFFFFF)
	SetWindowLongPtr_(GadgetID(#Msg_img_message_back), #GWL_STYLE, GetWindowLongPtr_(GadgetID(#Msg_img_message_back), #GWL_STYLE) | #WS_CLIPSIBLINGS)
 	SetWindowPos_(GadgetID(#Msg_img_message_back), #HWND_BOTTOM, -1, -1, -1, -1, #SWP_NOSIZE | #SWP_NOMOVE)
 	;buttons
 	If Flags & #PB_MessageRequester_Ok
 		ButtonGadget(#Msg_OK,(WndWidth-100)/2,ContHeight + 43,100,35,"OK") ;1 (line) + 32(title) + 10
 		SetGadgetFont(#Msg_OK,FontID(#Font))
 	ElseIf Flags & #PB_MessageRequester_YesNo
 		ButtonGadget(#Msg_Yes,WndWidth - 240,ContHeight + 43,100,35,"Yes")
 		ButtonGadget(#Msg_No,WndWidth - 120,ContHeight + 43,100,35,"No")
 		SetGadgetFont(#Msg_Yes,FontID(#Font))
 		SetGadgetFont(#Msg_No,FontID(#Font))
 		SetActiveGadget(#Msg_Yes)
 	Else
 		ButtonGadget(#Msg_OK,(WndWidth-100)/2,ContHeight + 43,100,35,"OK") 
 		SetGadgetFont(#Msg_OK,FontID(#Font))
 		SetActiveGadget(#Msg_OK)
 	EndIf
 	;border
	StartDrawing(WindowOutput(#MsgWindow))
	LineXY(0,0,WndWidth*DPI,0,$646464)
	LineXY(WndWidth*DPI-1,0,WndWidth*DPI-1,WndHeight*DPI,$646464)
	LineXY(0,WndHeight*DPI-1,WndWidth*DPI,WndHeight*DPI-1,$646464)
	LineXY(0,0,0,WndHeight*DPI,$646464)
	StopDrawing()
	AddKeyboardShortcut(#MsgWindow, #PB_Shortcut_Return, 15)
	BindEvent(#PB_Event_Gadget,@LftMouseDown(),#MsgWindow,#Msg_canvas_title,#PB_EventType_LeftButtonDown)
	BindEvent(#PB_Event_Gadget,@MouseMve(),#MsgWindow,#Msg_canvas_title,#PB_EventType_MouseMove)
	Repeat
		Event = WaitWindowEvent()
		If Event = #PB_Event_CloseWindow
			Quit = 1
		ElseIf Event = #PB_Event_Menu
			If EventMenu() = 15
				If GetActiveGadget() = #Msg_OK
					CloseWindow(#MsgWindow)
					ProcedureReturn #PB_MessageRequester_Ok
				EndIf
			EndIf
		ElseIf Event = #PB_Event_Gadget
			Select EventGadget()	
				Case #Msg_OK
					CloseWindow(#MsgWindow)
					ProcedureReturn #PB_MessageRequester_Ok
				Case #Msg_Yes
					CloseWindow(#MsgWindow)
					ProcedureReturn #PB_MessageRequester_Yes
				Case #Msg_No
					CloseWindow(#MsgWindow)
					ProcedureReturn #PB_MessageRequester_No
				Case #Msg_img_close
					Quit = 1
			EndSelect
		EndIf
	Until Quit = 1
	CloseWindow(#MsgWindow)
EndProcedure

MessageRequesterEx("Attention!","Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.",#MB_ICONWARNING|#PB_MessageRequester_YesNo)

DataSection
	CloseMsgStart:
		IncludeBinary "closeBtn.png"
	CloseMsgEnd:
	InfoStart:
		IncludeBinary "info.png"
	InfoEnd:
	ErrorStart:
		IncludeBinary "error.png"
	ErrorEnd:
	WarningStart:
		IncludeBinary "warning.png"
	WarningEnd:
	QuestionStart:
		IncludeBinary "question.png"
	QuestionEnd:
EndDataSection
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: MessageRequester Positioning

Post by Paul »

So what is the purpose of ...

March 27, 2024 : Version 6.10 LTS
Added - Parent window support to all requesters

I would have assumed if Parent ID flag is used the MessageRequester would appear centered with the Parent instead of the Screen.
Image Image
Fred
Administrator
Administrator
Posts: 18163
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: MessageRequester Positioning

Post by Fred »

The purpose of the parent window is not for centering the requester but to explicitly set the parent window so the requester is always show in front of this window. Why it doesn't center it, you should ask MS..
BarryG
Addict
Addict
Posts: 4160
Joined: Thu Apr 18, 2019 8:17 am

Re: MessageRequester Positioning

Post by BarryG »

I use this to open MessageRequesters over my window -> https://www.purebasic.fr/english/viewto ... 22#p578222
Post Reply