VC++在win7下该如何使用

VC++在win7下该如何使用,第1张

Win7支持VC 的,它的MFC库只是对Win32 API的封装而已,为的是面向对象(提供类),如果Win7不支持它,那么起码有80%的Windows桌面软件不能用于Win7了。Visual C 6.0企业版就可以用在Win7上吧,我没试过。VS和VC的区别在于前者是个强大的集成IDE,提供了VC 、VB和VC#的开发环境,主要是微软的.NET战略,VS对VC 的支持不好,据微软说VS 2010会加大对VC 的支持,但当前行业做Windows开发的,大多都用Visual C 6.0,而VS主要用来做Web开发和Windows Mobile移动开发的。C语言面向过程,简单的说就是一大堆函数;C 是C的扩展,主要是加入了面向对象的思想,有了string、istream类等等,对于开发人员方便多了,因为它把相关函数组织成类,更大地减少了记忆负担。

//PrintScreen.h 文件

#pragma once

#include <string>

using namespace std

class PrintScreen

{

public:

PrintScreen()

~PrintScreen()

public:

bool GetBmp( string sBmpName)

protected:

bool WriteBmpToFile(HBITMAP&m_hBitmap)

public:

string sBmpName

}

//PrintScreen.cpp 文件

#include "StdAfx.h"

#include "PrintScreen.h"

PrintScreen::PrintScreen()

{

}

PrintScreen::~PrintScreen()

{

}

bool PrintScreen::GetBmp( string sBmpName) //获得图片

{

bool bRet = true

HBITMAP m_hBitmap = NULL

do

{

this->sBmpName = sBmpName

//图片的句柄

HWND hWnd = ::GetDesktopWindow() //获得屏幕的HWND.

HWND hWndF = ::GetForegroundWindow() //获得当前活动窗口

HDC hScreenDC = ::GetDC(hWnd) //获得屏幕的HDC.

HDC MemDC = ::CreateCompatibleDC(hScreenDC)

RECT rect//要截屏的矩形

::GetWindowRect(hWndF,&rect) //获得当前活动窗口的大小(截取的大小位置)

//::GetWindowRect(hWnd,&rect) //截取桌面

SIZE screensize

screensize.cx = rect.right - rect.left

screensize.cy = rect.bottom - rect.top

do

{

m_hBitmap = ::CreateCompatibleBitmap( hScreenDC, screensize.cx, screensize.cy )

}while( m_hBitmap == NULL )

HGDIOBJ hOldBMP = ::SelectObject( MemDC, m_hBitmap )

::BitBlt( MemDC, 0, 0, screensize.cx, screensize.cy, hScreenDC, rect.left, rect.top, SRCCOPY )

::SelectObject( MemDC, hOldBMP )

::DeleteObject( MemDC )

::ReleaseDC( hWnd, hScreenDC )

if ( !( this->WriteBmpToFile( m_hBitmap) ) )//保存文件

{

bRet = true

break

}

} while ( false )

DeleteObject( m_hBitmap )

return bRet

}

bool PrintScreen::WriteBmpToFile( HBITMAP &m_hBitmap ) //保存bmp图片

{

bool bRet = true

do

{

HDC hDC = ::CreateDC( "DISPLAY", NULL,NULL,NULL )

int iBits = ::GetDeviceCaps( hDC, BITSPIXEL ) * ::GetDeviceCaps( hDC, PLANES )//当前分辨率下每个像素所占字节数

::DeleteDC( hDC )

WORD wBitCount//位图中每个像素所占字节数

if ( iBits <= 1 )

wBitCount = 1

else if ( iBits <= 4 )

wBitCount = 4

else if ( iBits <= 8 )

wBitCount = 8

else if ( iBits <= 24 )

wBitCount = 24

else

wBitCount = iBits

DWORD dwPaletteSize=0 //调色板大小,位图中像素字节大小

if ( wBitCount <= 8 )

dwPaletteSize = ( 1 <<wBitCount) * sizeof( RGBQUAD )

BITMAP bm //位图属性结构

::GetObject( m_hBitmap, sizeof( bm ), ( LPSTR )&bm )

BITMAPINFOHEADER bi, bi1 //位图信息头结构

bi.biSize = sizeof( BITMAPINFOHEADER )

bi.biWidth = bm.bmWidth

bi.biHeight = bm.bmHeight

bi.biPlanes = 1

bi.biBitCount = wBitCount

bi.biCompression = BI_RGB //BI_RGB表示位图没有压缩

bi.biSizeImage = 0

bi.biXPelsPerMeter = 0

bi.biYPelsPerMeter = 0

bi.biClrUsed = 0

bi.biClrImportant = 0

bi1 = bi

bi1.biBitCount = 24

DWORD dwBmBitsSize = ( ( bm.bmWidth * wBitCount + 31 ) / 32 ) * 4 * bm.bmHeight

HANDLE hDib= ::GlobalAlloc( GHND, dwBmBitsSize + dwPaletteSize + sizeof( BITMAPINFOHEADER ) ) //为位图内容分配内存

LPBITMAPINFOHEADER lpbi = ( LPBITMAPINFOHEADER )GlobalLock( hDib )

*lpbi = bi

HANDLE hPal = ::GetStockObject( DEFAULT_PALETTE )// 处理调色板

HANDLE hOldPal = NULL

if ( hPal )

{

hDC = ::GetDC( NULL )

hOldPal = SelectPalette( hDC,(HPALETTE)hPal, FALSE )

RealizePalette( hDC )

}

int nOutputBytes = 0

unsigned char *pJpp, *m_pDibBits

pJpp = new unsigned char [ dwBmBitsSize ]

m_pDibBits = new unsigned char [ dwBmBitsSize ]

::GetDIBits( hDC, m_hBitmap, 0, ( unsigned int ) bm.bmHeight,(LPSTR)lpbi + sizeof( BITMAPINFOHEADER ) + dwPaletteSize, ( BITMAPINFO* )lpbi, DIB_RGB_COLORS )// 获取该调色板下新的像素值

if ( hOldPal ) //恢复调色板

{

SelectPalette( hDC, ( HPALETTE )hOldPal, TRUE )

RealizePalette( hDC )

::ReleaseDC( NULL, hDC )

}

BITMAPFILEHEADER bmfHdr //位图文件头结构0

bmfHdr.bfType = 0x4D42 // "BM"设置位图文件头

DWORD 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

HANDLE hFile = ::CreateFile( this->sBmpName.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)//创建位图文件

DWORD dwWritten

WriteFile( hFile, ( LPSTR )&bmfHdr, sizeof( BITMAPFILEHEADER ), &dwWritten, NULL )// 写入位图文件头

WriteFile( hFile, ( LPSTR )lpbi, dwDIBSize, &dwWritten, NULL ) // 写入位图文件其余内容

GlobalUnlock( hDib ) //清除

GlobalFree( hDib )

CloseHandle( hFile )

delete []pJpp

delete []m_pDibBits

}while( false )

return bRet

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/10056287.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存