📄 copy.c
字号:
//**********************************************************************
//
// copy.c
//
// Source file for Device-Independent Bitmap (DIB) API. Provides
// the following functions:
//
// CopyWindowToDIB() - Copies a window to a DIB
// CopyScreenToDIB() - Copies entire screen to a DIB
// CopyWindowToBitmap()- Copies a window to a standard Bitmap
// CopyScreenToBitmap()- Copies entire screen to a standard Bitmap
// PaintDIB() - Displays DIB in the specified DC
// PaintBitmap() - Displays bitmap in the specified DC
//
// The following functions are called from DIBUTIL.C:
//
// DIBToBitmap() - Creates a bitmap from a DIB
// BitmapToDIB() - Creates a DIB from a bitmap
// DIBWidth() - Gets the width of the DIB
// DIBHeight() - Gets the height of the DIB
// CreateDIBPalette() - Gets the DIB's palette
// GetSystemPalette() - Gets the current palette
//
// Development Team: Mark Bader
// Patrick Schreiber
// Garrett McAuliffe
// Eric Flo
// Tony Claflin
//
// Written by Microsoft Product Support Services, Developer Support.
// COPYRIGHT:
//
// (C) Copyright Microsoft Corp. 1993. All rights reserved.
//
// You have a royalty-free right to use, modify, reproduce and
// distribute the Sample Files (and/or any modified version) in
// any way you find useful, provided that you agree that
// Microsoft has no warranty obligations or liability for any
// Sample Application Files which are modified.
//
//**********************************************************************
/* header files */
#include <WINDOWS.H>
#include "DIBUTIL.H"
#include "DIBAPI.H"
/*************************************************************************
*
* CopyWindowToDIB()
*
* Parameters:
*
* HWND hWnd - specifies the window
*
* WORD fPrintArea - specifies the window area to copy into the device-
* independent bitmap
*
* Return Value:
*
* HDIB - identifies the device-independent bitmap
*
* Description:
*
* This function copies the specified part(s) of the window to a device-
* independent bitmap.
*
* History: Date Author Reason
* 9/15/91 Patrick Schreiber Created
* 9/25/91 Patrick Schreiber Added header and comments
*
************************************************************************/
HDIB FAR CopyWindowToDIB(HWND hWnd, WORD fPrintArea)
{
HDIB hDIB = NULL; // handle to DIB
/* check for a valid window handle */
if (!hWnd)
return NULL;
switch (fPrintArea)
{
case PW_WINDOW: // copy entire window
{
RECT rectWnd;
/* get the window rectangle */
GetWindowRect(hWnd, &rectWnd);
/* get the DIB of the window by calling
* CopyScreenToDIB and passing it the window rect
*/
hDIB = CopyScreenToDIB(&rectWnd);
}
break;
case PW_CLIENT: // copy client area
{
RECT rectClient;
POINT pt1, pt2;
/* get the client area dimensions */
GetClientRect(hWnd, &rectClient);
/* convert client coords to screen coords */
pt1.x = rectClient.left;
pt1.y = rectClient.top;
pt2.x = rectClient.right;
pt2.y = rectClient.bottom;
ClientToScreen(hWnd, &pt1);
ClientToScreen(hWnd, &pt2);
rectClient.left = pt1.x;
rectClient.top = pt1.y;
rectClient.right = pt2.x;
rectClient.bottom = pt2.y;
/* get the DIB of the client area by calling
* CopyScreenToDIB and passing it the client rect
*/
hDIB = CopyScreenToDIB(&rectClient);
}
break;
default: // invalid print area
return NULL;
}
/* return the handle to the DIB */
return hDIB;
}
/*************************************************************************
*
* CopyScreenToDIB()
*
* Parameter:
*
* LPRECT lpRect - specifies the window
*
* Return Value:
*
* HDIB - identifies the device-independent bitmap
*
* Description:
*
* This function copies the specified part of the screen to a device-
* independent bitmap.
*
* History: Date Author Reason
* 9/15/91 Patrick Schreiber Created
* 9/25/91 Patrick Schreiber Added header and comments
* 12/10/91 Patrick Schreiber Released palette
*
************************************************************************/
HDIB FAR CopyScreenToDIB(LPRECT lpRect)
{
HBITMAP hBitmap; // handle to device-dependent bitmap
HPALETTE hPalette; // handle to palette
HDIB hDIB = NULL; // handle to DIB
/* get the device-dependent bitmap in lpRect by calling
* CopyScreenToBitmap and passing it the rectangle to grab
*/
hBitmap = CopyScreenToBitmap(lpRect);
/* check for a valid bitmap handle */
if (!hBitmap)
return NULL;
/* get the current palette */
hPalette = GetSystemPalette();
/* convert the bitmap to a DIB */
hDIB = BitmapToDIB(hBitmap, hPalette);
/* clean up */
DeleteObject(hPalette);
DeleteObject(hBitmap);
/* return handle to the packed-DIB */
return hDIB;
}
/*************************************************************************
*
* CopyWindowToBitmap()
*
* Parameters:
*
* HWND hWnd - specifies the window
*
* WORD fPrintArea - specifies the window area to copy into the device-
* dependent bitmap
*
* Return Value:
*
* HDIB - identifies the device-dependent bitmap
*
* Description:
*
* This function copies the specified part(s) of the window to a device-
* dependent bitmap.
*
* History: Date Author Reason
* 9/15/91 Patrick Schreiber Created
* 9/25/91 Patrick Schreiber Added header and comments
*
************************************************************************/
HBITMAP FAR CopyWindowToBitmap(HWND hWnd, WORD fPrintArea)
{
HBITMAP hBitmap = NULL; // handle to device-dependent bitmap
/* check for a valid window handle */
if (!hWnd)
return NULL;
switch (fPrintArea)
{
case PW_WINDOW: // copy entire window
{
RECT rectWnd;
/* get the window rectangle */
GetWindowRect(hWnd, &rectWnd);
/* get the bitmap of that window by calling
* CopyScreenToBitmap and passing it the window rect
*/
hBitmap = CopyScreenToBitmap(&rectWnd);
}
break;
case PW_CLIENT: // copy client area
{
RECT rectClient;
POINT pt1, pt2;
/* get client dimensions */
GetClientRect(hWnd, &rectClient);
/* convert client coords to screen coords */
pt1.x = rectClient.left;
pt1.y = rectClient.top;
pt2.x = rectClient.right;
pt2.y = rectClient.bottom;
ClientToScreen(hWnd, &pt1);
ClientToScreen(hWnd, &pt2);
rectClient.left = pt1.x;
rectClient.top = pt1.y;
rectClient.right = pt2.x;
rectClient.bottom = pt2.y;
/* get the bitmap of the client area by calling
* CopyScreenToBitmap and passing it the client rect
*/
hBitmap = CopyScreenToBitmap(&rectClient);
}
break;
default: // invalid print area
return NULL;
}
/* return handle to the bitmap */
return hBitmap;
}
/*************************************************************************
*
* CopyScreenToBitmap()
*
* Parameter:
*
* LPRECT lpRect - specifies the window
*
* Return Value:
*
* HDIB - identifies the device-dependent bitmap
*
* Description:
*
* This function copies the specified part of the screen to a device-
* dependent bitmap.
*
* History: Date Author Reason
* 9/15/91 Patrick Schreiber Created
* 9/25/91 Patrick Schreiber Added header and comments
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -