-
problem displaying image
I just started learning windows programming with forger's tutorial, but I'm stck on the bitmap part. It keeps giving me errors:
In function 'Z7WndProcP6HWND_jjl'
[Linker error] undefined reference to 'DeleteObject@4'
[Linker error] undefined reference to ...
etc.
im using dev c++ and windows xp
main.cpp
Code:
#include <windows.h>
#include "resource.h"
const char g_szClassName[] = "myWindowClass";
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hwnd, IDOK);
break;
case IDCANCEL:
EndDialog(hwnd, IDCANCEL);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HBITMAP g_hbmBall = NULL;
switch(msg)
{
case WM_LBUTTONDOWN:
{
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
DeleteObject(g_hbmBall);
PostQuitMessage(0);
break;
case WM_CREATE:
{
HMENU hMenu, hSubMenu;
HICON hIcon, hIconSm;
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff");
AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "&Go");
AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GSE, "G&o somewhere else");
hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Howard");
AppendMenu(hSubMenu, MF_STRING, ID_DEATH, "&Die");
hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");
AppendMenu(hSubMenu, MF_STRING, ID_HELP_ABOUT, "&About");
SetMenu(hwnd, hMenu);
hIcon = (HICON)LoadImage(NULL, "h.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
if(hIcon)
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
else
MessageBox(hwnd, "Could not load large icon!", "Error", MB_OK | MB_ICONERROR);
hIconSm = (HICON)LoadImage(NULL, "h.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
if(hIconSm)
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
else
MessageBox(hwnd, "Could not load small icon!", "Error", MB_OK | MB_ICONERROR);
g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
if(g_hbmBall == NULL)
MessageBox(hwnd, "Could not load IDB_BALL!", "Error", MB_OK | MB_ICONEXCLAMATION);
}
break;
case WM_PAINT:
{
BITMAP bm;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmBall);
GetObject(g_hbmBall, sizeof(bm), &bm);
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_FILE_EXIT:
{
int ret = MessageBox(hwnd, "Are you sure you want to exit?", "menu", MB_OKCANCEL);
if (ret == IDOK)
DestroyWindow(hwnd);
}
break;
case ID_STUFF_GO:
MessageBox(hwnd, "Stuff -> Go", "menu", MB_OK);
break;
case ID_STUFF_GSE:
MessageBox(hwnd, "Stuff -> Go Somewhere Else", "menu", MB_OK);
break;
case ID_DEATH:
MessageBox(hwnd, "Howard -> Death", "menu", MB_OK);
break;
case ID_HELP_ABOUT:
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
break;
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
wc.hCursor = LoadCursor(NULL, "15.cur");
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
wc.lpszClassName = g_szClassName;
wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
resource.h
Code:
#define IDC_CURSOR 011
#define IDR_MYMENU 101
#define IDI_MYICON 201
#define IDD_ABOUT 301
#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
#define ID_STUFF_GSE 9003
#define ID_DEATH 9004
#define IDC_STATIC 9005
#define ID_HELP_ABOUT 9006
#define IDB_BALL 9007
resource.rc
Code:
#include "resource.h"
#include <windows.h>
IDI_MYICON ICON "h.ico"
IDC_CURSOR CURSOR "15.cur"
IDB_BALL BITMAP "image.bmp"
IDR_MYMENU MENU
{
POPUP "&File"
{
MENUITEM "E&xit", ID_FILE_EXIT
}
POPUP "&Stuff"
{
MENUITEM "&Go", ID_STUFF_GO
MENUITEM "G&o somewhere else", ID_STUFF_GSE
}
POPUP "&Howard"
{
MENUITEM "&Die", 0, GRAYED
}
POPUP "&Help"
{
MENUITEM "&About", ID_HELP_ABOUT
}
}
IDD_ABOUT DIALOG DISCARDABLE 0, 0, 239, 66
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "My About Box"
FONT 8, "MS Sans Serif"
{
DEFPUSHBUTTON "&OK",IDOK,174,18,50,14
PUSHBUTTON "&Cancel",IDCANCEL,174,35,50,14
GROUPBOX "About this program...",IDC_STATIC,7,7,225,52
CTEXT "An example program showing how to use Dialog Boxes\r\n\r\nEdited by Howard",
IDC_STATIC,16,18,144,33
}
-
Those are linker errors which arise because you have not linked the correct libraries. The simplest solution for you would be to start over with a new project and make sure you select 'Windows Application' from the available 'new project' choices; this will ensure that a default subset of windows libraries will be linked when you build your project.
-
thx
It compiles without any errors but the picture doesnt appear on the window
-
Problem solved: HBITMAP g_hbmBall = NULL; had to be a global variable