按钮上的打开图像文件单击以矩形

时间:2015-11-12 05:03:49

标签: c image winapi jpeg rectangles

如何打开图像文件并在单击按钮时将其放在矩形中?我使用winapi和c编程语言,请提前帮助和感谢

全局变量:

HDC                     hdc, hdcMem;
HBITMAP                 hLogo, hGambar;
OPENFILENAME            ofn = {0};
char szNamaFile[MAX_PATH]   = {0}

WM_CREATE:

HWND   btnLoad;

btnLoad     = CreateWindow("Button", "&Load", WS_CHILD | WS_TABSTOP | WS_VISIBLE | BS_FLAT, 149, 421, 70, 25, hWnd, (HMENU)IDB_LOAD, GetModuleHandle(NULL), NULL);
if(btnLoad == NULL)
{
   MessageBox(NULL, "Gagal Membuat Button!", "Error Uy!", MB_ICONEXCLAMATION);
   exit(EXIT_FAILURE);
}
hGambar = (HBITMAP)LoadImage(hInst, szNamaFile, IMAGE_BITMAP, 5, 5, LR_LOADFROMFILE);
SendMessage(btnLoad, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hGambar);

WM_COMMAND:

if(HIWORD(wParam) != BN_CLICKED)
   break;
switch(LOWORD(wParam))
{
   case IDB_LOAD:
      BukaFormDialog(hWnd);
   break;
}
break;

WM_PAINT:

PAINTSTRUCT     ps ;

InvalidateRect (hWnd, NULL, TRUE);

RECT rctWindow;
GetClientRect(hWnd, &rctWindow);

RECT  rctPct;

rctPct.left         = 3;
rctPct.top          = rctKiri.top + 3;
rctPct.right        = rctKiri.right - 3;
rctPct.bottom       = rctKiri.bottom- 75;

hdc = BeginPaint(hWnd, &ps);

/*------------Picture Box--------------*/
Rectangle(hdc, rctPct.left, rctPct.top, rctPct.right, rctPct.bottom);
/*-------------------------------------*/

EndPaint(hWnd, &ps);
break;

程序:

char BukaFormDialog(HWND hWnd)
{  
    ofn.lStructSize     = sizeof(ofn);
    ofn.hwndOwner       = hWnd;
    ofn.lpstrFilter     = "File JPG (*.JPG)\0*.JPG\0File BMP (*.BMP)\0*.BMP\0File PNG(*.PNG)\0*.PNG\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile       = szNamaFile;
    ofn.lpstrTitle      = "Pilih Gambar ...";
    ofn.nMaxFile        = MAX_PATH;
    ofn.Flags           = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;

    if(GetOpenFileName(&ofn))
    {
        hMem        = CreateCompatibleDC(hdc);
        SelectObject(hMem, hGambar);
        BitBlt(hdc, 4, 4, 50,50, hMem, 0, 0, SRCCOPY);
        DeleteDC(hMem);
        DeleteObject(hGambar);
    }
    else
        MessageBox(hWnd, "Gagal Membuka File", "Error", MB_ICONERROR | MB_OK);
    return (0);
}

2 个答案:

答案 0 :(得分:1)

这里有四个问题

1st)LoadImage(..,IMAGE_BITMAP,...)仅支持BMP文件。

2)你正在删除错误的句柄(hGambar

if(GetOpenFileName(&ofn))
    {
        hMem        = CreateCompatibleDC(hdc);
        hGambar=SelectObject(hMem, hGambar); //save the original hBmp
        BitBlt(hdc, 4, 4, 50,50, hMem, 0, 0, SRCCOPY);
        hGambar=SelectObject(hMem, hGambar); // Select Back the original hBmp
        DeleteDC(hMem);
        DeleteObject(hGambar); // now hGambar is back delete it
    }

第3条)您的上图不是持久性的,它将被任何重叠的窗口擦除。将其移至WM_PAINT

4)不要在WM_PAINT中呼叫InvalidateRectInvalidateRgn

答案 1 :(得分:0)

这是一个完整的工作代码

#include <windows.h>

HDC                     hdc, hdcMem;
HBITMAP                 hLogo, hGambar=NULL;

#ifndef IDB_LOAD
#define IDB_LOAD    1000
#endif

#define APP_CLASS   TEXT("APP_CLASS")


char szNamaFile[MAX_PATH];

/*____________________________________________________________________________________________________________
*/
int BukaFormDialog(HWND hWnd)
{  

    OPENFILENAME  ofn={sizeof(ofn)};

    ofn.hwndOwner       = hWnd;
    ofn.lpstrFilter     = "File BMP (*.BMP)\0\0";
    ofn.lpstrFile       = szNamaFile;
    ofn.lpstrTitle      = "Pilih Gambar ...";
    ofn.nMaxFile        = MAX_PATH;
    ofn.Flags           = OFN_FILEMUSTEXIST;

    if(GetOpenFileName(&ofn))
    {
        HBITMAP hTmp;
        DeleteObject(hGambar);
        hGambar=LoadImage(NULL,szNamaFile,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
        if(hGambar){
            InvalidateRect(hWnd,NULL,1);
            return 1;
        }
        MessageBox(hWnd, "Bad Image", "Error", MB_ICONERROR | MB_OK);

    }
    else{
        //User pressed Cancel*/;
    }
    return (0);
}



/*____________________________________________________________________________________________________________
*/

int WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam){
    switch(uMsg){
        case WM_CREATE:
            CreateWindow(
                TEXT("Button"), 
                TEXT("&Load"),
                WS_CHILD | WS_TABSTOP | WS_VISIBLE | BS_FLAT,
                149, 421, 70, 25, 
                hWnd, (HMENU)IDB_LOAD, NULL, NULL);

            break;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
               case IDB_LOAD:
                  BukaFormDialog(hWnd);
               break;
            }
            break;
        case WM_PAINT:{
            PAINTSTRUCT ps;
            HDC hMem;
            BeginPaint(hWnd,&ps);

            if(hGambar){
                if(hMem=CreateCompatibleDC(ps.hdc)){
                    hGambar=SelectObject(hMem,hGambar); /*select & swap*/
                    BitBlt(ps.hdc, 4,  4, 50,50, hMem, 0, 0, SRCCOPY);
                    hGambar=SelectObject(hMem,hGambar);/*select & swap back*/
                    DeleteDC(hMem);
                }
            }
            EndPaint(hWnd,&ps);
        }
            break;
        case WM_DESTROY:
            DeleteObject(hGambar);
            break;
        default:
            return DefWindowProc(hWnd,uMsg,wParam,lParam);
        }
    return 0;
}

/*____________________________________________________________________________________________________________
*/
int WinMain(HINSTANCE hInstance,HINSTANCE hPrev,LPSTR lpCmd,int nShow){

    WNDCLASS wc={
                0,
                WinProc,
                0,
                0,
                hInstance,
                NULL,
                LoadCursor(NULL,IDC_ARROW),
                (HBRUSH) (COLOR_WINDOW+1),
                NULL,
                APP_CLASS};

    if(RegisterClass(&wc)){
        if(CreateWindow(APP_CLASS,TEXT("Title"),
                WS_OVERLAPPEDWINDOW|WS_VISIBLE,
                CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
                NULL,NULL,NULL,NULL)){
            MSG msg;
            while(GetMessage(&msg,NULL,0,0)){
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    }
    return 0;
}