C++将一个图片显示到窗口里面

// Test2.cpp : 定义控制台应用程序的入口点。
//
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")


#include "stdafx.h"


#include
#include
#include
#include


HWND WINAPI GetConsoleWindow();
void HideTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = FALSE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
void ShowTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = TRUE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
int main() {
    HWND  hwnd;
    HDC   hdc;
    HFONT hfont;
    HBITMAP hbm;
    HDC hdcBits;
    BITMAP bm;
CImage Image;


    system("color F0");
    system("cls");
    HideTheCursor();
    hwnd  = GetConsoleWindow();
    hdc   = GetDC(hwnd);


Image.Load(L"1.jpg");
hbm=Image.Detach();


//hbm=LoadImage(0,"C:\\Windows\\1.jpg",IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
    if (hbm) {
        hdcBits = CreateCompatibleDC(hdc);
        GetObject (hbm, sizeof(BITMAP), &bm);
        SelectObject(hdcBits,hbm);
        BitBlt(hdc,20,10,bm.bmWidth, bm.bmHeight,hdcBits,0,0,SRCCOPY);
BitBlt(hdc,20,10,bm.bmWidth, bm.bmHeight,hdcBits,0,0,SRCCOPY);
        DeleteDC(hdcBits);
        DeleteObject(hbm);
    }
    hfont = CreateFont(24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, L"华文楷体");
    SelectObject(hdc,hfont);
    SetTextColor(hdc,RGB(255,255,255));
    SetBkMode(hdc,TRANSPARENT);
    TextOut(hdc,210,30,L"这个背景是泡泡",14);
    DeleteObject(hfont);
    ReleaseDC(hwnd,hdc);
    getch();
    system("color 07");
    system("cls");
    ShowTheCursor();
    return 0;
}

你可能感兴趣的:(C++)