簡體   English   中英

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

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

我正在嘗試使用c ++在Win32 api中創建一個應用程序,我想在沒有任何條形碼的情況下使其成為FullScreen,我成功但我仍然在背景圖像中有問題。 圖像重復但我希望它被拉伸。 你知道嗎? 以下部分來自代碼:

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;

}

謝謝

您沒有顯示確切的代碼,但似乎您加載位圖,從中創建一個畫筆,然后將該畫筆設置為窗口的畫筆。 畫筆確實會導致您報告的重復圖像行為。 要獲得拉伸位圖,您可以跳過任何與畫筆相關的代碼。 而是處理發送到窗口的WM_ERASEBKGND消息。 在其中,調用StretchBlt將您的位圖繪制到窗口的客戶區域。 要繪制的HDC在消息的wParam參數中給出。

步驟1,CreateWindowEx創建窗口

2,SetWindowPos將窗口放在所有窗口和全屏幕的頂部

3,在windows的WindowProce上處理WM_PAINT消息

4,加載你的位圖

5,使用CreateCompatibleDC創建內存dc

6,通過調用SelectObject將位圖切換到內存dc

7,將StretchBlt做到你的實際直流,使用准備好的內存直流作為源,你應該知道位圖的實際寬度和高度,以便進行適當的拉伸

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM