如何在win32中使用visual studio c拉伸背景图像

how to stretch a background image in win32 using visual studio c

本文关键字:studio 图像 背景 visual win32      更新时间:2023-10-16

我试图用c++在Win32 api中创建一个应用程序,我想让它在没有任何栏的情况下全屏,我成功了,但我在背景图像中仍然有问题。图像是重复的,但我希望它被拉伸。你知道吗?代码的下面部分:

int WINAPI WinMain (HINSTANCE cetteInstance, HINSTANCE precedenteInstance,
LPSTR lignesDeCommande, int modeDAffichage)
{
HWND fenetrePrincipale;
MSG message;
WNDCLASS classeFenetre;
instance = cetteInstance;

classeFenetre.style = 0;
classeFenetre.lpfnWndProc = procedureFenetrePrincipale;
classeFenetre.cbClsExtra = 0;
classeFenetre.cbWndExtra = 0;
classeFenetre.hInstance = NULL;
classeFenetre.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
classeFenetre.hCursor = LoadCursor(NULL, IDC_ARROW);
   // classeFenetre.hbrBackground = (HBRUSH)(1 + COLOR_BTNFACE);
//classeFenetre.hbrBackground = CreatePatternBrush(LoadBitmap( instance, MAKEINTRESOURCE("imagesImage1.bmp" ) ) );
HBITMAP hbmp = LoadBitmap(instance,MAKEINTRESOURCE(IDB_BITMAP1));
    if(NULL == hbmp)
    {
        MessageBox(NULL,L"BitMap Loading Failed.",L"Error",MB_ICONEXCLAMATION | MB_OK);
    }
    else
    {
        HBRUSH hbr = CreatePatternBrush(hbmp);
        if(NULL == hbr)
        {
            MessageBox(NULL,L"Brush Creation Failed.",L"Error",MB_ICONEXCLAMATION | MB_OK);
        }
        else
        {
            //StretchBlt();
            HDC hdcMem = GetDC (NULL) ;
            HDC wndHDC = GetDC (fenetrePrincipale) ;
            StretchBlt(hdcMem, 0, 0, 800, 600, wndHDC, 0, 0, 1280, 1024, SRCCOPY);
            classeFenetre.hbrBackground = hbr ;

        }
    }
classeFenetre.lpszMenuName = NULL;
classeFenetre.lpszClassName = L"classeF";
//fullscreen mode and delete minimize and max buttons

// On prévoit quand même le cas où ça échoue
if(!RegisterClass(&classeFenetre)) return FALSE;
//WS_OVERLAPPEDWINDOW
    fenetrePrincipale = CreateWindow(L"classeF", L"Ma premiere fenetre winAPI !",WS_MAXIMIZE|WS_POPUP ,
                               CW_USEDEFAULT, CW_USEDEFAULT, 800, 630,
                                               NULL,
                                               NULL,//LoadMenu(instance, L"ID_MENU"),
                                               cetteInstance,
                                               NULL);
if (!fenetrePrincipale) return FALSE;
//ShowWindow(fenetrePrincipale, modeDAffichage);
ShowWindow(fenetrePrincipale,SW_MAXIMIZE);
UpdateWindow(fenetrePrincipale);

while (GetMessage(&message, NULL, 0, 0))
{
    TranslateMessage(&message);
    DispatchMessage(&message);
}
return message.wParam;

}

谢谢

您没有显示确切的代码,但看起来您加载了一个位图,从中创建了一个画笔,然后将该画笔设置为窗口的画笔。笔刷确实会导致你报告的重复图像行为。要获得拉伸位图,您可以跳过任何与笔刷相关的代码。相反,处理发送到窗口的WM_ERASEBKGND消息。在其中,调用StretchBlt将您的位图绘制到窗口的客户端区域。要绘制的HDC在消息的wParam参数中给出。

步骤1、创建窗口

2,设置twindowpos将你的窗口放在所有窗口和全屏的顶部

在你的windows的windowprocess句柄WM_PAINT消息

4、加载位图 5、使用CreateCompatibleDC创建内存dc 通过调用SelectObject 将位图选择到内存dc中7、对实际dc执行StretchBlt,使用准备好的内存dc作为源,您应该知道位图的实际宽度和高度,以便进行适当的拉伸