Visual Basic para Aplicaciones - Una perlita de regalo...

Life is soft - evento anual de software empresarial
 
Vista:

Una perlita de regalo...

Publicado por JuanC (243 intervenciones) el 08/07/2006 18:17:42
Un ejemplo para usar los mismos componentes
en dos formularios, ahorrando recursos
(con menor tamaño de archivo) y aumentando la
velocidad al trabajar con gran cantidad de datos,
ya que se cargan una sola vez y luego se transporta
el componente ya cargado...

Adaptado de un artículo de internet
Autor: Harvey Triana
Fuente: Programación Cliente-Servidor con Visual Basic, Kennet L. Spencer.

Para el ejemplo, utilizar un Módulo y dos UserForm (Excel 2000)

En el Módulo1
=============
Option Explicit

Private Declare Function apiFindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function apiGetFocus Lib "user32" Alias "GetFocus" () As Long

Public Declare Function apiSetParent Lib "user32" Alias "SetParent" ( _
ByVal hWndChild As Long, _
ByVal hWndParent As Long) As Long

Public Function GetControlHandle(Ctl As Control) As Long
On Error Resume Next
Ctl.SetFocus
If Err Then
GetControlHandle = 0
Else
GetControlHandle = apiGetFocus
End If
On Error GoTo 0
End Function

Public Function GetWindowHandle(Frm As Object) As Long
On Error Resume Next
GetWindowHandle = apiFindWindow(vbNullString, Frm.Caption)
On Error GoTo 0
End Function

En el Form1
===========
Componentes:
Un Frame1 conteniendo un ListBox1 y un TextBox1
Un CommandButton1

'Código
Private Sub CommandButton1_Click()
Dim hWnd&
On Error Resume Next

hWnd = GetWindowHandle(UserForm2)

If hWnd <> vbNull Then
Frame1.Left = 5
Frame1.Top = 5
Frame1.Caption = "en Form2"
apiSetParent GetControlHandle(Frame1), hWnd
End If

Me.Left = Me.Left - 10000
UserForm2.Show
End Sub

En el Form2
===========
'Código
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Dim hWnd&
On Error Resume Next
Me.Hide
hWnd = GetWindowHandle(UserForm1)

If hWnd <> vbNull Then
UserForm1.Frame1.Caption = "en Form1"
apiSetParent GetControlHandle(UserForm1.Frame1), hWnd
End If

UserForm1.Left = UserForm1.Left + 10000
End Sub

Espero que les guste y les sirva de base para grandes proyectos...

Saludos desde Baires, JuanC
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder