在按钮上打开图像文件单击矩形
Open image file on button click to rectangle
如何在点击按钮时打开图片文件并将其放入矩形中?我使用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)[=14=]*.JPG[=14=]File BMP (*.BMP)[=14=]*.BMP[=14=]File PNG(*.PNG)[=14=]*.PNG[=14=]All Files (*.*)[=14=]*.*[=14=]";
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);
}
你这里有四个问题
1st) LoadImage(..,IMAGE_BITMAP,...)
仅支持 BMP 文件。
2nd) 你删除了错误的 Handle (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
}
3rd) 你上面的图不是持久的,它会被任何重叠擦除window。将其移动到 WM_PAINT
.
4) 不要在WM_PAINT中调用InvalidateRect
或InvalidateRgn
。
这是完整的工作代码
#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)[=10=][=10=]";
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;
}
如何在点击按钮时打开图片文件并将其放入矩形中?我使用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)[=14=]*.JPG[=14=]File BMP (*.BMP)[=14=]*.BMP[=14=]File PNG(*.PNG)[=14=]*.PNG[=14=]All Files (*.*)[=14=]*.*[=14=]";
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);
}
你这里有四个问题
1st) LoadImage(..,IMAGE_BITMAP,...)
仅支持 BMP 文件。
2nd) 你删除了错误的 Handle (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
}
3rd) 你上面的图不是持久的,它会被任何重叠擦除window。将其移动到 WM_PAINT
.
4) 不要在WM_PAINT中调用InvalidateRect
或InvalidateRgn
。
这是完整的工作代码
#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)[=10=][=10=]";
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;
}