2013-11-02 121 views
0

我有這個代碼,屏幕捕捉,它應該工作,但我不知道問題在哪裏 它說「沒有目標架構」,我認爲它的位事情..即時運行Windows 7 32位,和IM使用Visual Studio的2012年提前謝謝你C++控制檯「屏幕截圖」問題

#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 
+1

您可能需要移動的#include''以上的#include''。看到[這個前面的問題在SO](http://stackoverflow.com/questions/4845198/fatal-error-no-target-architecture-in-visual-studio-2010) – Dan

回答

1

您需要包括WINDOWS.H WINDEF.H

+0

謝謝先生,它的工作很好 – ExtraMt

0

你需要進入爲您的項目配置,並設置目標架構。在你的情況下,我認爲這是x86。