0

I tried every source to replace the background of button by implementation subclassing.

When I change the background, i got a square shape and impossible to click it. somebody can share a good tutorial or sample of code?

anyway, this is my code:

LRESULT CALLBACK DrawPushButton(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC          hdcMem;
HBITMAP      hbmMem;
HANDLE       hnd;
PAINTSTRUCT  ps;
HDC          hdc;

int win_width=100;
int win_height=50;
    switch(uMsg)
    {
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);

        hdcMem = CreateCompatibleDC(hdc);
        hbmMem = CreateCompatibleBitmap(hdc, win_width, win_height);
        hnd   = SelectObject(hdcMem, hbmMem);
        BitBlt(hdc, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
        EndPaint(hwnd, &ps);
        return 0;
        break;
    }
    return CallWindowProc (OldWndProc, hwnd, uMsg, wParam, lParam);
}

edit: i added the BS_OWNERDRAW windows style to the button:

HWND hWndButton=CreateWindowEx(NULL,L"BUTTON",L"OK",WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON|BS_OWNERDRAW,50,220,100,24,hWnd,(HMENU)IDC_MAIN_BUTTON,GetModuleHandle(NULL),NULL);

and i changed the callback,but WM_DRAWITEM didn't call:

LRESULT CALLBACK DrawPushButton(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_DRAWITEM:
            HDC hdc = GetDC(hwnd);
            TextOut(hdc,0,0,L"itay",4);
        break;
    }
    return 0;
}
Itay Avraham
  • 329
  • 1
  • 3
  • 15
  • 1
    To custom draw a button, you need to give it the `BS_OWNERDRAW` window style and then handle the `WM_DRAWITEM` message. – Remy Lebeau Nov 24 '13 at 19:52
  • You don't think you should be deleting the DC and Bitmap that you're creating *every time the button paints* ? – Jonathan Potter Nov 24 '13 at 21:01
  • @RemyLebeau i added the BS_OWNERDRAW but when i debugging the program, WM_DRAWITEM didn't call. see my comment above. – Itay Avraham Nov 25 '13 at 12:45
  • @ItayAvraham: if you [read the documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/bb775923.aspx), `WM_DRAWITEM` will be sent to the button's parent window, not to the button's window. – Remy Lebeau Nov 25 '13 at 16:08
  • @ItayAvraham: Why go through so much pain to change the background of the button? You can achieve this without subclassing. In my answer to [this question](http://stackoverflow.com/questions/18745447/how-can-i-change-the-background-color-of-a-button-winapi-c?rq=1) you can see all the options to change the buttons background, and an implementation of some of them. Hopefully this will help you. Best regards and Marry Christmas. – AlwaysLearningNewStuff Dec 25 '13 at 07:07

1 Answers1

0

It's not a 'good' code, though it is functional - you should probably, as mentioned, be using BS_OWNERDRAW style and handle the drawing in the wndproc of the dialog/window that contains the button. I've chosen to paste an old example I have that approaches it in the same manner you have. You have to do all of the painting yourself. However, you do have the chance to respond to the WM_TIMER message that a button gets that makes it throb or pulsate. (not sure if you get a chance to do so if you only do the drawing from the dialog/window's WindowProc)

I've also chosen to use a superseded method of sub-classing, for the sake of simplicity. Since version 6 of the common controls, you should instead use SetWindowSubclass (http://msdn.microsoft.com/en-us/library/windows/desktop/bb762102(v=vs.85).aspx)

main.cpp

#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"

HINSTANCE hInst;

LRESULT CALLBACK btnProc(HWND btnWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    long oldWndProc;
    int i;
    oldWndProc = GetWindowLong(btnWnd, GWL_USERDATA);
    switch (uMsg)
    {
        case WM_PAINT:
            {
                HDC hdc;
                PAINTSTRUCT ps;
                RECT mRect;
                HBRUSH mBrush;

                hdc = BeginPaint(btnWnd, &ps);
                mBrush = CreateSolidBrush( RGB(100, 147, 111));
                GetClientRect(btnWnd, &mRect);
                FillRect(hdc, &mRect, mBrush);
                DeleteObject(mBrush);

                EndPaint(btnWnd, &ps);
            }
            return 0;
    }
    return CallWindowProc((WNDPROC)oldWndProc, btnWnd, uMsg, wParam, lParam);
}


void onCommand(HWND hwndDlg, WPARAM wParam, LPARAM lParam)
{
    switch (LOWORD(wParam))
    {
    case IDC_BUTTON1:
        MessageBeep(MB_ICONEXCLAMATION);
        break;
    }
}

LRESULT CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
        {
            HWND btnWnd;
            long oldBtnProc;
            btnWnd = GetDlgItem(hwndDlg, IDC_BUTTON1);
            oldBtnProc = GetWindowLong(btnWnd, GWL_WNDPROC);
            SetWindowLong(btnWnd, GWL_USERDATA, oldBtnProc);
            SetWindowLong(btnWnd, GWL_WNDPROC, (long)btnProc);
        }
        return TRUE;

        case WM_CLOSE:
        {
            EndDialog(hwndDlg, 0);
        }
        return TRUE;

        case WM_COMMAND:
            onCommand(hwndDlg, wParam, lParam);
            return TRUE;
    }
    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst=hInstance;
    InitCommonControls();
    return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}

resource.h

#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define DLG_MAIN                                100
#define IDC_BUTTON1                             1000

resource.rc

// Generated by ResEdit 1.5.11
// Copyright (C) 2006-2012
// http://www.resedit.net

#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"

//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
DLG_MAIN DIALOG 0, 0, 186, 95
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "Ms Shell Dlg"
{
    PUSHBUTTON      "Button1", IDC_BUTTON1, 7, 7, 50, 14
}

//
// Manifest resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
1                  RT_MANIFEST    ".\\manifest.xml"

manifest.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
    </dependentAssembly>
  </dependency>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="asInvoker"
          uiAccess="false"/>
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>
enhzflep
  • 12,927
  • 2
  • 32
  • 51