Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change FontName of a MsgBox

Status
Not open for further replies.

edderic

Programmer
May 8, 1999
628
I can change the ForeColor and BackColor of a MsgBox with api's.<br>
is there a way to change the FontName with api's ?<br>
<br>
Thanks<br>
<br>
<A HREF="mailto:vbg.be@vbgroup.nl">vbg.be@vbgroup.nl</A><br>
<br>

 
Here is it :<br>
<br>
Make a module and declare :<br>
<br>
Option Explicit<br>
<br>
<br>
' the max length of a path for the system (usually 260 or there abouts)<br>
' this is used to size the buffer string for retrieving the class name of the active window below<br>
Public Const MAX_PATH As Long = 260&<br>
<br>
Public Const API_TRUE As Long = 1&<br>
Public Const API_FALSE As Long = 0&<br>
<br>
' font *borrowed* from the form used to replace MessageBox font<br>
Public g_hBoldFont As Long<br>
<br>
' Public Const MSGBOXTEXT As String = &quot;Have you ever seen a standard message box with a different font than all the others on the system?&quot;<br>
Public Const MSGBOXTEXT As String = &quot;MS SANS SERIF 18 test op groote letterteksten&quot;<br>
' Public Const MSGBOXTEXT As String = &quot;Test&quot;<br>
<br>
Public Const WM_SETFONT As Long = &H30<br>
' made up constants for setting our timer<br>
Public Const NV_CLOSEMSGBOX As Long = &H5000&<br>
Public Const NV_MOVEMSGBOX As Long = &H5001&<br>
Public Const NV_MSGBOXCHNGFONT As Long = &H5002&<br>
<br>
' MessageBox() Flags<br>
Public Const MB_ICONQUESTION As Long = &H20&<br>
Public Const MB_TASKMODAL As Long = &H2000&<br>
' SetWindowPos Flags<br>
Public Const SWP_NOSIZE As Long = &H1&<br>
Public Const SWP_NOZORDER As Long = &H4&<br>
Public Const HWND_TOP As Long = 0&<br>
<br>
Type RECT<br>
Left As Long<br>
Top As Long<br>
Right As Long<br>
Bottom As Long<br>
End Type<br>
<br>
' API declares<br>
Public Declare Function LockWindowUpdate& Lib &quot;user32&quot; (ByVal hwndLock&)<br>
<br>
Public Declare Function GetActiveWindow& Lib &quot;user32&quot; ()<br>
<br>
Public Declare Function GetDesktopWindow& Lib &quot;user32&quot; ()<br>
<br>
Public Declare Function FindWindow& Lib &quot;user32&quot; Alias &quot;FindWindowA&quot; (ByVal lpClassName$, _<br>
ByVal lpWindowName$)<br>
<br>
Public Declare Function FindWindowEx& Lib &quot;user32&quot; Alias &quot;FindWindowExA&quot; (ByVal hWndParent&, _<br>
ByVal hWndChildAfter&, ByVal lpClassName$, ByVal lpWindowName$)<br>
<br>
Public Declare Function SendMessage& Lib &quot;user32&quot; Alias &quot;SendMessageA&quot; (ByVal hWnd&, ByVal _<br>
wMsg&, ByVal wParam&, lParam As Any)<br>
<br>
Public Declare Function MoveWindow& Lib &quot;user32&quot; (ByVal hWnd&, ByVal x&, ByVal y&, _<br>
ByVal nWidth&, ByVal nHeight&, ByVal bRepaint&)<br>
<br>
Public Declare Function ScreenToClientLong& Lib &quot;user32&quot; Alias &quot;ScreenToClient&quot; (ByVal hWnd&, _<br>
lpPoint&)<br>
<br>
Public Declare Function GetDC& Lib &quot;user32&quot; (ByVal hWnd&)<br>
Public Declare Function ReleaseDC& Lib &quot;user32&quot; (ByVal hWnd&, ByVal hDC&)<br>
<br>
' drawtext flags<br>
Public Const DT_WORDBREAK As Long = &H10&<br>
Public Const DT_CALCRECT As Long = &H400&<br>
Public Const DT_EDITCONTROL As Long = &H2000&<br>
Public Const DT_END_ELLIPSIS As Long = &H8000&<br>
Public Const DT_MODIFYSTRING As Long = &H10000<br>
Public Const DT_PATH_ELLIPSIS As Long = &H4000&<br>
Public Const DT_RTLREADING As Long = &H20000<br>
Public Const DT_WORD_ELLIPSIS As Long = &H40000<br>
<br>
Public Declare Function DrawText& Lib &quot;user32&quot; Alias &quot;DrawTextA&quot; (ByVal hDC&, ByVal lpsz$, _<br>
ByVal cchText&, lpRect As RECT, ByVal dwDTFormat&)<br>
<br>
Public Declare Function SetForegroundWindow& Lib &quot;user32&quot; (ByVal hWnd&)<br>
<br>
Public Declare Function GetClassName& Lib &quot;user32&quot; Alias &quot;GetClassNameA&quot; (ByVal hWnd&, _<br>
ByVal lpClassName$, ByVal nMaxCount&)<br>
<br>
Public Declare Function GetWindowRect& Lib &quot;user32&quot; (ByVal hWnd&, lpRect As RECT)<br>
<br>
Public Declare Function SetWindowPos& Lib &quot;user32&quot; (ByVal hWnd&, ByVal hWndInsertAfter&, _<br>
ByVal x&, ByVal y&, ByVal cx&, ByVal cy&, ByVal wFlags&)<br>
<br>
Public Declare Function MessageBox& Lib &quot;user32&quot; Alias &quot;MessageBoxA&quot; (ByVal hWnd&, _<br>
ByVal lpText$, ByVal lpCaption$, ByVal wType&)<br>
<br>
Public Declare Function SetTimer& Lib &quot;user32&quot; (ByVal hWnd&, ByVal nIDEvent&, ByVal uElapse&, _<br>
ByVal lpTimerFunc&)<br>
<br>
Public Declare Function KillTimer& Lib &quot;user32&quot; (ByVal hWnd&, ByVal nIDEvent&)<br>
<br>
<br>
Add a procedure :<br>
<br>
Public Sub TimerProc(ByVal hWnd&, ByVal uMsg&, ByVal idEvent&, ByVal dwTime&)<br>
' this is a callback function. This means that windows &quot;calls back&quot; to this function<br>
' when it's time for the timer event to fire<br>
<br>
' first thing we do is kill the timer so that no other timer events will fire<br>
KillTimer hWnd, idEvent<br>
<br>
' select the type of manipulation that we want to perform<br>
Select Case idEvent<br>
Case NV_CLOSEMSGBOX '&lt;-- we want to close this messagebox after 4 seconds<br>
Dim hMessageBox&<br>
<br>
' find the messagebox window<br>
hMessageBox = FindWindow(&quot;#32770&quot;, &quot;Self Closing Message Box&quot;)<br>
<br>
' if we found it make sure it has the keyboard focus and then send it an enter to dismiss it<br>
If hMessageBox Then<br>
Call SetForegroundWindow(hMessageBox)<br>
SendKeys &quot;{enter}&quot;<br>
End If<br>
<br>
Case NV_MOVEMSGBOX '&lt;-- we want to move this messagebox<br>
Dim hMsgBox&, xPoint&, yPoint&<br>
Dim stMsgBoxRect As RECT, stParentRect As RECT<br>
<br>
' find the messagebox window<br>
hMsgBox = FindWindow(&quot;#32770&quot;, &quot;Position A Message Box&quot;)<br>
<br>
' if we found it then move it<br>
If hMsgBox Then<br>
' get the rect for the parent window and the messagebox<br>
Call GetWindowRect(hMsgBox, stMsgBoxRect)<br>
Call GetWindowRect(hWnd, stParentRect)<br>
<br>
' calculate the position for putting the messagebox in the middle of the form<br>
xPoint = stParentRect.Left + (((stParentRect.Right - stParentRect.Left) \ 2) - _<br>
((stMsgBoxRect.Right - stMsgBoxRect.Left) \ 2))<br>
yPoint = stParentRect.Top + (((stParentRect.Bottom - stParentRect.Top) \ 2) - _<br>
((stMsgBoxRect.Bottom - stMsgBoxRect.Top) \ 2))<br>
<br>
' make sure the messagebox will not be off the screen.<br>
If xPoint &lt; 0 Then xPoint = 0<br>
If yPoint &lt; 0 Then yPoint = 0<br>
If (xPoint + (stMsgBoxRect.Right - stMsgBoxRect.Left)) &gt; _<br>
(Screen.Width \ Screen.TwipsPerPixelX) Then<br>
xPoint = (Screen.Width \ Screen.TwipsPerPixelX) - (stMsgBoxRect.Right - stMsgBoxRect.Left)<br>
End If<br>
If (yPoint + (stMsgBoxRect.Bottom - stMsgBoxRect.Top)) &gt; _<br>
(Screen.Height \ Screen.TwipsPerPixelY) Then<br>
yPoint = (Screen.Height \ Screen.TwipsPerPixelY) - (stMsgBoxRect.Bottom - stMsgBoxRect.Top)<br>
End If<br>
<br>
<br>
' move the messagebox<br>
Call SetWindowPos(hMsgBox, HWND_TOP, xPoint, yPoint, _<br>
API_FALSE, API_FALSE, SWP_NOZORDER Or SWP_NOSIZE)<br>
End If<br>
<br>
' unlock the desktop<br>
Call LockWindowUpdate(API_FALSE)<br>
<br>
<br>
Case NV_MSGBOXCHNGFONT '&lt;-- we want to change the font for this messagebox<br>
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!<br>
' NOTE: Changing the font of a message box is not recomemded!!<br>
' This portion of the demo is just provided to show some of the possibilities<br>
' for manipulating other windows using the Windows API.<br>
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!<br>
<br>
' find the messagebox window<br>
hMsgBox = FindWindow(&quot;#32770&quot;, &quot;Eric De Decker&quot;)<br>
<br>
' if we found it then find the static control that holds the text...<br>
If hMsgBox Then<br>
Dim hStatic&, hButton&, stMsgBoxRect2 As RECT<br>
Dim stStaticRect As RECT, stButtonRect As RECT<br>
<br>
' find the static control that holds the message text<br>
hStatic = FindWindowEx(hMsgBox, API_FALSE, &quot;Static&quot;, MSGBOXTEXT)<br>
hButton = FindWindowEx(hMsgBox, API_FALSE, &quot;Button&quot;, &quot;OK&quot;)<br>
<br>
' if we found it, change the text and resize the static control so it will be displayed<br>
If hStatic Then<br>
' get the rects of the message box and the static control before we change the font<br>
Call GetWindowRect(hMsgBox, stMsgBoxRect2)<br>
Call GetWindowRect(hStatic, stStaticRect)<br>
Call GetWindowRect(hButton, stButtonRect)<br>
<br>
' set the font we borrowed from the form into the static control<br>
Call SendMessage(hStatic, WM_SETFONT, g_hBoldFont, ByVal API_TRUE)<br>
<br>
With stStaticRect<br>
' convert the rect from screen coordinates to client coordinates<br>
Call ScreenToClientLong(hMsgBox, .Left)<br>
Call ScreenToClientLong(hMsgBox, .Right)<br>
<br>
Dim nRectHeight&, nHeightDifference&, hStaticDC&<br>
<br>
' get the current height of the static control<br>
nHeightDifference = .Bottom - .Top<br>
<br>
' get the device context of the static control to pass to DrawText<br>
hStaticDC = GetDC(hStatic)<br>
<br>
' use DrawText to calculate the new height of the static control<br>
nRectHeight = DrawText(hStaticDC, MSGBOXTEXT, (-1&), stStaticRect, _<br>
DT_CALCRECT Or DT_EDITCONTROL Or DT_WORDBREAK)<br>
<br>
' release the DC<br>
Call ReleaseDC(hStatic, hStaticDC)<br>
<br>
' calculate the difference in height<br>
nHeightDifference = nRectHeight - nHeightDifference<br>
<br>
' resize the static control so that the new larger bold text will fit in the messagebox<br>
Call MoveWindow(hStatic, .Left, .Top, .Right - .Left, nRectHeight, API_TRUE)<br>
End With<br>
<br>
' move the button to the new position<br>
With stButtonRect<br>
' convert the rect from screen coordinates to client coordinates<br>
Call ScreenToClientLong(hMsgBox, .Left)<br>
Call ScreenToClientLong(hMsgBox, .Right)<br>
<br>
' move the button<br>
Call MoveWindow(hButton, .Left, .Top + nHeightDifference, .Right - .Left, .Bottom - .Top, API_TRUE)<br>
End With<br>
<br>
With stMsgBoxRect2<br>
' resize and reposition the messagebox<br>
Call MoveWindow(hMsgBox, .Left, .Top - (nHeightDifference \ 2), .Right - .Left, (.Bottom - .Top) + nHeightDifference, API_TRUE)<br>
<br>
' NOTE: if your message is very long, you may need to add code to make sure the messagebox<br>
' will not run off the screen....<br>
End With<br>
End If<br>
End If<br>
<br>
' unlock the desktop<br>
Call LockWindowUpdate(API_FALSE)<br>
<br>
End Select<br>
<br>
End Sub<br>
<br>
In the form load :<br>
<br>
Private Sub Form_Load()<br>
<br>
' we will use the font from the form to change the text in one of our message boxes<br>
' first, set the attributes of the font that we will want to display.<br>
With Font<br>
.Bold = True<br>
.Italic = True<br>
End With<br>
<br>
' next, grab a handle to the form's font and store it in the global variable for use later.<br>
' don't change the font on the form or the value in the global variable will be invalid.<br>
Dim IFont As IFont<br>
Set IFont = Font<br>
<br>
g_hBoldFont = IFont.hFont<br>
<br>
Set IFont = Nothing<br>
<br>
End Sub<br>
<br>
<br>
Changes de fonts :<br>
<br>
Private Sub Command3_Click()<br>
' this changes th font for the message text of the messagebox.<br>
' the routine in the callback timer function<br>
<br>
' lock the desktop so that the initial font is not shown<br>
Call LockWindowUpdate(GetDesktopWindow())<br>
<br>
' set the callback timer with our application defined ID (NV_MSGBOXCHNGFONT)<br>
' set the time for 10 microseconds to allow the messagebox time to become active<br>
SetTimer hWnd, NV_MSGBOXCHNGFONT, 10&, AddressOf TimerProc<br>
<br>
' call the messagebox API function<br>
Call MessageBox(hWnd, MSGBOXTEXT, &quot;Eric De Decker&quot;, MB_ICONQUESTION Or MB_TASKMODAL)<br>
<br>
End Sub<br>
<br>
Position MessageBox :<br>
<br>
Private Sub Command2_Click()<br>
' this positions the messagebox in the desired location on the screen.<br>
' the location is defined in the callback timer function<br>
<br>
' lock the desktop so that the initial position is not shown<br>
Call LockWindowUpdate(GetDesktopWindow())<br>
<br>
' set the callback timer with our application defined ID (NV_MOVEMSGBOX)<br>
' set the time for 10 microseconds to allow the messagebox time to become active<br>
SetTimer hWnd, NV_MOVEMSGBOX, 10&, AddressOf TimerProc<br>
<br>
' call the messagebox API function<br>
Call MessageBox(hWnd, &quot;Have you ever seen a message box that wasn't in the middle of the screen?&quot;, _<br>
&quot;Position A Message Box&quot;, MB_ICONQUESTION Or MB_TASKMODAL)<br>
<br>
End Sub<br>
<br>
Self closing MessageBox :<br>
<br>
Private Sub Command1_Click()<br>
' this shows a messagebox that will be dismissed after 4 seconds<br>
<br>
' set the callback timer and pass our application defined ID (NV_CLOSEMSGBOX)<br>
' set the time for 4 seconds (4000& microseconds)<br>
SetTimer hWnd, NV_CLOSEMSGBOX, 4000&, AddressOf TimerProc<br>
<br>
' call the messagebox API function<br>
Call MessageBox(hWnd, &quot;Watch this message box close itself after four seconds&quot;, _<br>
&quot;Self Closing Message Box&quot;, MB_ICONQUESTION Or MB_TASKMODAL)<br>
<br>
End Sub<br>
<br>
Succes<br>
<br>
Eric De Decker
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top