I am trying to view a powerpoint show in a form...
I could make this work fine in VB6 but after bringing the code over to VS 2003 I have an issue with hWnd control.
I know that hwnd has changed to .handle now, but it is still not working correctly!
Code:
Const APP_NAME = "Powerpoint"
Const ppShowTypeSpeaker = 1
Const ppShowTypeInWindow = 1000
Const ppAdvanceOnTime = 2
Public oPPTApp As Object
Public oPPTPres As Object
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Function start_show()
Dim screenClasshWnd As Long
On Error Resume Next
oPPTApp = CreateObject("Powerpoint.Application")
oPPTPres = oPPTApp.Presentations.Open("c:\demo.ppt", , , False)
With oPPTPres
With .Slides
With .Range
With .SlideShowTransition
.AdvanceOnTime = True
End With
End With
End With
With .SlideShowSettings
.ShowType = ppShowTypeSpeaker
.AdvanceMode = ppAdvanceOnTime
With .Run
.width = pptbox1.Width
.height = pptbox1.Height
End With
End With
screenClasshWnd = FindWindow("screenClass", 0&)
SetParent(screenClasshWnd, pptbox1.Handle.ToInt32)
End With
End Function
I think the issue is to do with the hwnd parameters in the private declare functions, but I dont know what to change them to??
Any ideas?
Thanks
ps.. pptbox1 is a groupbox (which i think is the same ad the frame in vb6)
Windows API functions almost universally expect 32-bit values. In VB6 the Long type is 32 bits wide and the Integer type is 16 bits wide. In VB.NET the Long type is 64 bits wide and the Integer type is 32 bits wide. Where you used Longs in VB6 you should use Integers in VB.NET.
That said, when you're dealing with handles in VB.NET you should use the IntPtr type, which is the type that the Control.Handle property returns.
I realise that the VB.NET code won't work in VB6. I was implying that your saying over and over that this used to work in VB6 was irrelevant and unnecessary. I was also thinking that you'd post the relevant code directly, like in your first post.
I see you have Option Strict Off which will make for the possibilities of more errors due to casting issues. But I browsed the code and you need to remove the "On Error Resume Next" VB 6 legacy code error handler as in .NET you should use a Try Catch error code block.
The SetParent API is not declared correctly.
Code:
Option Explicit On
Option Strict On
Imports System.Runtime.InteropServices
Public Class Form1
'Declare your constants as Integer instead.
Const APP_NAME As String = "Powerpoint"
Const ppShowTypeSpeaker As Integer = 1
Const ppShowTypeInWindow As Integer = 1000
Const ppAdvanceOnTime As Integer = 2
<DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="FindWindow")> _
Private Shared Function FindWindow( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, EntryPoint:="SetParent")> _
Private Shared Function SetParent( _
ByVal hWndChild As IntPtr, _
ByVal hWndNewParent As IntPtr) As Integer
End Function
Private Function start_show()
'Blah
End Sub
End Class
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
it does not like the pptbox1.Handle.ToInt32 - is there something else I should put there?
I thought jmcilhinney mentioned that Control.Handle was an IntPtr. If this is true, then you shouldn't be converting it to an Int32. So you would use this instead:
Code:
SetParent(screenClasshWnd, pptbox1.Handle)
Originally Posted by jono_m
Also I get lots of errors if I put Option Strict On!
That means you're not using types, methods and classes correctly. You really should leave it on and fix the errors otherwise odd bugs that are almost impossible to track down can crop up due to typing issues.
KrisSiegel.com - My Personal Website with my blog and portfolio Don't Forget to Rate Posts!
1) C:\...\Form1.vb(141): Option Strict On requires all function and property declarations to have an 'As' clause.
2) C:\...\Form1.vb(145): Option Strict On disallows late binding.
3) C:\...\Form1.vb(148): Option Strict On disallows late binding.
4) C:\...\Form1.vb(155): Option Strict On disallows late binding.
5) C:\...\Form1.vb(167): Option Strict On disallows late binding.
Code:
Private Function start_show() ' Error 1
Dim screenClasshWnd As IntPtr
oPPTApp = CreateObject("Powerpoint.Application")
oPPTPres = oPPTApp.Presentations.Open("c:\demo.ppt", , , False) ' Error 2
With oPPTPres
With .Slides ' Error 3
With .Range
With .SlideShowTransition
.AdvanceOnTime = True
End With
End With
End With
With .SlideShowSettings ' Error 4
.ShowType = ppShowTypeSpeaker
.AdvanceMode = ppAdvanceOnTime
With .Run
.width = pptbox1.Width
.height = pptbox1.Height
End With
End With
screenClasshWnd = FindWindow("screenClass", 0&)
SetParent(screenClasshWnd, pptbox1.Handle)
End With
oPPTPres.Saved = True ' Error 5
End Function
Well, for Error #1 I suggest taking a look at creating Functions in VB.Net. Basically, you're not returning anything but Functions in VB.net must return a value.
Instead, I believe you can change Function to Sub and it should work as expected. That or you can add the As Type at the top and return the Type (if you're returning anything that is).
Not sure about the others.
KrisSiegel.com - My Personal Website with my blog and portfolio Don't Forget to Rate Posts!
A function needs to always return something or its not a function. Change to a sub if your not returning anything.
For the rest you need to create new object variables for them and use Reflection as Late Binding doesnt let you access objects and properties more then a single level down. After fixing #1 you may switch back to Option Strict Off to solve it.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
The powerpoint will now display inside the groupbox, but it will not resize to fit so I only see part of the powerpoint!
I have specified the width & height to be that of the groupbox in the code.. is there something I have missed out?
Jono
--- Edit
I think I might have worked out why it wont resize to the groupbox..
It looks like the powerpoint might read the sizes differently, I know that vb.net deals with sizes in pixels, does anyone know what that translates to in the powerpoint?
Its because you have changed the parenting of PowerPoint which is not designed to be run in a container object like another form or form with a groupbox.
What you are missing is more API code to set the location and size/width as once its placed inside another container its coordinated become relative to the containing object.
Use GetWindowPlacement and SetWindowPlacement to properly position and size PowerPoint inside your Groupbox.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.