简体   繁体   English

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

[英]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. 我正在尝试使用c ++在Win32 api中创建一个应用程序,我想在没有任何条形码的情况下使其成为FullScreen,我成功但我仍然在背景图像中有问题。 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 谢谢

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. 而是处理发送到窗口的WM_ERASEBKGND消息。 In it, call StretchBlt to paint your bitmap onto the client area of your window. 在其中,调用StretchBlt将您的位图绘制到窗口的客户区域。 The HDC to paint to is given in the message's wParam argument. 要绘制的HDC在消息的wParam参数中给出。

Steps 1, CreateWindowEx to create the window 步骤1,CreateWindowEx创建窗口

2, SetWindowPos to place your window on top of all windows and Fullscreen 2,SetWindowPos将窗口放在所有窗口和全屏幕的顶部

3, On your windows's WindowProce handle WM_PAINT message 3,在windows的WindowProce上处理WM_PAINT消息

4, Load your bitmap 4,加载你的位图

5, Create a memory dc using CreateCompatibleDC 5,使用CreateCompatibleDC创建内存dc

6, Selet your bitmap into memory dc by calling SelectObject 6,通过调用SelectObject将位图切换到内存dc

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 7,将StretchBlt做到你的实际直流,使用准备好的内存直流作为源,你应该知道位图的实际宽度和高度,以便进行适当的拉伸

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM