1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
7 static char *VB_Name = "CaptureModule";
8 // --------------------------------------------------------------------
9 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
11 // Visual Basic 4.0 16/32 Capture Routines
13 // This module contains several routines for capturing windows into a
14 // picture. All the routines work on both 16 and 32 bit Windows
16 // The routines also have palette support.
18 // CreateBitmapPicture - Creates a picture object from a bitmap and
20 // CaptureWindow - Captures any window given a window handle.
21 // CaptureActiveWindow - Captures the active window on the desktop.
22 // CaptureForm - Captures the entire form.
23 // CaptureClient - Captures the client area of a form.
24 // CaptureScreen - Captures the entire screen.
25 // PrintPictureToFitPage - prints any picture as big as possible on
29 // - No error trapping is included in these routines.
30 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
32 // --- Option Explicit
35 // ::: #ifndef HAS_PALETTEENTRY
42 // ::: } PALETTEENTRY;
43 // ::: #define HAS_PALETTEENTRY
46 // ::: #ifndef HAS_LOGPALETTE
49 // ::: int palVersion;
50 // ::: int palNumEntries;
51 // ::: PALETTEENTRY palPalEntry[255]; // Enough for 256 colors.
53 // ::: #define HAS_LOGPALETTE
56 // ::: #ifndef HAS_GUID
64 // ::: #define HAS_GUID
69 #define RASTERCAPS (38)
70 #define RC_PALETTE (0x100)
71 #define SIZEPALETTE (104)
73 // ::: #ifndef HAS_RECT
81 // ::: #define HAS_RECT
84 long CreateCompatibleDC(long hDC);
85 long CreateCompatibleBitmap(long hDC, long nWidth, long nHeight);
86 long GetDeviceCaps(long hDC, long iCapabilitiy);
87 long GetSystemPaletteEntries(long hDC, long wStartIndex, long wNumEntries, PALETTEENTRY lpPaletteEntries);
88 long CreatePalette(LOGPALETTE lpLogPalette);
89 long SelectObject(long hDC, long hObject);
90 long BitBlt(long hDCDest, long XDest, long YDest, long nWidth, long nHeight, long hDCSrc, long XSrc, long YSrc, long dwRop);
91 long DeleteDC(long hDC);
92 long GetForegroundWindow();
93 long SelectPalette(long hDC, long hPalette, long bForceBackground);
94 long RealizePalette(long hDC);
95 long GetWindowDC(long hWnd);
96 long GetDC(long hWnd);
97 long GetWindowRect(long hWnd, RECT lpRect);
98 long ReleaseDC(long hWnd, long hDC);
99 long GetDesktopWindow();
101 // ::: #ifndef HAS_PicBmp
102 // ::: typedef struct
108 // ::: long Reserved;
110 // ::: #define HAS_PicBmp
113 long OleCreatePictureIndirect(PicBmp PicDesc, GUID RefIID, long fPictureOwnsHandle, IPicture IPic);
117 #define RASTERCAPS (38)
118 #define RC_PALETTE (0x100)
119 #define SIZEPALETTE (104)
121 // ::: #ifndef HAS_RECT
122 // ::: typedef struct
129 // ::: #define HAS_RECT
132 int CreateCompatibleDC(int hDC);
133 int CreateCompatibleBitmap(int hDC, int nWidth, int nHeight);
134 int GetDeviceCaps(int hDC, int iCapabilitiy);
135 int GetSystemPaletteEntries(int hDC, int wStartIndex, int wNumEntries, PALETTEENTRY lpPaletteEntries);
136 int CreatePalette(LOGPALETTE lpLogPalette);
137 int SelectObject(int hDC, int hObject);
138 int BitBlt(int hDCDest, int XDest, int YDest, int nWidth, int nHeight, int hDCSrc, int XSrc, int YSrc, long dwRop);
139 int DeleteDC(int hDC);
140 int GetForegroundWindow();
141 int SelectPalette(int hDC, int hPalette, int bForceBackground);
142 int RealizePalette(int hDC);
143 int GetWindowDC(int hWnd);
145 int GetWindowRect(int hWnd, RECT lpRect);
146 int ReleaseDC(int hWnd, int hDC);
147 int GetDesktopWindow();
149 // ::: #ifndef HAS_PicBmp
150 // ::: typedef struct
158 // ::: #define HAS_PicBmp
161 int OleCreatePictureIndirect(PicBmp PictDesc, GUID RefIID, int fPictureOwnsHandle, IPicture IPic);
165 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
167 // CreateBitmapPicture
168 // - Creates a bitmap type Picture object from a bitmap and
172 // - Handle to a bitmap.
175 // - Handle to a Palette.
176 // - Can be null if the bitmap doesn't use a palette.
179 // - Returns a Picture object containing the bitmap.
180 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
183 Picture CreateBitmapPicture(long hBmp, long hPal)
185 Picture CreateBitmapPicture;
190 Picture CreateBitmapPicture(int hBmp, int hPal)
192 Picture CreateBitmapPicture;
199 // IPicture requires a reference to "Standard OLE Types."
203 // Fill in with IDispatch Interface ID.
205 IID_IDispatch.Data1 = 0x20400;
206 IID_IDispatch.Data4[0] = 0xC0;
207 IID_IDispatch.Data4[7] = 0x46;
210 // Fill Pic with necessary parts.
212 pic.Size = Len(pic); // Length of structure.
213 pic.Type = vbPicTypeBitmap; // Type of Picture (bitmap).
214 pic.hBmp = hBmp; // Handle to bitmap.
215 pic.hPal = hPal; // Handle to palette (may be null).
218 // Create Picture object.
219 r = OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic);
221 // Return the new Picture object.
222 CreateBitmapPicture = IPic;
224 return CreateBitmapPicture;
227 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
230 // - Captures any portion of a window.
233 // - Handle to the window to be captured.
236 // - If True CaptureWindow captures from the client area of the
238 // - If False CaptureWindow captures from the entire window.
240 // LeftSrc, TopSrc, WidthSrc, HeightSrc
241 // - Specify the portion of the window to capture.
242 // - Dimensions need to be specified in pixels.
245 // - Returns a Picture object containing a bitmap of the specified
246 // portion of the window that was captured.
247 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
251 Picture CaptureWindow(long hWndSrc, boolean Client, long LeftSrc, long TopSrc, long WidthSrc, long HeightSrc)
253 Picture CaptureWindow;
264 long PaletteSizeScrn;
267 Picture CaptureWindow(int hWndSrc, boolean Client, int LeftSrc, int TopSrc, long WidthSrc, long HeightSrc)
269 Picture CaptureWindow;
285 // Depending on the value of Client get the proper device context.
288 hDCSrc = GetDC(hWndSrc); // Get device context for client area.
292 hDCSrc = GetWindowDC(hWndSrc); // Get device context for entire
296 // Create a memory device context for the copy process.
297 hDCMemory = CreateCompatibleDC(hDCSrc);
298 // Create a bitmap and place it in the memory DC.
299 hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc);
300 hBmpPrev = SelectObject(hDCMemory, hBmp);
302 // Get screen properties.
303 RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS); // Raster
305 HasPaletteScrn = RasterCapsScrn & RC_PALETTE; // Palette
307 PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE); // Size of
310 // If the screen has a palette make a copy and realize it.
311 if (HasPaletteScrn && (PaletteSizeScrn == 256))
313 // Create a copy of the system palette.
314 LogPal.palVersion = 0x300;
315 LogPal.palNumEntries = 256;
316 r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry[0]);
317 hPal = CreatePalette(LogPal);
318 // Select the new palette into the memory DC and realize it.
319 hPalPrev = SelectPalette(hDCMemory, hPal, 0);
320 r = RealizePalette(hDCMemory);
323 // Copy the on-screen image into the memory DC.
324 r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy);
326 // Remove the new copy of the on-screen image.
327 hBmp = SelectObject(hDCMemory, hBmpPrev);
329 // If the screen has a palette get back the palette that was
330 // selected in previously.
331 if (HasPaletteScrn && (PaletteSizeScrn == 256))
333 hPal = SelectPalette(hDCMemory, hPalPrev, 0);
336 // Release the device context resources back to the system.
337 r = DeleteDC(hDCMemory);
338 r = ReleaseDC(hWndSrc, hDCSrc);
340 // Call CreateBitmapPicture to create a picture object from the
341 // bitmap and palette handles. Then return the resulting picture
344 CaptureWindow = CreateBitmapPicture(hBmp, hPal);
346 return CaptureWindow;
349 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
352 // - Captures the entire screen.
355 // - Returns a Picture object containing a bitmap of the screen.
356 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
358 Picture CaptureScreen()
360 Picture CaptureScreen;
370 // Get a handle to the desktop window.
371 hWndScreen = GetDesktopWindow();
373 // Call CaptureWindow to capture the entire desktop give the handle
374 // and return the resulting Picture object.
376 CaptureScreen = CaptureWindow(hWndScreen, False, 0, 0, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY);
378 return CaptureScreen;
381 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
384 // - Captures an entire form including title bar and border.
387 // - The Form object to capture.
390 // - Returns a Picture object containing a bitmap of the entire
392 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
394 Picture CaptureForm(Form frmSrc)
398 // Call CaptureWindow to capture the entire form given its window
399 // handle and then return the resulting Picture object.
400 CaptureForm = CaptureWindow(frmSrc.hWnd, False, 0, 0, frmSrc.ScaleX(frmSrc.Width, vbTwips, vbPixels), frmSrc.ScaleY(frmSrc.Height, vbTwips, vbPixels));
405 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
408 // - Captures the client area of a form.
411 // - The Form object to capture.
414 // - Returns a Picture object containing a bitmap of the form's
416 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
418 Picture CaptureClient(Form frmSrc)
420 Picture CaptureClient;
422 // Call CaptureWindow to capture the client area of the form given
423 // its window handle and return the resulting Picture object.
424 CaptureClient = CaptureWindow(frmSrc.hWnd, True, 0, 0, frmSrc.ScaleX(frmSrc.ScaleWidth, frmSrc.ScaleMode, vbPixels), frmSrc.ScaleY(frmSrc.ScaleHeight, frmSrc.ScaleMode, vbPixels));
426 return CaptureClient;
429 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
431 // CaptureActiveWindow
432 // - Captures the currently active window on the screen.
435 // - Returns a Picture object containing a bitmap of the active
437 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
439 Picture CaptureActiveWindow()
441 Picture CaptureActiveWindow;
454 // Get a handle to the active/foreground window.
455 hWndActive = GetForegroundWindow();
457 // Get the dimensions of the window.
458 r = GetWindowRect(hWndActive, RectActive);
460 // Call CaptureWindow to capture the active window given its
461 // handle and return the Resulting Picture object.
462 CaptureActiveWindow = CaptureWindow(hWndActive, False, 0, 0, RectActive.right - RectActive.left, RectActive.bottom - RectActive.top);
464 return CaptureActiveWindow;
467 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
470 // PrintPictureToFitPage
471 // - Prints a Picture object as big as possible.
474 // - Destination Printer object.
477 // - Source Picture object.
478 // ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
480 void PrintPictureToFitPage(Printer Prn, Picture pic)
482 #define vbHiMetric (8)
490 // Determine if picture should be printed in landscape or portrait
491 // and set the orientation.
492 if (pic.Height >= pic.Width)
494 Prn.Orientation = vbPRORPortrait; // Taller than wide.
498 Prn.Orientation = vbPRORLandscape; // Wider than tall.
501 // Calculate device independent Width-to-Height ratio for picture.
502 PicRatio = pic.Width / pic.Height;
504 // Calculate the dimentions of the printable area in HiMetric.
505 PrnWidth = Prn.ScaleX(Prn.ScaleWidth, Prn.ScaleMode, vbHiMetric);
506 PrnHeight = Prn.ScaleY(Prn.ScaleHeight, Prn.ScaleMode, vbHiMetric);
507 // Calculate device independent Width to Height ratio for printer.
508 PrnRatio = PrnWidth / PrnHeight;
510 // Scale the output to the printable area.
511 if (PicRatio >= PrnRatio)
513 // Scale picture to fit full width of printable area.
514 PrnPicWidth = Prn.ScaleX(PrnWidth, vbHiMetric, Prn.ScaleMode);
515 PrnPicHeight = Prn.ScaleY(PrnWidth / PicRatio, vbHiMetric, Prn.ScaleMode);
519 // Scale picture to fit full height of printable area.
520 PrnPicHeight = Prn.ScaleY(PrnHeight, vbHiMetric, Prn.ScaleMode);
521 PrnPicWidth = Prn.ScaleX(PrnHeight * PicRatio, vbHiMetric, Prn.ScaleMode);
524 // Print the picture using the PaintPicture method.
525 Prn_PaintPicture(pic, 0, 0, PrnPicWidth, PrnPicHeight);
528 // --------------------------------------------------------------------