-
[02/03] View Powerpoint Show in VB Form
Hi,
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)
Jono
-
Re: [02/03] View Powerpoint Show in VB Form
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.
-
Re: [02/03] View Powerpoint Show in VB Form
Hi, thanks for the reply.
I have changed all the longs, but there has been no difference!
Any other things I can try?
Thanks
Jono
-
Re: [02/03] View Powerpoint Show in VB Form
What actually happens when you run the code?
-
Re: [02/03] View Powerpoint Show in VB Form
It loads the presentation, but the presentation does not load into the groupbox as it should!!
This worked fine in vb6!
Jono
-
Re: [02/03] View Powerpoint Show in VB Form
I wonder if you could say again that it worked in VB6?
Show us your exact code so we can test it.
-
1 Attachment(s)
Re: [02/03] View Powerpoint Show in VB Form
Well obviously the VB.NET Code wont work in VB6....
Here is the project so far
There is also a powerpoint called demo.ppt in the zip - that is the file I am testing with!
Jono
-
Re: [02/03] View Powerpoint Show in VB Form
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.
-
Re: [02/03] View Powerpoint Show in VB Form
Sorry I thought you ment you wanted to see the whole project!
The code in the first post is pretty much what I am using, except I have changed the longs to integers as suggested.
Jono
-
Re: [02/03] View Powerpoint Show in VB Form
Any idea on this?
Thanks
Jono
-
Re: [02/03] View Powerpoint Show in VB Form
Hello?
Any help with this???
Jono
-
Re: [02/03] View Powerpoint Show in VB Form
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
-
Re: [02/03] View Powerpoint Show in VB Form
Thanks for the advice RobDog888,
I have tryed this but get this error:
C:\...\Form1.vb(164): Value of type 'Integer' cannot be converted to 'System.IntPtr'.
Which refers to the line:
Code:
SetParent(screenClasshWnd, pptbox1.Handle.ToInt32)
it does not like the pptbox1.Handle.ToInt32 - is there something else I should put there?
Also I get lots of errors if I put Option Strict On!
Thanks
Jono
-
Re: [02/03] View Powerpoint Show in VB Form
Quote:
Originally Posted by jono_m
C:\...\Form1.vb(164): Value of type 'Integer' cannot be converted to 'System.IntPtr'.
Which refers to the line:
Code:
SetParent(screenClasshWnd, pptbox1.Handle.ToInt32)
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)
Quote:
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.
-
Re: [02/03] View Powerpoint Show in VB Form
kasracer,
Thanks for pointing that out... I must have mis-read his post!
Ok.. that error is gone now, but the presentation still wont go into the control.
I will go through and try to correct all errors generated by having option stricty on and see if that helps..
Any other ideas would be good,
Thanks
Jono
-
Re: [02/03] View Powerpoint Show in VB Form
Just use "pptbox1.Handle" as I declared the API correctly with the argument of an IntPtr so no conversion is needed with the .ToInt32.
-
Re: [02/03] View Powerpoint Show in VB Form
I did that and i dont get an error now, however the powerpoint presentation still does not appear inside the groupbox!
Jono
-
Re: [02/03] View Powerpoint Show in VB Form
Well thats because you were supressing it and didnt know if you even had an error. ;)
What is the error message?
-
Re: [02/03] View Powerpoint Show in VB Form
Errors:
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
How do i fix this??
Thanks
Jono
-
Re: [02/03] View Powerpoint Show in VB Form
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.
-
Re: [02/03] View Powerpoint Show in VB Form
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.
-
Re: [02/03] View Powerpoint Show in VB Form
Ok, getting there!
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?
Jono
-
Re: [02/03] View Powerpoint Show in VB Form
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.