There's a lot of unnecessary code in your source that hasn't even been used, that's why I asked if you actually made this from scratch.
It's fine if you look at other sources to help you out, but the objective of this task is to learn stuff from it and actually understand what you're doing.
I cleaned it up a bit and have this:
Header:
#pragma once
#include "resource.h"
using namespace std;
//Global variables
Gdiplus::Rect main_rect(775,410,200,65);
vector<Gdiplus::Rect> btn_rects;
enum MyEnum
{
down,
up,
};
struct DrawbleButton
{
Gdiplus::Rect rect;
WCHAR* ImagePath;
};
Main CPP:
// Win32Project1.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "james2.h"
#define MAX_LOADSTRING 100
//Disable unnecessary warnings
#pragma warning(disable: 4018 4985)
// Global Variables:
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
void MyOnPaint(HDC hdc , HWND hWnd)
{
using namespace Gdiplus;
Image background(L"background.jpg");
Graphics graphics(hdc);
Pen pen(Color(0, 255, 255),4.0F);
DrawbleButton buttons[3];
buttons[0].rect = Rect(775,410,200,65);
buttons[0].ImagePath = L"click_me.png";
buttons[1].rect = Rect(20,410,200,65);
buttons[1].ImagePath = L"click_me.gif";
buttons[2].rect = Rect(375,410,200,65);
buttons[2].ImagePath = L"click_me2.png";
//draw background
graphics.DrawImage(&background,0,0);
for(int i = 0; i < 3; i++)
{
btn_rects.push_back(buttons[i].rect);
Image img(buttons[i].ImagePath);
graphics.DrawRectangle(&pen,buttons[i].rect);
graphics.DrawImage(&img,buttons[i].rect);
}
}
void mouse_ev(int ev_type,WPARAM wParam,LPARAM lParam , HWND hWnd)
{
using namespace Gdiplus;
Point CO(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
switch (ev_type)
{
case up:
for(int i = 0 ; i < btn_rects.size();i++)
{
if(btn_rects[i].Contains(CO))
{
int value = MessageBox(NULL,"BTN Clicked !","title",MB_OKCANCEL|MB_ICONEXCLAMATION);
}
}
break;
}
return;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
{
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
MyOnPaint(hdc , hWnd);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
mouse_ev(down,wParam,lParam,hWnd);
break;
case WM_LBUTTONUP:
mouse_ev(up,wParam,lParam,hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32PROJECT1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd = CreateWindow(szWindowClass, "Our App", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
LoadString(hInstance, IDC_WIN32PROJECT1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT1));
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Gdiplus::GdiplusShutdown(m_gdiplusToken);
return (int) msg.wParam;
}