1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
7 // static char *VB_Name = "CaptureModule";
9 // --------------------------------------------------------------------
10 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
12 // Visual Basic 4.0 16/32 Capture Routines
14 // This module contains several routines for capturing windows into a
15 // picture. All the routines work on both 16 and 32 bit Windows
17 // The routines also have palette support.
19 // CreateBitmapPicture - Creates a picture object from a bitmap and
21 // CaptureWindow - Captures any window given a window handle.
22 // CaptureActiveWindow - Captures the active window on the desktop.
23 // CaptureForm - Captures the entire form.
24 // CaptureClient - Captures the client area of a form.
25 // CaptureScreen - Captures the entire screen.
26 // PrintPictureToFitPage - prints any picture as big as possible on
30 // - No error trapping is included in these routines.
31 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
33 // --- Option Explicit
36 // ::: #ifndef HAS_PALETTEENTRY
43 // ::: } PALETTEENTRY;
44 // ::: #define HAS_PALETTEENTRY
47 // ::: #ifndef HAS_LOGPALETTE
50 // ::: int palVersion;
51 // ::: int palNumEntries;
52 // ::: PALETTEENTRY palPalEntry[255]; // Enough for 256 colors.
54 // ::: #define HAS_LOGPALETTE
57 // ::: #ifndef HAS_GUID
65 // ::: #define HAS_GUID
70 #define RASTERCAPS (38)
71 #define RC_PALETTE (0x100)
72 #define SIZEPALETTE (104)
74 // ::: #ifndef HAS_RECT
82 // ::: #define HAS_RECT
85 long CreateCompatibleDC(long hDC);
86 long CreateCompatibleBitmap(long hDC, long nWidth, long nHeight);
87 long GetDeviceCaps(long hDC, long iCapabilitiy);
88 long GetSystemPaletteEntries(long hDC, long wStartIndex, long wNumEntries, PALETTEENTRY lpPaletteEntries);
89 long CreatePalette(LOGPALETTE lpLogPalette);
90 long SelectObject(long hDC, long hObject);
91 long BitBlt(long hDCDest, long XDest, long YDest, long nWidth, long nHeight, long hDCSrc, long XSrc, long YSrc, long dwRop);
92 long DeleteDC(long hDC);
93 long GetForegroundWindow();
94 long SelectPalette(long hDC, long hPalette, long bForceBackground);
95 long RealizePalette(long hDC);
96 long GetWindowDC(long hWnd);
97 long GetDC(long hWnd);
98 long GetWindowRect(long hWnd, RECT lpRect);
99 long ReleaseDC(long hWnd, long hDC);
100 long GetDesktopWindow();
102 // ::: #ifndef HAS_PicBmp
103 // ::: typedef struct
109 // ::: long Reserved;
111 // ::: #define HAS_PicBmp
114 long OleCreatePictureIndirect(PicBmp PicDesc, GUID RefIID, long fPictureOwnsHandle, IPicture IPic);
118 #define RASTERCAPS (38)
119 #define RC_PALETTE (0x100)
120 #define SIZEPALETTE (104)
122 // ::: #ifndef HAS_RECT
123 // ::: typedef struct
130 // ::: #define HAS_RECT
133 int CreateCompatibleDC(int hDC);
134 int CreateCompatibleBitmap(int hDC, int nWidth, int nHeight);
135 int GetDeviceCaps(int hDC, int iCapabilitiy);
136 int GetSystemPaletteEntries(int hDC, int wStartIndex, int wNumEntries, PALETTEENTRY lpPaletteEntries);
137 int CreatePalette(LOGPALETTE lpLogPalette);
138 int SelectObject(int hDC, int hObject);
139 int BitBlt(int hDCDest, int XDest, int YDest, int nWidth, int nHeight, int hDCSrc, int XSrc, int YSrc, long dwRop);
140 int DeleteDC(int hDC);
141 int GetForegroundWindow();
142 int SelectPalette(int hDC, int hPalette, int bForceBackground);
143 int RealizePalette(int hDC);
144 int GetWindowDC(int hWnd);
146 int GetWindowRect(int hWnd, RECT lpRect);
147 int ReleaseDC(int hWnd, int hDC);
148 int GetDesktopWindow();
150 // ::: #ifndef HAS_PicBmp
151 // ::: typedef struct
159 // ::: #define HAS_PicBmp
162 int OleCreatePictureIndirect(PicBmp PictDesc, GUID RefIID, int fPictureOwnsHandle, IPicture IPic);
166 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
168 // CreateBitmapPicture
169 // - Creates a bitmap type Picture object from a bitmap and
173 // - Handle to a bitmap.
176 // - Handle to a Palette.
177 // - Can be null if the bitmap doesn't use a palette.
180 // - Returns a Picture object containing the bitmap.
181 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
184 Picture CreateBitmapPicture(long hBmp, long hPal)
186 Picture CreateBitmapPicture;
191 Picture CreateBitmapPicture(int hBmp, int hPal)
193 Picture CreateBitmapPicture;
200 // IPicture requires a reference to "Standard OLE Types."
204 // Fill in with IDispatch Interface ID.
206 IID_IDispatch.Data1 = 0x20400;
207 IID_IDispatch.Data4[0] = 0xC0;
208 IID_IDispatch.Data4[7] = 0x46;
211 // Fill Pic with necessary parts.
213 pic.Size = sizeof(pic); // Length of structure.
214 pic.Type = vbPicTypeBitmap; // Type of Picture (bitmap).
215 pic.hBmp = hBmp; // Handle to bitmap.
216 pic.hPal = hPal; // Handle to palette (may be null).
219 // Create Picture object.
220 r = OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic);
222 // Return the new Picture object.
223 CreateBitmapPicture = IPic;
225 return CreateBitmapPicture;
228 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
231 // - Captures any portion of a window.
234 // - Handle to the window to be captured.
237 // - If True CaptureWindow captures from the client area of the
239 // - If False CaptureWindow captures from the entire window.
241 // LeftSrc, TopSrc, WidthSrc, HeightSrc
242 // - Specify the portion of the window to capture.
243 // - Dimensions need to be specified in pixels.
246 // - Returns a Picture object containing a bitmap of the specified
247 // portion of the window that was captured.
248 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
252 Picture CaptureWindow(long hWndSrc, boolean Client, long LeftSrc, long TopSrc, long WidthSrc, long HeightSrc)
254 Picture CaptureWindow;
265 long PaletteSizeScrn;
268 Picture CaptureWindow(int hWndSrc, boolean Client, int LeftSrc, int TopSrc, long WidthSrc, long HeightSrc)
270 Picture CaptureWindow;
286 // Depending on the value of Client get the proper device context.
289 hDCSrc = GetDC(hWndSrc); // Get device context for client area.
293 hDCSrc = GetWindowDC(hWndSrc); // Get device context for entire
297 // Create a memory device context for the copy process.
298 hDCMemory = CreateCompatibleDC(hDCSrc);
299 // Create a bitmap and place it in the memory DC.
300 hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc);
301 hBmpPrev = SelectObject(hDCMemory, hBmp);
303 // Get screen properties.
304 RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS); // Raster
306 HasPaletteScrn = RasterCapsScrn & RC_PALETTE; // Palette
308 PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE); // Size of
311 // If the screen has a palette make a copy and realize it.
312 if (HasPaletteScrn && (PaletteSizeScrn == 256))
314 // Create a copy of the system palette.
315 LogPal.palVersion = 0x300;
316 LogPal.palNumEntries = 256;
317 r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry[0]);
318 hPal = CreatePalette(LogPal);
319 // Select the new palette into the memory DC and realize it.
320 hPalPrev = SelectPalette(hDCMemory, hPal, 0);
321 r = RealizePalette(hDCMemory);
324 // Copy the on-screen image into the memory DC.
325 r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy);
327 // Remove the new copy of the on-screen image.
328 hBmp = SelectObject(hDCMemory, hBmpPrev);
330 // If the screen has a palette get back the palette that was
331 // selected in previously.
332 if (HasPaletteScrn && (PaletteSizeScrn == 256))
334 hPal = SelectPalette(hDCMemory, hPalPrev, 0);
337 // Release the device context resources back to the system.
338 r = DeleteDC(hDCMemory);
339 r = ReleaseDC(hWndSrc, hDCSrc);
341 // Call CreateBitmapPicture to create a picture object from the
342 // bitmap and palette handles. Then return the resulting picture
345 CaptureWindow = CreateBitmapPicture(hBmp, hPal);
347 return CaptureWindow;
350 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
353 // - Captures the entire screen.
356 // - Returns a Picture object containing a bitmap of the screen.
357 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
359 Picture CaptureScreen()
361 Picture CaptureScreen;
371 // Get a handle to the desktop window.
372 hWndScreen = GetDesktopWindow();
374 // Call CaptureWindow to capture the entire desktop give the handle
375 // and return the resulting Picture object.
377 CaptureScreen = CaptureWindow(hWndScreen, False, 0, 0, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY);
379 return CaptureScreen;
382 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
385 // - Captures an entire form including title bar and border.
388 // - The Form object to capture.
391 // - Returns a Picture object containing a bitmap of the entire
393 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
395 Picture CaptureForm(Form frmSrc)
399 // Call CaptureWindow to capture the entire form given its window
400 // handle and then return the resulting Picture object.
401 CaptureForm = CaptureWindow(frmSrc.hWnd, False, 0, 0, frmSrc.ScaleX(frmSrc.Width, vbTwips, vbPixels), frmSrc.ScaleY(frmSrc.Height, vbTwips, vbPixels));
406 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
409 // - Captures the client area of a form.
412 // - The Form object to capture.
415 // - Returns a Picture object containing a bitmap of the form's
417 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
419 Picture CaptureClient(Form frmSrc)
421 Picture CaptureClient;
423 // Call CaptureWindow to capture the client area of the form given
424 // its window handle and return the resulting Picture object.
425 CaptureClient = CaptureWindow(frmSrc.hWnd, True, 0, 0, frmSrc.ScaleX(frmSrc.ScaleWidth, frmSrc.ScaleMode, vbPixels), frmSrc.ScaleY(frmSrc.ScaleHeight, frmSrc.ScaleMode, vbPixels));
427 return CaptureClient;
430 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
432 // CaptureActiveWindow
433 // - Captures the currently active window on the screen.
436 // - Returns a Picture object containing a bitmap of the active
438 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
440 Picture CaptureActiveWindow()
442 Picture CaptureActiveWindow;
455 // Get a handle to the active/foreground window.
456 hWndActive = GetForegroundWindow();
458 // Get the dimensions of the window.
459 r = GetWindowRect(hWndActive, RectActive);
461 // Call CaptureWindow to capture the active window given its
462 // handle and return the Resulting Picture object.
463 CaptureActiveWindow = CaptureWindow(hWndActive, False, 0, 0, RectActive.right - RectActive.left, RectActive.bottom - RectActive.top);
465 return CaptureActiveWindow;
468 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
471 // PrintPictureToFitPage
472 // - Prints a Picture object as big as possible.
475 // - Destination Printer object.
478 // - Source Picture object.
479 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
481 void PrintPictureToFitPage(Printer Prn, Picture pic)
483 #define vbHiMetric (8)
491 // Determine if picture should be printed in landscape or portrait
492 // and set the orientation.
493 if (pic.Height >= pic.Width)
495 Prn.Orientation = vbPRORPortrait; // Taller than wide.
499 Prn.Orientation = vbPRORLandscape; // Wider than tall.
502 // Calculate device independent Width-to-Height ratio for picture.
503 PicRatio = pic.Width / pic.Height;
505 // Calculate the dimentions of the printable area in HiMetric.
506 PrnWidth = Prn.ScaleX(Prn.ScaleWidth, Prn.ScaleMode, vbHiMetric);
507 PrnHeight = Prn.ScaleY(Prn.ScaleHeight, Prn.ScaleMode, vbHiMetric);
508 // Calculate device independent Width to Height ratio for printer.
509 PrnRatio = PrnWidth / PrnHeight;
511 // Scale the output to the printable area.
512 if (PicRatio >= PrnRatio)
514 // Scale picture to fit full width of printable area.
515 PrnPicWidth = Prn.ScaleX(PrnWidth, vbHiMetric, Prn.ScaleMode);
516 PrnPicHeight = Prn.ScaleY(PrnWidth / PicRatio, vbHiMetric, Prn.ScaleMode);
520 // Scale picture to fit full height of printable area.
521 PrnPicHeight = Prn.ScaleY(PrnHeight, vbHiMetric, Prn.ScaleMode);
522 PrnPicWidth = Prn.ScaleX(PrnHeight * PicRatio, vbHiMetric, Prn.ScaleMode);
525 // Print the picture using the PaintPicture method.
527 Prn.PaintPicture(pic, 0, 0, PrnPicWidth, PrnPicHeight);
531 // --------------------------------------------------------------------