Ashok
asked on
Convert VB source code to Delphi (it is using Windows API)
For VB/Delphi expert....
Public Function CaptureWindow(ByVal hWndSrc As Long, _
ByVal Client As Boolean, ByVal LeftSrc As Long, _
ByVal TopSrc As Long, ByVal WidthSrc As Long, _
ByVal HeightSrc As Long) As Picture
Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim r As Long
Dim hDCSrc As Long
Dim hPal As Long
Dim hPalPrev As Long
Dim RasterCapsScrn As Long
Dim HasPaletteScrn As Long
Dim PaletteSizeScrn As Long
Dim LogPal As LOGPALETTE
' Depending on the value of Client get the proper device context.
If Client Then
hDCSrc = GetDC(hWndSrc) ' Get device context for client area.
Else
hDCSrc = GetWindowDC(hWndSrc) ' Get device context for entire
' window.
End If
' Create a memory device context for the copy process.
hDCMemory = CreateCompatibleDC(hDCSrc)
' Create a bitmap and place it in the memory DC.
hBmp = CreateCompatibleBitmap(hDC Src, WidthSrc, HeightSrc)
hBmpPrev = SelectObject(hDCMemory, hBmp)
' Get screen properties.
RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
' capabilities.
HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette
' support.
PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of
' palette.
' If the screen has a palette make a copy and realize it.
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
' Create a copy of the system palette.
LogPal.palVersion = &H300
LogPal.palNumEntries = 256
r = GetSystemPaletteEntries(hD CSrc, 0, 256, _
LogPal.palPalEntry(0))
hPal = CreatePalette(LogPal)
' Select the new palette into the memory DC and realize it.
hPalPrev = SelectPalette(hDCMemory, hPal, 0)
r = RealizePalette(hDCMemory)
End If
' Copy the on-screen image into the memory DC.
r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, _
LeftSrc, TopSrc, vbSrcCopy)
' Remove the new copy of the on-screen image.
hBmp = SelectObject(hDCMemory, hBmpPrev)
' If the screen has a palette get back the palette that was
' selected in previously.
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
hPal = SelectPalette(hDCMemory, hPalPrev, 0)
End If
' Release the device context resources back to the system.
r = DeleteDC(hDCMemory)
r = ReleaseDC(hWndSrc, hDCSrc)
' Call CreateBitmapPicture to create a picture object from the
' bitmap and palette handles. Then return the resulting picture
' object.
Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
End Function
'''''''''''''''''''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''
'
' CaptureForm
' - Captures an entire form including title bar and border.
'
' frmSrc
' - The Form object to capture.
'
' Returns
' - Returns a Picture object containing a bitmap of the entire
' form.
'''''''''''''''''''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''
'
Public Function CaptureForm(frmSrc As Form) As Picture
' Call CaptureWindow to capture the entire form given its window
' handle and then return the resulting Picture object.
Set CaptureForm = CaptureWindow(frmSrc.hWnd, False, 0, 0, _
frmSrc.ScaleX(frmSrc.Width , vbTwips, vbPixels), _
frmSrc.ScaleY(frmSrc.Heigh t, vbTwips, vbPixels))
End Function
Public Function CaptureWindow(ByVal hWndSrc As Long, _
ByVal Client As Boolean, ByVal LeftSrc As Long, _
ByVal TopSrc As Long, ByVal WidthSrc As Long, _
ByVal HeightSrc As Long) As Picture
Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim r As Long
Dim hDCSrc As Long
Dim hPal As Long
Dim hPalPrev As Long
Dim RasterCapsScrn As Long
Dim HasPaletteScrn As Long
Dim PaletteSizeScrn As Long
Dim LogPal As LOGPALETTE
' Depending on the value of Client get the proper device context.
If Client Then
hDCSrc = GetDC(hWndSrc) ' Get device context for client area.
Else
hDCSrc = GetWindowDC(hWndSrc) ' Get device context for entire
' window.
End If
' Create a memory device context for the copy process.
hDCMemory = CreateCompatibleDC(hDCSrc)
' Create a bitmap and place it in the memory DC.
hBmp = CreateCompatibleBitmap(hDC
hBmpPrev = SelectObject(hDCMemory, hBmp)
' Get screen properties.
RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
' capabilities.
HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette
' support.
PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of
' palette.
' If the screen has a palette make a copy and realize it.
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
' Create a copy of the system palette.
LogPal.palVersion = &H300
LogPal.palNumEntries = 256
r = GetSystemPaletteEntries(hD
LogPal.palPalEntry(0))
hPal = CreatePalette(LogPal)
' Select the new palette into the memory DC and realize it.
hPalPrev = SelectPalette(hDCMemory, hPal, 0)
r = RealizePalette(hDCMemory)
End If
' Copy the on-screen image into the memory DC.
r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, _
LeftSrc, TopSrc, vbSrcCopy)
' Remove the new copy of the on-screen image.
hBmp = SelectObject(hDCMemory, hBmpPrev)
' If the screen has a palette get back the palette that was
' selected in previously.
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
hPal = SelectPalette(hDCMemory, hPalPrev, 0)
End If
' Release the device context resources back to the system.
r = DeleteDC(hDCMemory)
r = ReleaseDC(hWndSrc, hDCSrc)
' Call CreateBitmapPicture to create a picture object from the
' bitmap and palette handles. Then return the resulting picture
' object.
Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
End Function
''''''''''''''''''''''''''
'
' CaptureForm
' - Captures an entire form including title bar and border.
'
' frmSrc
' - The Form object to capture.
'
' Returns
' - Returns a Picture object containing a bitmap of the entire
' form.
''''''''''''''''''''''''''
'
Public Function CaptureForm(frmSrc As Form) As Picture
' Call CaptureWindow to capture the entire form given its window
' handle and then return the resulting Picture object.
Set CaptureForm = CaptureWindow(frmSrc.hWnd,
frmSrc.ScaleX(frmSrc.Width
frmSrc.ScaleY(frmSrc.Heigh
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Jacco
I get Undeclared identifier: 'CaptureWindow'
I am using Delphi 7.
Following first few lines are from my unit in which I tried your function.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
As you said, I have Windows, Graphics & Forms.
Ashok
I get Undeclared identifier: 'CaptureWindow'
I am using Delphi 7.
Following first few lines are from my unit in which I tried your function.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
As you said, I have Windows, Graphics & Forms.
Ashok
ASKER
So where is CaptureWindow ? Is it Windows API ? I do not think so.
Thanks,
Ashok
Thanks,
Ashok
CaptureWindow is the first function he wrote for you... you need to put both functions in your code
ASKER
OK I got it.
Thanks BlackTigerX :-) I was busy coding another solution (this machine has no Email so no ding-dong ;-)
ASKER
I was hoping to use this code which I found from MSDN site to use
inf Delphi so that it could capture inactive (also minimized) window,
but it is not working as I expected.
When a window is minimized, it gives blank canvas.
When the window is active but Task Manager on top (using Stay on top), it gives
window image with Task Manager window.
I wanted only the actual window's image.
Can this be done any other way?
Thanks,
Ashok
inf Delphi so that it could capture inactive (also minimized) window,
but it is not working as I expected.
When a window is minimized, it gives blank canvas.
When the window is active but Task Manager on top (using Stay on top), it gives
window image with Task Manager window.
I wanted only the actual window's image.
Can this be done any other way?
Thanks,
Ashok
function CaptureForm(frmSrc: TForm): TBitmap;
begin
// Call CaptureWindow to capture the entire form given its window
// handle and then return the resulting Picture object.
Result := CaptureWindow(frmSrc.Handl
end;
Don't forget to put Windows, Graphics and Forms in the uses clause of the unit.
The function creates a bitmap which you have to destroy yourself!
Regards Jacco