Amine
Amine

Reputation: 367

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

I'm trying to create an application in Win32 api with c++ and I want to make it FullScreen without any bar , i succeeded but i still have a problem in the background image. The image is repeated but i want it to be stretched. Have you any idea? below part from the code :

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("images\Image1.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;

}

thanks

Upvotes: 0

Views: 2587

Answers (2)

Tony Thomas
Tony Thomas

Reputation: 1005

Steps 1, CreateWindowEx to create the window

2, SetWindowPos to place your window on top of all windows and Fullscreen

3, On your windows's WindowProce handle WM_PAINT message

4, Load your bitmap

5, Create a memory dc using CreateCompatibleDC

6, Selet your bitmap into memory dc by calling SelectObject

7, Do the StretchBlt to your actual dc, using the prepared memory dc as the source, you should know the actual width and height of the bitmap for proper stretching

Upvotes: 0

Rob Kennedy
Rob Kennedy

Reputation: 163287

You haven't shown the exact code, but it appears that you load a bitmap, create a brush from it, and then set that brush as the brush for your window. Brushes would indeed lead to the repeating-image behavior you report. To get a stretched bitmap, you may skip any brush-related code. Instead, handle the WM_ERASEBKGND message sent to your window. In it, call StretchBlt to paint your bitmap onto the client area of your window. The HDC to paint to is given in the message's wParam argument.

Upvotes: 2

Related Questions