C ++控制台“屏幕截图”问题

时间:2013-11-02 12:17:38

标签: c++ console screen capture

我有这个代码,屏幕截图,它应该工作,但我不知道问题出在哪里 它说“没有目标架构”我认为它有点事情..我运行Windows 7 32位,我正在使用visual studio 2012.谢谢你提前stackoverflow

#include <windef.h>
#include <wingdi.h>
#include <fileapi.h>
#include <Windows.h>
#include <handleapi.h>
#include <iostream>
using namespace std;    

HWND Window;
int main()
{
    char file[] = "c:\\Users\\Mt\Desktop\\j.jpg";
    if(Capture(file) == true)
        cout << "Screen shot successful at " << file << endl;
    else
        cout << "Unknow Error " << endl;

}    

HWND Window;
BOOL Capture(char *file)
{
    HDC hdc;
    HBITMAP bitmap;
    BITMAPINFO bmpinfo;
    LPVOID pBits;
    HDC hdc2;
    DWORD dwWidth, dwHeight, dwBPP, dwNumColors;
    HGDIOBJ gdiobj;
    HANDLE hfile;
    DWORD dwBytes;      

    hdc=CreateDC("DISPLAY", NULL, NULL, NULL);
    if(hdc==NULL) {
        return FALSE;
    }
    dwWidth = GetDeviceCaps(hdc, HORZRES);
    dwHeight = GetDeviceCaps(hdc, VERTRES);
    dwBPP = GetDeviceCaps(hdc, BITSPIXEL);
    if(dwBPP<=8) {
        dwNumColors = GetDeviceCaps(hdc, NUMCOLORS);
        dwNumColors = 256;
    } else {
        dwNumColors = 0;
    }
    hdc2=CreateCompatibleDC(hdc);
    if(hdc2==NULL) {
        DeleteDC(hdc);
        return FALSE;
    }
    bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmpinfo.bmiHeader.biWidth = dwWidth;
    bmpinfo.bmiHeader.biHeight = dwHeight;
    bmpinfo.bmiHeader.biPlanes = 1;
    bmpinfo.bmiHeader.biBitCount = (WORD) dwBPP;
    bmpinfo.bmiHeader.biCompression = BI_RGB;
    bmpinfo.bmiHeader.biSizeImage = 0;
    bmpinfo.bmiHeader.biXPelsPerMeter = 0;
    bmpinfo.bmiHeader.biYPelsPerMeter = 0;
    bmpinfo.bmiHeader.biClrUsed = dwNumColors;
    bmpinfo.bmiHeader.biClrImportant = dwNumColors;
    bitmap = CreateDIBSection(hdc, &bmpinfo, DIB_PAL_COLORS, &pBits, NULL, 0);
    if(bitmap==NULL) {
        DeleteDC(hdc);
        DeleteDC(hdc2);
        return FALSE;
    }
    gdiobj = SelectObject(hdc2, (HGDIOBJ)bitmap);
    if((gdiobj==NULL) || (gdiobj==(void *)GDI_ERROR)) {
        DeleteDC(hdc);
        DeleteDC(hdc2);
        return FALSE;
    }
    if (!BitBlt(hdc2, 0,0, dwWidth, dwHeight, hdc, 0,0, SRCCOPY)) {
        DeleteDC(hdc);
        DeleteDC(hdc2);
        return FALSE;
    }   
    RGBQUAD colors[256];
    if(dwNumColors!=0) {
        dwNumColors = GetDIBColorTable(hdc2, 0, dwNumColors, colors);
    }   
    BITMAPFILEHEADER bitmapfileheader;
    BITMAPINFOHEADER bitmapinfoheader;
    bitmapfileheader.bfType = 0x4D42;
    bitmapfileheader.bfSize = ((dwWidth * dwHeight * dwBPP)/8) + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (dwNumColors * sizeof(RGBQUAD));
    bitmapfileheader.bfReserved1 = 0;
    bitmapfileheader.bfReserved2 = 0;
    bitmapfileheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (dwNumColors * sizeof(RGBQUAD));  
    bitmapinfoheader.biSize = sizeof(BITMAPINFOHEADER);
    bitmapinfoheader.biWidth = dwWidth;
    bitmapinfoheader.biHeight = dwHeight;
    bitmapinfoheader.biPlanes = 1;
    bitmapinfoheader.biBitCount = (WORD)dwBPP;
    bitmapinfoheader.biCompression = BI_RGB;
    bitmapinfoheader.biSizeImage = 0;
    bitmapinfoheader.biXPelsPerMeter = 0;
    bitmapinfoheader.biYPelsPerMeter = 0;
    bitmapinfoheader.biClrUsed = dwNumColors;
    bitmapinfoheader.biClrImportant = 0;    
    hfile=CreateFile(file,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    if(hfile==INVALID_HANDLE_VALUE) {
        DeleteObject(bitmap);
        DeleteDC(hdc2);
        DeleteDC(hdc);
        return FALSE;
    }
    WriteFile(hfile,&bitmapfileheader,sizeof(BITMAPFILEHEADER), &dwBytes, NULL);
    WriteFile(hfile,&bitmapinfoheader,sizeof(BITMAPINFOHEADER), &dwBytes, NULL);
    if(dwNumColors!=0)
        WriteFile(hfile,colors,sizeof(RGBQUAD)*dwNumColors,&dwBytes,NULL);
    WriteFile(hfile,pBits,(dwWidth*dwHeight*dwBPP)/8,&dwBytes,NULL);
    CloseHandle(hfile);     
    DeleteObject(bitmap);
    DeleteDC(hdc2);
    DeleteDC(hdc);
    return TRUE;
}    

这就是我得到的......我不知道我做错了什么

Error   1   error C1189: #error :  "No Target Architecture" C:\Program Files\Windows Kits\8.0\Include\um\winnt.h    146 1   capture it 2

2 个答案:

答案 0 :(得分:1)

您需要在windef.h之前包含windows.h

答案 1 :(得分:0)

您需要进入项目的配置,并设置目标架构。在你的情况下,我假设是x86。