Results 1 to 5 of 5

Thread: James : Challenge Accepted !

  1. #1
    Developer RyBack's Avatar
    Join Date
    Apr 2014
    Location
    In Front of the screen
    Posts
    1,603

    Cool James : Challenge Accepted !

    Hi,

    Just sharing a For-Fun application

    Concept : Create a drawable Button.

    Idea : James.

    Coding : RyBack.

    Status : Alpha.

    Comment : i know it looks like shit , but it's purpose is learning c++ and GDI+ (gaining some experience and also it's a challenge).

    File : James.zip

  2. #2
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    nice job, but how much of this was your code?
    Your method works fine. Actually, I had to rewrite your code because I couldn't even open it up on my version of VS, also for some reason your debugged exe was throwing several errors so I had to recompile it.

    Basically after I made some adjustments I got it to compile and run without a problem, but I'm surprised you went about it the way you did.
    I can't say it's a bad jopb. You did everything I asked, but damn, it just looks way more convoluted than it needed to be in my opinion.

    You can easily create new buttons using CreateWindow(), and then just render the image on the button.
    The nice thing about it also is that instead of checking so many variables to see if you click on a button, you just check against the HMENU variable you defined.
    For example
    case myButton1:

    You can stick with the way you did it, but I'd like to see if you can optimize your code a bit.

    Also your method right now the way you have it rendering the image, the images need to be in the same folder as your binary.

    Here's the wrapper I created with your code:
    www.x-null.net/James/james2.zip

  3. #3
    Developer RyBack's Avatar
    Join Date
    Apr 2014
    Location
    In Front of the screen
    Posts
    1,603

    Default

    Thanks ^_^ , I actually had no time to port it , so i removed some heavy useless files and zipped it up xD


    Quote Originally Posted by James View Post
    nice job, but how much of this was your code?
    Most of it , Which part do you think isn't mine?

    Quote Originally Posted by James View Post
    You did everything I asked, but damn, it just looks way more convoluted than it needed to be in my opinion.
    Sounds like I surprised someone , I knew it wasn't the best way to do it , but for somehow I liked the idea .
    Quote Originally Posted by James View Post
    You can easily create new buttons using CreateWindow(), and then just render the image on the button.
    The nice thing about it also is that instead of checking so many variables to see if you click on a button, you just check against the HMENU variable you defined.
    For example
    case myButton1:
    I thought of that , but I wanted to make something that differs from ur method .

    Quote Originally Posted by James View Post
    You can stick with the way you did it, but I'd like to see if you can optimize your code a bit.
    How ?

    Quote Originally Posted by James View Post
    Also your method right now the way you have it rendering the image, the images need to be in the same folder as your binary.
    Read the first line .
    Last edited by RyBack; May 7th, 2015 at 01:00 PM.

  4. #4
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    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;
    }

  5. #5
    Developer RyBack's Avatar
    Join Date
    Apr 2014
    Location
    In Front of the screen
    Posts
    1,603

    Default

    Well i didn't make this code only for this purpose , I kept trying lots of functions of GDI+ , even though the ones that has nothing to do with buttons xD.

    BTW , the reason I use current origins and old origins is to make sure the mouse has clicked the button in a good way.

    Eg.

    In ur edit :


    if(btn_rects[i].Contains(CO))
    {
    int value = MessageBox(NULL,"BTN Clicked !","title",MB_OKCANCEL|MB_ICONEXCLAMATION);
    }


    and



    case WM_LBUTTONDOWN:
    mouse_ev(down,wParam,lParam,hWnd);
    break;


    case WM_LBUTTONUP:
    mouse_ev(up,wParam,lParam,hWnd);
    break;


    with this code the Button down event is useless , also if i click down anywhere in the client area and release on any button it will be treated as a button click , which isn't.

    but every thing else is good

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •