1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
9 // static char *VB_Name = "CaptureModule";
11 // --------------------------------------------------------------------
12 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
14 // Visual Basic 4.0 16/32 Capture Routines
16 // This module contains several routines for capturing windows into a
17 // picture. All the routines work on both 16 and 32 bit Windows
19 // The routines also have palette support.
21 // CreateBitmapPicture - Creates a picture object from a bitmap and
23 // CaptureWindow - Captures any window given a window handle.
24 // CaptureActiveWindow - Captures the active window on the desktop.
25 // CaptureForm - Captures the entire form.
26 // CaptureClient - Captures the client area of a form.
27 // CaptureScreen - Captures the entire screen.
28 // PrintPictureToFitPage - prints any picture as big as possible on
32 // - No error trapping is included in these routines.
33 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
35 // --- Option Explicit
38 // ::: #ifndef HAS_PALETTEENTRY
45 // ::: } PALETTEENTRY;
46 // ::: #define HAS_PALETTEENTRY
49 // ::: #ifndef HAS_LOGPALETTE
52 // ::: int palVersion;
53 // ::: int palNumEntries;
54 // ::: PALETTEENTRY palPalEntry[255]; // Enough for 256 colors.
56 // ::: #define HAS_LOGPALETTE
59 // ::: #ifndef HAS_GUID
67 // ::: #define HAS_GUID
72 #define RASTERCAPS (38)
73 #define RC_PALETTE (0x100)
74 #define SIZEPALETTE (104)
76 // ::: #ifndef HAS_RECT
84 // ::: #define HAS_RECT
87 long CreateCompatibleDC(long hDC);
88 long CreateCompatibleBitmap(long hDC, long nWidth, long nHeight);
89 long GetDeviceCaps(long hDC, long iCapabilitiy);
90 long GetSystemPaletteEntries(long hDC, long wStartIndex, long wNumEntries, PALETTEENTRY lpPaletteEntries);
91 long CreatePalette(LOGPALETTE lpLogPalette);
92 long SelectObject(long hDC, long hObject);
93 long BitBlt(long hDCDest, long XDest, long YDest, long nWidth, long nHeight, long hDCSrc, long XSrc, long YSrc, long dwRop);
94 long DeleteDC(long hDC);
95 long GetForegroundWindow();
96 long SelectPalette(long hDC, long hPalette, long bForceBackground);
97 long RealizePalette(long hDC);
98 long GetWindowDC(long hWnd);
99 long GetDC(long hWnd);
101 long GetWindowRect(long hWnd, RECT lpRect);
103 long ReleaseDC(long hWnd, long hDC);
104 long GetDesktopWindow();
106 // ::: #ifndef HAS_PicBmp
107 // ::: typedef struct
113 // ::: long Reserved;
115 // ::: #define HAS_PicBmp
118 long OleCreatePictureIndirect(PicBmp PicDesc, GUID RefIID, long fPictureOwnsHandle, IPicture IPic);
122 #define RASTERCAPS (38)
123 #define RC_PALETTE (0x100)
124 #define SIZEPALETTE (104)
126 // ::: #ifndef HAS_RECT
127 // ::: typedef struct
134 // ::: #define HAS_RECT
137 int CreateCompatibleDC(int hDC);
138 int CreateCompatibleBitmap(int hDC, int nWidth, int nHeight);
139 int GetDeviceCaps(int hDC, int iCapabilitiy);
140 int GetSystemPaletteEntries(int hDC, int wStartIndex, int wNumEntries, PALETTEENTRY lpPaletteEntries);
141 int CreatePalette(LOGPALETTE lpLogPalette);
142 int SelectObject(int hDC, int hObject);
143 int BitBlt(int hDCDest, int XDest, int YDest, int nWidth, int nHeight, int hDCSrc, int XSrc, int YSrc, long dwRop);
144 int DeleteDC(int hDC);
145 int GetForegroundWindow();
146 int SelectPalette(int hDC, int hPalette, int bForceBackground);
147 int RealizePalette(int hDC);
148 int GetWindowDC(int hWnd);
151 int GetWindowRect(int hWnd, RECT lpRect);
153 int ReleaseDC(int hWnd, int hDC);
154 int GetDesktopWindow();
156 // ::: #ifndef HAS_PicBmp
157 // ::: typedef struct
165 // ::: #define HAS_PicBmp
168 int OleCreatePictureIndirect(PicBmp PictDesc, GUID RefIID, int fPictureOwnsHandle, IPicture IPic);
172 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
174 // CreateBitmapPicture
175 // - Creates a bitmap type Picture object from a bitmap and
179 // - Handle to a bitmap.
182 // - Handle to a Palette.
183 // - Can be null if the bitmap doesn't use a palette.
186 // - Returns a Picture object containing the bitmap.
187 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
190 Picture CreateBitmapPicture(long hBmp, long hPal)
192 Picture CreateBitmapPicture;
197 Picture CreateBitmapPicture(int hBmp, int hPal)
199 Picture CreateBitmapPicture;
206 // IPicture requires a reference to "Standard OLE Types."
210 // Fill in with IDispatch Interface ID.
212 IID_IDispatch.Data1 = 0x20400;
213 IID_IDispatch.Data4[0] = 0xC0;
214 IID_IDispatch.Data4[7] = 0x46;
217 // Fill Pic with necessary parts.
219 pic.Size = sizeof(pic); // Length of structure.
220 pic.Type = vbPicTypeBitmap; // Type of Picture (bitmap).
221 pic.hBmp = hBmp; // Handle to bitmap.
222 pic.hPal = hPal; // Handle to palette (may be null).
225 // Create Picture object.
226 r = OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic);
228 // Return the new Picture object.
229 CreateBitmapPicture = IPic;
231 return CreateBitmapPicture;
234 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
237 // - Captures any portion of a window.
240 // - Handle to the window to be captured.
243 // - If True CaptureWindow captures from the client area of the
245 // - If False CaptureWindow captures from the entire window.
247 // LeftSrc, TopSrc, WidthSrc, HeightSrc
248 // - Specify the portion of the window to capture.
249 // - Dimensions need to be specified in pixels.
252 // - Returns a Picture object containing a bitmap of the specified
253 // portion of the window that was captured.
254 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
258 Picture CaptureWindow(long hWndSrc, boolean Client, long LeftSrc, long TopSrc, long WidthSrc, long HeightSrc)
260 Picture CaptureWindow;
271 long PaletteSizeScrn;
274 Picture CaptureWindow(int hWndSrc, boolean Client, int LeftSrc, int TopSrc, long WidthSrc, long HeightSrc)
276 Picture CaptureWindow;
292 // Depending on the value of Client get the proper device context.
295 hDCSrc = GetDC(hWndSrc); // Get device context for client area.
299 hDCSrc = GetWindowDC(hWndSrc); // Get device context for entire
303 // Create a memory device context for the copy process.
304 hDCMemory = CreateCompatibleDC(hDCSrc);
305 // Create a bitmap and place it in the memory DC.
306 hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc);
307 hBmpPrev = SelectObject(hDCMemory, hBmp);
309 // Get screen properties.
310 RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS); // Raster
312 HasPaletteScrn = RasterCapsScrn & RC_PALETTE; // Palette
314 PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE); // Size of
317 // If the screen has a palette make a copy and realize it.
318 if (HasPaletteScrn && (PaletteSizeScrn == 256))
320 // Create a copy of the system palette.
321 LogPal.palVersion = 0x300;
322 LogPal.palNumEntries = 256;
323 r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry[0]);
324 hPal = CreatePalette(LogPal);
325 // Select the new palette into the memory DC and realize it.
326 hPalPrev = SelectPalette(hDCMemory, hPal, 0);
327 r = RealizePalette(hDCMemory);
330 // Copy the on-screen image into the memory DC.
331 r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy);
333 // Remove the new copy of the on-screen image.
334 hBmp = SelectObject(hDCMemory, hBmpPrev);
336 // If the screen has a palette get back the palette that was
337 // selected in previously.
338 if (HasPaletteScrn && (PaletteSizeScrn == 256))
340 hPal = SelectPalette(hDCMemory, hPalPrev, 0);
343 // Release the device context resources back to the system.
344 r = DeleteDC(hDCMemory);
345 r = ReleaseDC(hWndSrc, hDCSrc);
347 // Call CreateBitmapPicture to create a picture object from the
348 // bitmap and palette handles. Then return the resulting picture
351 CaptureWindow = CreateBitmapPicture(hBmp, hPal);
353 return CaptureWindow;
356 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
359 // - Captures the entire screen.
362 // - Returns a Picture object containing a bitmap of the screen.
363 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
365 Picture CaptureScreen()
367 Picture CaptureScreen;
377 // Get a handle to the desktop window.
378 hWndScreen = GetDesktopWindow();
380 // Call CaptureWindow to capture the entire desktop give the handle
381 // and return the resulting Picture object.
383 CaptureScreen = CaptureWindow(hWndScreen, False, 0, 0, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY);
385 return CaptureScreen;
388 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
391 // - Captures an entire form including title bar and border.
394 // - The Form object to capture.
397 // - Returns a Picture object containing a bitmap of the entire
399 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
401 Picture CaptureForm(Form frmSrc)
405 // Call CaptureWindow to capture the entire form given its window
406 // handle and then return the resulting Picture object.
407 CaptureForm = CaptureWindow(frmSrc.hWnd, False, 0, 0, frmSrc.ScaleX(frmSrc.Width, vbTwips, vbPixels), frmSrc.ScaleY(frmSrc.Height, vbTwips, vbPixels));
412 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
415 // - Captures the client area of a form.
418 // - The Form object to capture.
421 // - Returns a Picture object containing a bitmap of the form's
423 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
425 Picture CaptureClient(Form frmSrc)
427 Picture CaptureClient;
429 // Call CaptureWindow to capture the client area of the form given
430 // its window handle and return the resulting Picture object.
431 CaptureClient = CaptureWindow(frmSrc.hWnd, True, 0, 0, frmSrc.ScaleX(frmSrc.ScaleWidth, frmSrc.ScaleMode, vbPixels), frmSrc.ScaleY(frmSrc.ScaleHeight, frmSrc.ScaleMode, vbPixels));
433 return CaptureClient;
438 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
440 // CaptureActiveWindow
441 // - Captures the currently active window on the screen.
444 // - Returns a Picture object containing a bitmap of the active
446 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
448 Picture CaptureActiveWindow()
450 Picture CaptureActiveWindow;
463 // Get a handle to the active/foreground window.
464 hWndActive = GetForegroundWindow();
466 // Get the dimensions of the window.
467 r = GetWindowRect(hWndActive, RectActive);
469 // Call CaptureWindow to capture the active window given its
470 // handle and return the Resulting Picture object.
471 CaptureActiveWindow = CaptureWindow(hWndActive, False, 0, 0, RectActive.right - RectActive.left, RectActive.bottom - RectActive.top);
473 return CaptureActiveWindow;
478 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
481 // PrintPictureToFitPage
482 // - Prints a Picture object as big as possible.
485 // - Destination Printer object.
488 // - Source Picture object.
489 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
491 void PrintPictureToFitPage(Printer Prn, Picture pic)
493 #define vbHiMetric (8)
501 // Determine if picture should be printed in landscape or portrait
502 // and set the orientation.
503 if (pic.Height >= pic.Width)
505 Prn.Orientation = vbPRORPortrait; // Taller than wide.
509 Prn.Orientation = vbPRORLandscape; // Wider than tall.
512 // Calculate device independent Width-to-Height ratio for picture.
513 PicRatio = pic.Width / pic.Height;
515 // Calculate the dimentions of the printable area in HiMetric.
516 PrnWidth = Prn.ScaleX(Prn.ScaleWidth, Prn.ScaleMode, vbHiMetric);
517 PrnHeight = Prn.ScaleY(Prn.ScaleHeight, Prn.ScaleMode, vbHiMetric);
518 // Calculate device independent Width to Height ratio for printer.
519 PrnRatio = PrnWidth / PrnHeight;
521 // Scale the output to the printable area.
522 if (PicRatio >= PrnRatio)
524 // Scale picture to fit full width of printable area.
525 PrnPicWidth = Prn.ScaleX(PrnWidth, vbHiMetric, Prn.ScaleMode);
526 PrnPicHeight = Prn.ScaleY(PrnWidth / PicRatio, vbHiMetric, Prn.ScaleMode);
530 // Scale picture to fit full height of printable area.
531 PrnPicHeight = Prn.ScaleY(PrnHeight, vbHiMetric, Prn.ScaleMode);
532 PrnPicWidth = Prn.ScaleX(PrnHeight * PicRatio, vbHiMetric, Prn.ScaleMode);
535 // Print the picture using the PaintPicture method.
537 Prn.PaintPicture(pic, 0, 0, PrnPicWidth, PrnPicHeight);
541 // --------------------------------------------------------------------