-
Mar 7th, 2012, 11:49 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Position MsgBox
Hi
I downloaded some code so that I can position my MsgBox's and it works fine
but in there example a command button is used I want it to fire when any of the
MsgBox's are shown in my project.
Can someone help me with this please.
Code:
Private Sub Command2_Click()
Dim hInst As Long
Dim Thread As Long
'Set up the CBT hook
hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc2, hInst, _
Thread)
'Display the message box
MsgBox "This message box is centered over Form1."
End Sub
-
Mar 7th, 2012, 12:34 PM
#2
Re: Position MsgBox
You should be able to overload the MsgBox function by putting the following function in a BAS module.
Code:
Public Function MsgBox(Prompt, Optional Buttons As VbMsgBoxStyle = vbOKOnly, [Title], [HelpFile], [Context]) As VbMsgBoxResult
Dim hInst As Long
Dim Thread As Long
'Set up the CBT hook
hInst = GetWindowLong(Me.hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc2, hInst, _
Thread)
'Display the message box
MsgBox = VBA.MsgBox Prompt, Buttons, Title, HelpFile, Context
End Function
This in and of itself won't compile: You're going to need to correctly define Buttons, Title, HelpFile and Context (or leave them out entirely if you wish). Furthermore, you're going to need to figure out what to do about Me.hwnd, which has no context in a BAS module: you can either pass in a value to the function or if the "Me" always refers to the same form, just place the use the form's name instead (ie GetWindowLong(MDIMain.hwnd, ...).
-
Mar 7th, 2012, 12:39 PM
#3
Re: Position MsgBox
In addition you may also need to modify the WinProc2 procedure to ensure that there is no form name hard coded.
Instead of all of the above I would design my own message box form and activate it from anywhere in your project by passing few parameters (perhaps, parent form, title, message, postion, etc...).
In such way there will be no subclassing and you will have better control over what and how is displayed.
-
Mar 8th, 2012, 10:39 AM
#4
Thread Starter
Hyperactive Member
Re: Position MsgBox
Hi RhinoBull
I think the hook method could cause me more problems than it solves so I decided to go down the route you suggested have a form as a message box.
I have found an excellent example posted to this forum by LongWolf the code was written by John R Parrish I have figured out how all the code works except
one how to move the msgbox(form) to my position.
Can you help with that please?
The code below is the option I want to use.
Code:
frmMsgBox.MsgCstm "Rocket Rons Test", "IP Control", mbInformation, 2, False, _
"Yes", "No", "Exit"
-
Mar 8th, 2012, 01:27 PM
#5
Re: Position MsgBox
You may need to expend MsgCstm procedure by additing 2 parameters (X/Y or Left/Top) and pass both each time you call it.
Having X/Y you can easily position the form.
-
Mar 9th, 2012, 03:04 AM
#6
Thread Starter
Hyperactive Member
Re: Position MsgBox
Hi RhinoBull
All original code here was written by John R Parrish
I think I can insert the X/Y params to the MsgCstm Function if my example below is correct
Code:
Public Function MsgCstm(ByRef Promt As String, _
ByRef Title As String, _
ByRef MsgIcon As eMsgIcon, _
ByRef DefaultBtn As Long, _
ByRef PosLeft as Long _ 'added by me
ByRef PosTop as Long _ 'added by me
ByRef ShowDontAsk As Boolean, _
ParamArray btnText()) As Long
End Function
But what I do not know is how to get the project to accept the numbers I enter to position the form like
Code:
PosLeft = Me.Move (Screen.Width - Me.Width) / 2, _
(Screen.Height - Me.Height) / 2
PosTop = 200
Can you help further please
-
Mar 9th, 2012, 08:08 AM
#7
Re: Position MsgBox
Well, I would do something slightly different:
Code:
'in the form (any form in your project that needs to call msgbox
Option Explicit
Private Sub Command1_Click()
ShowMessageBox Me.Left, Me.Top, Me, ... '<<< replace "..." with all manadatory parameters
End Sub
'in the module
Option Explicit
Public Sub MsgCstm(X As Single, Y As Single, frmOwner As Form, _
sPromt As String, sTitle As String, MsgIcon As eMsgIcon, _
DefaultBtn As Long, ShowDontAsk As Boolean, ParamArray btnText() _
)
Load frmMessageBox
'--------------------
'main code goes here
'--------------------
'position form
frmMessageBox.Move X, Y
'finally display the msgbox
frmMessageBox.Show vbModal, frmOwner
End Sub
-
Mar 9th, 2012, 12:08 PM
#8
Thread Starter
Hyperactive Member
Re: Position MsgBox
Hi RhinoBull
Sorry I am being a bit thick today I am not sure where to put the Sub or Function ShowMessageBox.
This is the code I have added as per your post:-
frmCopy
Code:
Private Sub Command1_Click()
ShowMessageBox Me.BackColor, Me.Caption, Me.ControlBox, Me.Enabled, Me.Font, Me.Height, _
Me.Left, Me.Moveable, Me.Top, Me.Visible
End Sub
modMsgBox
Code:
Public Sub MsgCstm(X As Single, Y As Single, frmOwner As Form, _
sPromt As String, sTitle As String, MsgIcon As eMsgIcon, _
DefaultBtn As Long, ShowDontAsk As Boolean, ParamArray btnText() _
)
Load frmMessageBox
'--------------------
'main code goes here
'--------------------
'position form
frmMessageBox.Move X, Y
'finally display the msgbox
frmMessageBox.Show vbModal, frm2 'frmOwner
End Sub
Does this still call John R Parrish Button code?
Need to get some more help please.
-
Mar 9th, 2012, 02:11 PM
#9
Re: Position MsgBox
I don't really know what John's procedure does so I commented with "main code goes here".
However, I mistakely used ShowMessageBox instead of original MsgCstm so rename it as necessary.
-
Mar 9th, 2012, 04:51 PM
#10
Thread Starter
Hyperactive Member
Re: Position MsgBox
Hi RhinoBull
When I said I was being a bit thick today it was true.
Staring me in the face was a form load sub put the position
code there and it all works fine and as I wanted.
Thanks for you time.
Problem resolved
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|