简体   繁体   中英

how to make a picture fit in a static control vc++ win32

can you tell me how to make a picture fit in a static control, i mean like if you create a static control for viewing pictures and if the picture quality or size of picture is bigger than control then it re size the static control with the size of picture. i could create the control and set the picture to it alright. but i don't know how to make it fit on control. this is how i create control and set picture to it.

Code:

HWND static_con(HWND hWnd, HINSTANCE hInst){
    HWND Static_Pic;

    Profile_Pic = CreateWindow("STATIC", NULL, SS_BITMAP|WS_CHILD|WS_VISIBLE|WS_TABSTOP, 5,5,33,33, hWnd, NULL, hInst, NULL);
    HBITMAP hBmp = (HBITMAP)LoadImage(NULL, "camera1.jpg", IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    if(hBmp == NULL){
        MessageBox(NULL, "Error while loading image", "Error", MB_OK|MB_ICONERROR);
    }
    SendMessage(Static_Pic, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);
    return 0;
}

and then i call the function in WM_CREATE handler which creates it successfully and now i don't know how to make it fit on control, i really appreciate if you could tell me how to make the picture fit on control.

You can use SS_REALSIZECONTROL From Microsoft's documentation.

SS_REALSIZECONTROL - Adjusts the bitmap to fit the size of the static control.

You can also manually scale the image. Get the size of the control where image is to go by using GetWindowRect(), then by using StretchBlt() scale the image so that its dimensions match that of the source, then do STM_SETIMAGE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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