一个简单的win32截图例子

  该代码为把当前屏幕截图并保存为文件的代码。

SaveBitmapToFile.cpp文件在我前面一章文章中:点此进入

#include "SaveBitmapToFile.cpp"

#include <windows.h>

#define  IDC_SAVE 1

#define  IDC_FILEPATH 2





LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) ;



void InitWindow(HWND hWnd, LPARAM lParam);



int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,

    PSTR szCmdLine, int iCmdShow)

{

    static TCHAR szAppName[] = TEXT ("test") ;



    HWND         hWnd ;

    MSG          msg ;

    WNDCLASS     wndclass ;



    wndclass.style         = CS_HREDRAW | CS_VREDRAW ;

    wndclass.lpfnWndProc   = WndProc ;

    wndclass.cbClsExtra    = 0 ;

    wndclass.cbWndExtra    = 0 ;

    wndclass.hInstance     = hInstance ;

    wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;

    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;

    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;

    wndclass.lpszMenuName  = NULL ;

    wndclass.lpszClassName = szAppName ;



    if (!RegisterClass (&wndclass))

    {

        MessageBox (NULL, TEXT ("注册窗口类失败!"), szAppName, MB_ICONERROR) ;

        return 0 ;

    }



    hWnd = CreateWindow (szAppName,

                        TEXT ("test"),

                        WS_OVERLAPPEDWINDOW,

                        100,

                        100,

                        350,

                        100,

                        NULL,

                        NULL,

                        hInstance,

                        NULL) ;



    ShowWindow (hWnd, iCmdShow) ;

    UpdateWindow (hWnd) ;



    while (GetMessage(&msg, NULL, 0, 0))

    {

        TranslateMessage (&msg) ;

        DispatchMessage (&msg) ;

    }

    return msg.wParam ;

}





LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

    HDC         hdc ;

    PAINTSTRUCT ps;

    HBITMAP        hBitmap;

    HDC            hdcMem;

    static int screenx, screeny;

    static char filePath[MAXPNAMELEN];



    switch (message)

    {

    case WM_PAINT:

        hdc = BeginPaint(hWnd, &ps);

        EndPaint(hWnd,&ps);

        return 0;

            

    case WM_CREATE:

        InitWindow(hWnd, lParam);

        screenx = GetSystemMetrics(SM_CXSCREEN);

        screeny = GetSystemMetrics(SM_CYSCREEN);

        return 0;



    case WM_COMMAND:



        switch (LOWORD(wParam))

        {

        case IDC_SAVE:

            GetDlgItemText(hWnd, IDC_FILEPATH, (LPWSTR)filePath, sizeof(filePath));



            ShowWindow(hWnd, SW_HIDE);

            Sleep(500);



            hdc = GetDC(NULL);



            hBitmap = CreateCompatibleBitmap(hdc, screenx, screeny);

            hdcMem = CreateCompatibleDC(hdc);

            

            SelectObject(hdcMem, hBitmap);

            BitBlt(hdcMem, 0, 0, screenx, screeny, hdc, 0, 0, SRCCOPY);

            SaveBitmapToFile((LPCTSTR)filePath, hdc, hBitmap);

            

            ReleaseDC(hWnd, hdcMem);

            ReleaseDC(hWnd, hdc);



            ShowWindow(hWnd, SW_SHOW);



            MessageBox(hWnd, TEXT("截图完成"), TEXT("提示"), MB_OK);

            return 0;

        }



        return 0;



    case WM_DESTROY:

        PostQuitMessage (0) ;

        return 0 ;

    }



    return DefWindowProc (hWnd, message, wParam, lParam) ;

}



void InitWindow(HWND hWnd, LPARAM lParam)

{

    HWND btnSave, txtFilePath;



    btnSave = CreateWindow(TEXT("BUTTON"),

                        TEXT("Save"),

                        WS_CHILD | BS_PUSHBUTTON,

                        20,

                        30,

                        100,

                        25,

                        hWnd,

                        (HMENU)IDC_SAVE,

                        ((LPCREATESTRUCT) lParam)->hInstance,

                        NULL);



    txtFilePath = CreateWindow(TEXT("EDIT"),

                                TEXT("d:\\test.bmp"),

                                WS_CHILD | WS_BORDER,

                                20,

                                1,

                                300,

                                25,

                                hWnd,

                                (HMENU)IDC_FILEPATH,

                                ((LPCREATESTRUCT) lParam)->hInstance,

                                NULL);



    ShowWindow(btnSave, SW_SHOW);

    ShowWindow(txtFilePath, SW_SHOW);

}

 

你可能感兴趣的:(Win32)