Chinaunix首页 | 论坛 | 博客
  • 博客访问: 212972
  • 博文数量: 70
  • 博客积分: 2050
  • 博客等级: 大尉
  • 技术积分: 700
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-15 21:42
文章分类

全部博文(70)

文章存档

2013年(1)

2011年(5)

2010年(3)

2009年(9)

2008年(17)

2007年(6)

2006年(29)

我的朋友

分类: C/C++

2010-04-27 12:05:07

#include
#include
#include

HINSTANCE hinst;
HWND hWnd;

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
int InitApplication(HINSTANCE);
int InitInstance(HINSTANCE, int);
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;
    BOOL fGotMessage;

    OleInitialize(NULL);
    if (!InitApplication(hinstance))
        return FALSE;

    if (!InitInstance(hinstance, nCmdShow))
        return FALSE;

    while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    OleUninitialize();
    return (int)msg.wParam;
    UNREFERENCED_PARAMETER(lpCmdLine);
}

BOOL InitApplication(HINSTANCE hinstance)
{
    WNDCLASSEX wcx;

    wcx.cbSize = sizeof(wcx);          // size of structure
    wcx.style = CS_HREDRAW | CS_VREDRAW;                    // redraw if size changes
    wcx.lpfnWndProc = MainWndProc;     // points to window procedure
    wcx.cbClsExtra = 0;                // no extra class memory
    wcx.cbWndExtra = 0;                // no extra window memory
    wcx.hInstance = hinstance;         // handle to instance
    wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);              // predefined app. icon
    wcx.hCursor = LoadCursor(NULL, IDC_ARROW);                    // predefined arrow
    wcx.hbrBackground = (HBRUSH)COLOR_WINDOW;                  // white background brush
    wcx.lpszMenuName =  NULL;    // name of menu resource
    wcx.lpszClassName = TEXT("19920709");  // name of window class
    wcx.hIconSm = LoadImage(hinstance, MAKEINTRESOURCE(5), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR); // small class icon

    return RegisterClassEx(&wcx);
}

BOOL InitInstance(HINSTANCE hinstance, int nCmdShow)
{
    hinst = hinstance;
    hWnd = CreateWindow(
        TEXT("19920709"),        // name of window class
        TEXT("show example"),            // title-bar string
        WS_OVERLAPPEDWINDOW, // top-level window
        CW_USEDEFAULT,       // default horizontal position
        CW_USEDEFAULT,       // default vertical position
        CW_USEDEFAULT,       // default width
        CW_USEDEFAULT,       // default height
        (HWND) NULL,         // no owner window
        (HMENU) NULL,        // use class menu
        hinstance,           // handle to application instance
        NULL);      // no window-creation data

    if (!hWnd)
        return FALSE;

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
    return TRUE;

}

LPPICTURE LoadPictureFromFile(LPCTSTR lpszFile)
{
    HANDLE hFile;
    HGLOBAL hGlobal;
    DWORD dwFileSize, dwBytesRead;
    LPVOID pvData;
    LPSTREAM pstm;
    LPPICTURE ppic;
    BOOL bOK;
    HRESULT hr;

    hFile = CreateFile(lpszFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
        return NULL;

    dwFileSize = GetFileSize(hFile, NULL);
    if (dwFileSize == (DWORD)-1)
    {
        CloseHandle(hFile);
        return NULL;
    }

    hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
    if (hGlobal == NULL)
    {
        CloseHandle(hFile);
        return NULL;
    }

    pvData = GlobalLock(hGlobal);
    if (pvData == NULL)
    {
        CloseHandle(hFile);
        GlobalUnlock(hGlobal);
        GlobalFree(hGlobal);
        return NULL;
    }
    dwBytesRead = 0;
    bOK = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
    CloseHandle(hFile);
    GlobalUnlock(hGlobal);
    if (!bOK)
    {
        return NULL;
    }

    pstm = NULL;
    hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
    if (!(SUCCEEDED(hr)))
    {
        if (pstm != NULL)
            pstm->lpVtbl->Release(pstm);
        return NULL;
    }

    hr = OleLoadPicture(pstm, dwFileSize, FALSE, &IID_IPicture, (LPVOID *)&ppic);
    pstm->lpVtbl->Release(pstm);
    GlobalFree(hGlobal);
    if (!(SUCCEEDED(hr)))
        return NULL;

    return ppic;
}

VOID DrawPicture(HWND hWnd, LPPICTURE ppic)
{
    HDC hDC;
    long x, y;
    RECT client;

    hDC = GetDC(hWnd);
    GetClientRect(hWnd, &client);
    ppic->lpVtbl->get_Width(ppic, &x);
    ppic->lpVtbl->get_Height(ppic, &y);
    ppic->lpVtbl->Render(ppic, hDC, 0, client.bottom, client.right, -client.bottom, 0, 0, x, y, &client);
    ReleaseDC(hWnd, hDC);
}

LPPICTURE picture;

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

    LRESULT rc = 0;

    switch (uMsg)
    {
        case WM_CREATE:
            picture = LoadPictureFromFile(TEXT("c:\\example.jpg"));
            break;

        case WM_SIZE:
            DrawPicture(hWnd, picture);
            break;
        case WM_DESTROY:
            picture->lpVtbl->Release(picture);
            PostQuitMessage(0);
            break;
        default:
            rc = DefWindowProc(hWnd, uMsg, wParam, lParam);
            break;
    }

    return rc;
}
阅读(577) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~