home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / vb_refer / on_top / on_top.txt
Text File  |  1993-01-28  |  5KB  |  152 lines

  1.                         On Top
  2.  
  3. This is a Visual Basic 2.0 project (should work in VB 1.0 but I didn't try it).
  4. The project demonstrates a way of keeping one form out of a group of forms on top
  5. while minimizing "flashing" and without changing the focus. 
  6.  
  7. The project keeps Form3 on top of Form1. This is accomplished by using the 
  8. ShowWindow API call and "shadow" or "backing" forms. The backing forms
  9. are dummy forms that have the following property values:
  10.  
  11.     No Caption
  12.     No border
  13.     No Control Box (system menu)
  14.     No Minimize button
  15.     No Maximize button
  16.  
  17. When a backing form is displayed by itself on the screen it looks like a white 
  18. hole in the display (I assumed the default background color is white in this case). 
  19. The backing forms track the position of another form and are always positioned 
  20. under another form (or in back of another form). The purpose of the backing form 
  21. is to trap the move messages of the top form. It does this because anytime the top 
  22. form is moved, part of the bottom form is exposed and then we get a PAINT MESSAGE. 
  23. This has the effect of converting the move message of the top form to a paint 
  24. event in the bottom form. Of course the backing form immediatley sizes and moves 
  25. it self to stay perfectly under the top form so that it can't be seen!
  26.  
  27. This project has 4 forms and one module (*.BAS) file. The forms are called
  28.  
  29.     Form1.FRM    
  30.     Form2.FRM
  31.     Form3.FRM
  32.     Form4.FRM
  33.     Module1.BAS
  34.  
  35. To re-create the project, make four forms. Set the property valus of Form2.FRM
  36. and Form4.FRM as shown above for the "backing" forms. Copy the text to the
  37. appropriate events in the forms and *.bas file and go for it! Anytime you place
  38. Form1 on top of Form3, Form3 will come to the top .. but Form1 will still have
  39. the focus. Have fun!
  40.  
  41. One note, the extra backing form (I think it is Form2) is a remnant of another 
  42. experiment. This experiment made the position of two forms track. If you are 
  43. interested let me know. It only takes a few lines of code.
  44.  
  45. Jon Sigler
  46. 71631,776
  47.  
  48. *********************** Form1 ****************************
  49.  
  50. Option Explicit
  51.  
  52. Sub Form_GotFocus ()
  53.     Dim an_error As Integer
  54.     Form4.Top = form1.Top
  55.     Form4.Left = form1.Left
  56.     Form4.Width = form1.Width
  57.     Form4.Height = form1.Height
  58.     If Intersection(form3, form1) Then
  59.         an_error = ShowWindow(form3.hWnd, SW_SHOWNA)
  60.     End If
  61. End Sub
  62.  
  63. Sub Form_Load ()
  64.     Form4.Show
  65.     Form2.Show
  66.     form3.Show
  67. End Sub
  68.  
  69. Sub Form_Paint ()
  70.     Cls
  71.     Print "This form"
  72.     Print "will not"
  73.     Print "stay on"
  74.     Print "of Form3."
  75. End Sub
  76.  
  77. Sub Form_Resize ()
  78.     Dim an_error As Integer
  79.     Form4.Top = form1.Top
  80.     Form4.Left = form1.Left
  81.     Form4.Width = form1.Width
  82.     Form4.Height = form1.Height
  83.     If Intersection(form3, form1) Then
  84.         an_error = ShowWindow(form3.hWnd, SW_SHOWNA)
  85.     End If
  86. End Sub
  87.  
  88. *********************** Form2 ****************************
  89.  
  90. Option Explicit
  91.  
  92. Sub Form_Paint ()
  93.     Dim an_error As Integer
  94.     Form2.Top = form3.Top
  95.     Form2.Left = form3.Left
  96.     Form2.Width = form3.Width
  97.     Form2.Height = form3.Height
  98.     If Intersection(form3, form1) Then
  99.         an_error = ShowWindow(form3.hWnd, SW_SHOWNA)
  100.     End If
  101. End Sub
  102.  
  103. *********************** Form3 ****************************
  104. Option Explicit
  105.  
  106. Sub Form_Paint ()
  107.     Cls
  108.     Print "This form"
  109.     Print "will always"
  110.     Print "stay on top"
  111.     Print "of Form1."
  112. End Sub
  113.  
  114. *********************** Form4 ****************************
  115. Option Explicit
  116.  
  117. Sub Form_Paint ()
  118.     Dim an_error As Integer
  119.     Form4.Top = form1.Top
  120.     Form4.Left = form1.Left
  121.     Form4.Width = form1.Width
  122.     Form4.Height = form1.Height
  123.     If Intersection(form3, form1) Then
  124.         an_error = ShowWindow(form3.hWnd, SW_SHOWNA)
  125.     End If
  126. End Sub
  127.  
  128. ********************** Module1 ***************************
  129. Option Explicit
  130.  
  131. 'SW_SHOWNA
  132. Global Const SW_SHOWNA = 8
  133.  
  134. 'ShowWindow
  135. Declare Function ShowWindow Lib "User" (ByVal hWnd As Integer, ByVal nCmdShow As Integer) As Integer
  136.  
  137. Function Intersection (FormA As Form, FormB As Form) As Integer
  138.     'this simply determines if an intersection exits between FormA and FormB
  139.     If ((FormA.Left + FormA.Width) < FormB.Left) Then
  140.         Intersection = False
  141.     ElseIf ((FormB.Left + FormB.Width) < FormA.Left) Then
  142.         Intersection = False
  143.     ElseIf ((FormA.Top + FormA.Height) < FormB.Top) Then
  144.         Intersection = False
  145.     ElseIf ((FormB.Top + FormB.Height) < FormA.Top) Then
  146.         Intersection = False
  147.     Else
  148.         Intersection = True
  149.     End If
  150. End Function
  151.  
  152.