控制檯顯示圖片
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow();
void GotoXY(int x,int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
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);
}
}
SMALL_RECT SizeOfWindow(HANDLE hConsoleOutput)
{
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(hConsoleOutput, &info);
return info.srWindow;
}
void AutoSizeConsole()
{
HANDLE handle_out;
CONSOLE_SCREEN_BUFFER_INFO info;
handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(handle_out, &info);
SMALL_RECT rect =SizeOfWindow(handle_out);
COORD size={rect.Right+1,rect.Bottom+1};//2
//SetConsoleWindowInfo(handle_out, true, &rect);
SetConsoleScreenBufferSize(handle_out, size);//3
CloseHandle(handle_out);
}
int main() {
HWND hwnd;
HDC hdc;
HFONT hfont;
HBITMAP hbm;
HDC hdcBits;
BITMAP bm;
system("color F0");
system("cls");
HideTheCursor();
AutoSizeConsole();
hwnd = GetConsoleWindow();
hdc = GetDC(hwnd);
hbm=(HBITMAP)LoadImage(0,(LPTSTR)"image1.bmp",IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
if (hbm) {
hdcBits = CreateCompatibleDC(hdc);
GetObject (hbm, sizeof(BITMAP), &bm);
SelectObject(hdcBits,hbm);
BitBlt(hdc,0,0,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, "宋體");
SelectObject(hdc,hfont);
SetTextColor(hdc,RGB(255,255,255));
SetBkMode(hdc,TRANSPARENT);
TextOut(hdc,210,30,"這個是透明效果",14);
DeleteObject(hfont);
ReleaseDC(hwnd,hdc);
getch();
system("color 07");
system("cls");
ShowTheCursor();
return 0;
}