2009年10月26日 星期一

Win32秀圖


(1) Win32 SDK 寫法:

用 Dev C++ 的話要連結 -lGDI32,並加上:
extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow();

#undef UNICODE
#include <windows.h>

int main()
{
    BITMAP bmp;
    HANDLE hOut   = GetStdHandle (STD_OUTPUT_HANDLE);
    HANDLE hBmp   = LoadImage (0, "b.bmp", 0,0,0, LR_LOADFROMFILE);
    HDC    hdc    = GetDC (GetConsoleWindow());
    HDC    hdcMem = CreateCompatibleDC (hdc);

    GetObject (hBmp, sizeof bmp, &bmp);

    int h = bmp.bmHeight;
    int w = bmp.bmWidth;

    SMALL_RECT rc   = {0, 0, w/8 ,h/16};
    COORD      size = {w/8+1 ,h/16+1};

    SelectObject (hdcMem, hBmp);
    DeleteObject (hBmp);
    SetConsoleScreenBufferSize (hOut, size);
    SetConsoleWindowInfo (hOut,1,&rc);
    while (BitBlt (hdc, 0,0, w,h, hdcMem, 0,0, SRCCOPY));
}

(2) GDI+ 寫法:

#pragma comment (lib, "gdiplus.lib")
#include <stdio.h>
#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow();

struct GDIp 
{
    GdiplusStartupInput in;
    ULONG_PTR tok;
    GDIp() {GdiplusStartup (&tok, &in, 0);}
   ~GDIp() {GdiplusShutdown (tok);}
} 
GDIp;

int main()
{
    Graphics gp (GetDC (GetConsoleWindow()));
    Bitmap in (L"d:/img.jpg");
    gp.DrawImage (&in, 0,0); 
    return getchar();     
}

沒有留言:

張貼留言