?? getimagedlg.cpp
字號:
// getimageDlg.cpp : implementation file
//
#include "stdafx.h"
#include "getimage.h"
#include "getimageDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGetimageDlg dialog
CGetimageDlg::CGetimageDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGetimageDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGetimageDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CGetimageDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGetimageDlg)
DDX_Control(pDX, IDC_PIC, m_Pic);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGetimageDlg, CDialog)
//{{AFX_MSG_MAP(CGetimageDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_GET, OnGet)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGetimageDlg message handlers
BOOL CGetimageDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CGetimageDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CGetimageDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//把選定的屏幕區(qū)域拷貝到位圖中的函數(shù)
//lprect 代表選定區(qū)域
HBITMAP CGetimageDlg::Copybitmap(LPRECT lprect)
{
HDC hscrdc, hmemdc;
// 屏幕和內存設備描述表
HBITMAP hbitmap, holdbitmap;
// 位圖句柄
int nx, ny, nx2, ny2;
// 選定區(qū)域坐標
int nwidth, nheight;
// 位圖寬度和高度
int xscrn, yscrn;
// 屏幕分辨率
// 確保選定區(qū)域不為空矩形
if (IsRectEmpty(lprect))
return NULL;
//為屏幕創(chuàng)建設備描述表
hscrdc = CreateDC("display", NULL, NULL, NULL);
//為屏幕設備描述表創(chuàng)建兼容的內存設備描述表
hmemdc = CreateCompatibleDC(hscrdc);
// 獲得選定區(qū)域坐標
nx = lprect->left;
ny = lprect->top;
nx2 = lprect->right;
ny2 = lprect->bottom;
// 獲得屏幕分辨率
xscrn = GetDeviceCaps(hscrdc, HORZRES);
yscrn = GetDeviceCaps(hscrdc, VERTRES);
//確保選定區(qū)域是可見的
if (nx < 0)
nx = 0;
if (ny < 0)
ny = 0;
if (nx2 > xscrn)
nx2 = xscrn;
if (ny2 > yscrn)
ny2 = yscrn;
nwidth = nx2 - nx;
nheight = ny2 - ny;
// 創(chuàng)建一個與屏幕設備描述表兼容的位圖
hbitmap = CreateCompatibleBitmap(hscrdc, nwidth, nheight);
// 把新位圖選到內存設備描述表中
holdbitmap = (HBITMAP)SelectObject(hmemdc, hbitmap);
// 把屏幕設備描述表拷貝到內存設備描述表中
BitBlt(hmemdc, 0, 0, nwidth, nheight,hscrdc, nx, ny, SRCCOPY);
//得到屏幕位圖的句柄
hbitmap = (HBITMAP)SelectObject(hmemdc, holdbitmap);
//清除
DeleteDC(hscrdc);
DeleteDC(hmemdc);
// 返回位圖句柄
return hbitmap;
}
//把屏幕內容粘貼到剪貼板上
void CGetimageDlg::Toclip(HBITMAP hbitmap)
{
if (::OpenClipboard(m_hWnd)) //hwnd為程序窗口句柄
{
//清空剪貼板
EmptyClipboard();
//把屏幕內容粘貼到剪貼板上,hbitmap 為剛才的屏幕位圖句柄
SetClipboardData(CF_BITMAP, hbitmap);
//關閉剪貼板
CloseClipboard();
}
}
//把屏幕內容以位圖格式存到磁盤文件上
//lpfilename 為位圖文件名,hbitmap 為剛才的屏幕位圖句柄
int CGetimageDlg::Tofile(HBITMAP hbitmap , LPSTR lpfilename)
{
HDC hdc; //設備描述表
int ibits;
WORD wbitcount; //當前顯示分辨率下每個像素所占字節(jié)數(shù)
//位圖中每個像素所占字節(jié)數(shù),定義調色板大小,位圖中像素字節(jié)大小,位圖文件大小 ,寫入文件字節(jié)數(shù)
DWORD dwpalettesize=0, dwbmbitssize, dwdibsize, dwwritten;
BITMAP bitmap; //位圖屬性結構
BITMAPFILEHEADER bmfhdr; //位圖文件頭結構
BITMAPINFOHEADER bi; //位圖信息頭結構
LPBITMAPINFOHEADER lpbi; //指向位圖信息頭結構
//定義文件,分配內存句柄,調色板句柄
HANDLE fh, hdib, hpal,holdpal=NULL;
//計算位圖文件每個像素所占字節(jié)數(shù)
hdc = CreateDC("display",NULL,NULL,NULL);
ibits = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
DeleteDC(hdc);
if (ibits <= 1)
wbitcount = 1;
else if (ibits <= 4)
wbitcount = 4;
else if (ibits <= 8)
wbitcount = 8;
else if (ibits <= 16)
wbitcount = 16;
else if (ibits <= 24)
wbitcount = 24;
else
wbitcount = 32;
//計算調色板大小
if (wbitcount <= 8)
dwpalettesize = (1 << wbitcount) * sizeof(RGBQUAD);
//設置位圖信息頭結構
GetObject(hbitmap, sizeof(BITMAP), (LPSTR)&bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bitmap.bmWidth;
bi.biHeight = bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wbitcount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwbmbitssize = ((bitmap.bmWidth * wbitcount+31)/32)* 4 * bitmap.bmHeight ;
//為位圖內容分配內存
hdib = GlobalAlloc(GHND,dwbmbitssize + dwpalettesize +
sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hdib);
*lpbi = bi;
// 處理調色板
hpal = GetStockObject(DEFAULT_PALETTE);
if (hpal)
{
hdc = ::GetDC(NULL);
holdpal = SelectPalette(hdc, (HPALETTE)hpal, false);
RealizePalette(hdc);
}
// 獲取該調色板下新的像素值
GetDIBits(hdc, hbitmap, 0, (UINT) bitmap.bmHeight,(LPSTR)lpbi +
sizeof(BITMAPINFOHEADER)+dwpalettesize,(BITMAPINFO*)lpbi, DIB_RGB_COLORS);
//恢復調色板
if (holdpal)
{
SelectPalette(hdc, (HPALETTE)holdpal, true);
RealizePalette(hdc);
::ReleaseDC(NULL, hdc);
}
//創(chuàng)建位圖文件
fh = CreateFile(lpfilename, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|
FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh == INVALID_HANDLE_VALUE)
return false;
// 設置位圖文件頭
bmfhdr.bfType = 0x4d42; // "bm"
dwdibsize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+
dwpalettesize + dwbmbitssize;
bmfhdr.bfSize = dwdibsize;
bmfhdr.bfReserved1 = 0;
bmfhdr.bfReserved2 = 0;
bmfhdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) +
(DWORD)sizeof(BITMAPINFOHEADER)+ dwpalettesize;
// 寫入位圖文件頭
WriteFile(fh, (LPSTR)&bmfhdr, sizeof(BITMAPFILEHEADER), &dwwritten, NULL);
// 寫入位圖文件其余內容
WriteFile(fh, (LPSTR)lpbi, dwdibsize, &dwwritten, NULL);
//清除
GlobalUnlock(hdib);
GlobalFree(hdib);
CloseHandle(fh);
return true;
}
/*
//把屏幕內容以位圖格式存到磁盤文件上
//lpfilename 為位圖文件名,hbitmap 為剛才的屏幕位圖句柄
int CGetimageDlg::Tofile(HBITMAP hbitmap , LPSTR lpfilename)
{
HDC hdc; //設備描述表
int ibits;
WORD wbitcount; //當前顯示分辨率下每個像素所占字節(jié)數(shù)
//位圖中每個像素所占字節(jié)數(shù),定義調色板大小,位圖中像素字節(jié)大小,位圖文件大小 ,寫入文件字節(jié)數(shù)
DWORD dwpalettesize=0, dwbmbitssize, dwdibsize, dwwritten;
BITMAP bitmap; //位圖屬性結構
BITMAPFILEHEADER bmfhdr; //位圖文件頭結構
BITMAPINFOHEADER bi; //位圖信息頭結構
LPBITMAPINFOHEADER lpbi; //指向位圖信息頭結構
//定義文件,分配內存句柄,調色板句柄
HANDLE fh, hdib, hpal,holdpal=NULL;
//計算位圖文件每個像素所占字節(jié)數(shù)
hdc = CreateDC("display",NULL,NULL,NULL);
ibits = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
DeleteDC(hdc);
if (ibits <= 1)
wbitcount = 1;
else if (ibits <= 4)
wbitcount = 4;
else if (ibits <= 8)
wbitcount = 8;
else if (ibits <= 16)
wbitcount = 16;
else if (ibits <= 24)
wbitcount = 24;
else
wbitcount = 32;
//計算調色板大小
if (wbitcount <= 8)
dwpalettesize = (1 << wbitcount) * sizeof(RGBQUAD);
//設置位圖信息頭結構
GetObject(hbitmap, sizeof(BITMAP), (LPSTR)&bitmap);
bi.biSize = sizeof(LPBITMAPINFOHEADER);
bi.biWidth = bitmap.bmWidth;
bi.biHeight = bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wbitcount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwbmbitssize = ((bitmap.bmWidth * wbitcount+31)/32)* 4 * bitmap.bmHeight ;
//為位圖內容分配內存
hdib = GlobalAlloc(GHND,dwbmbitssize + dwpalettesize + sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hdib);
*lpbi = bi;
// 處理調色板
hpal = GetStockObject(DEFAULT_PALETTE);
if (hpal)
{
hdc = ::GetDC(NULL);
holdpal = SelectPalette(hdc, (HPALETTE)hpal, false);
RealizePalette(hdc);
}
// 獲取該調色板下新的像素值
GetDIBits(hdc, hbitmap, 0, (UINT) bitmap.bmHeight,(LPSTR)lpbi +
sizeof(BITMAPINFOHEADER)+dwpalettesize,(BITMAPINFO*)lpbi, DIB_RGB_COLORS);
//恢復調色板
if (holdpal)
{
SelectPalette(hdc, (HPALETTE)holdpal, true);
RealizePalette(hdc);
::ReleaseDC(NULL, hdc);
}
//創(chuàng)建位圖文件
fh = CreateFile(lpfilename, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh == INVALID_HANDLE_VALUE)
return false;
// 設置位圖文件頭
bmfhdr.bfType = 0x4d42; // "bm"
dwdibsize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+ dwpalettesize + dwbmbitssize;
bmfhdr.bfSize = dwdibsize;
bmfhdr.bfReserved1 = 0;
bmfhdr.bfReserved2 = 0;
bmfhdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER)+ dwpalettesize;
// 寫入位圖文件頭
WriteFile(fh, (LPSTR)&bmfhdr, sizeof(BITMAPFILEHEADER), &dwwritten, NULL);
// 寫入位圖文件其余內容
WriteFile(fh, (LPSTR)lpbi, dwdibsize, &dwwritten, NULL);
//清除
GlobalUnlock(hdib);
GlobalFree(hdib);
CloseHandle(fh);
return true;
}
*/
void CGetimageDlg::OnGet()
{
// TODO: Add your control notification handler code here
LPRECT temprect;
HBITMAP tempmap;
//假設要截圖的區(qū)域為(0,0)至(300,400),具體實踐中可由用戶選擇
temprect = new RECT();
temprect->bottom = 300;
temprect->left = 0;
temprect->right = 400;
temprect->top = 0;
tempmap = Copybitmap(temprect);
//拷貝到剪貼板
Toclip(tempmap);
//顯示獲取的屏幕
m_Pic.SetBitmap(tempmap);
//生成文件保存在C盤根目錄下
Tofile(tempmap,"c:\\temp.bmp");
delete temprect;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -