By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,248 Members | 1,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,248 IT Pros & Developers. It's quick & easy.

How can i change the color of the button when the mouse hovers over it. I am using wi

P: 1
So far this is the code

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include<Windows.h>
  3. #include<commctrl.h>
  4. #include<Winuser.h>
  5. //#include <atlstr.h>
  6. #define IDC_BUTTON 101
  7.  
  8.  
  9. TCHAR szClassName[ ] = _T("WindowsApp");
  10. LRESULT CALLBACK WindowProcedure (HWND , UINT , WPARAM , LPARAM);
  11.  
  12. HWND hEdit;
  13. HWND hWnd;
  14.  
  15. void AddControls(HWND);
  16.  
  17.  
  18.  
  19. int _tmain(int argc, _TCHAR* argv[])
  20. {
  21.  
  22.     HWND hWnd=0;
  23.     MSG msg;
  24.     WNDCLASSEX wincl;
  25.     HINSTANCE  hThisInstance =  (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE);
  26.  
  27.     wincl.hInstance = hThisInstance;
  28.     wincl.lpszClassName = L"szClassName";
  29.     wincl.lpfnWndProc = WindowProcedure;
  30.     wincl.style = CS_DBLCLKS;
  31.     wincl.cbSize =sizeof (WNDCLASSEX);
  32.  
  33.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  34.     wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  35.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  36.     wincl.lpszMenuName = NULL;
  37.     wincl.cbClsExtra = 0;
  38.     wincl.cbWndExtra = 0;
  39.     wincl.hbrBackground = GetSysColorBrush (COLOR_3DFACE);
  40.  
  41.     if (!RegisterClassEx (&wincl))
  42.         return -1;
  43.  
  44.  
  45.     hWnd = CreateWindowEx(
  46.         0,
  47.         L"szClassName",
  48.         L"WindowsApp",
  49.         WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  50.         100,
  51.         100,
  52.         500,
  53.         500,
  54.         HWND_DESKTOP,        
  55.         NULL,
  56.         hThisInstance,
  57.         NULL
  58.         );
  59.  
  60.  
  61.     while (GetMessage(&msg, NULL, NULL, NULL))
  62.     {
  63.         TranslateMessage(&msg);
  64.         DispatchMessage(&msg);
  65.     }
  66.  
  67.     return 0;
  68. }
  69.  
  70. //HBRUSH CreateGradientBrush(COLORREF top, COLORREF bottom, LPNMCUSTOMDRAW item)
  71. //    {
  72. //        HBRUSH Brush = NULL;
  73. //        HDC hdcmem = CreateCompatibleDC(item->hdc);
  74. //        HBITMAP hbitmap = CreateCompatibleBitmap(item->hdc, item->rc.right-item->rc.left, item->rc.bottom-item->rc.top);
  75. //        SelectObject(hdcmem, hbitmap);
  76. //
  77. //        int r1 = GetRValue(top), r2 = GetRValue(bottom), g1 = GetGValue(top), g2 = GetGValue(bottom), b1 = GetBValue(top), b2 = GetBValue(bottom);
  78. //        for(int i = 0; i < item->rc.bottom-item->rc.top; i++)
  79. //        { 
  80. //            RECT temp;
  81. //            int r,g,b;
  82. //            r = int(r1 + double(i * (r2-r1) / item->rc.bottom-item->rc.top));
  83. //            g = int(g1 + double(i * (g2-g1) / item->rc.bottom-item->rc.top));
  84. //            b = int(b1 + double(i * (b2-b1) / item->rc.bottom-item->rc.top));
  85. //            Brush = CreateSolidBrush(RGB(r, g, b));
  86. //            temp.left = 0;
  87. //            temp.top = i;
  88. //            temp.right = item->rc.right-item->rc.left;
  89. //            temp.bottom = i + 1; 
  90. //            FillRect(hdcmem, &temp, Brush);
  91. //            DeleteObject(Brush);
  92. //        }
  93. //        HBRUSH pattern = CreatePatternBrush(hbitmap);
  94. //
  95. //        DeleteDC(hdcmem);
  96. //        DeleteObject(Brush);
  97. //        DeleteObject(hbitmap);
  98. //
  99. //        return pattern;
  100. //    }
  101. //
  102.  
  103. void AddControls(HWND hWnd)
  104. {
  105.     //    HINSTANCE hThisInstance =  (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE);    
  106.     HWND h= CreateWindow(L"Button", L"Click to save text", WS_VISIBLE |WS_CHILD | BS_OWNERDRAW | BS_PUSHBUTTON, 200, 300, 150, 50, hWnd,(HMENU)IDC_BUTTON, NULL, NULL);
  107.     //SetWindowSubclass(h, &WindowProcedure, IDC_BUTTON, NULL);
  108. }
  109.  
  110. LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  111. {     /* static HBRUSH defaultbrush = NULL;
  112.         static HBRUSH hotbrush = NULL;
  113.         static HBRUSH selectbrush = NULL;
  114.         static HBRUSH push_uncheckedbrush = NULL;
  115.         static HBRUSH push_checkedbrush = NULL;
  116.         static HBRUSH push_hotbrush1 = NULL;
  117.         static HBRUSH push_hotbrush2 = NULL;  */
  118.     switch (msg)
  119.     {
  120.     case WM_CREATE:
  121.         AddControls(hWnd);
  122.         break;    
  123.  
  124.     case WM_DESTROY:
  125.         //            CloseHandle(hWnd);
  126.         PostQuitMessage (0);
  127.         break;
  128.  
  129.         //    /*    case BN_CLICKED:
  130.         //            MessageBox(NULL,L"Click",L"Button",0);
  131.         //            break;
  132.         //*/
  133.         //          case WM_DRAWITEM:
  134.         //     
  135.         //case WM_NOTIFY:
  136.         //    {
  137.         //
  138.         //        LPNMHDR some_item = (LPNMHDR)lParam;
  139.  
  140.         //        if (some_item->idFrom == IDC_BUTTON && some_item->code == NM_CUSTOMDRAW)
  141.         //        {
  142.         //            LPNMCUSTOMDRAW item = (LPNMCUSTOMDRAW)some_item;
  143.  
  144.         //            if (item->uItemState & CDIS_SELECTED)
  145.         //            {
  146.         //                //Select our color when the button is selected
  147.         //                if (selectbrush == NULL)
  148.         //                    selectbrush = CreateGradientBrush(RGB(180, 0, 0), RGB(255, 180, 0), item);
  149.  
  150.         //                //Create pen for button border
  151.         //                HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
  152.  
  153.         //                //Select our brush into hDC
  154.         //                HGDIOBJ old_pen = SelectObject(item->hdc, pen);
  155.         //                HGDIOBJ old_brush = SelectObject(item->hdc, selectbrush);
  156.  
  157.         //                //If you want rounded button, then use this, otherwise use FillRect().
  158.         //                RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 5, 5);
  159.  
  160.         //                //Clean up
  161.         //                SelectObject(item->hdc, old_pen);
  162.         //                SelectObject(item->hdc, old_brush);
  163.         //                DeleteObject(pen);
  164.  
  165.         //                //Now, I don't want to do anything else myself (draw text) so I use this value for return:
  166.         //                return CDRF_DODEFAULT;
  167.         //                //Let's say I wanted to draw text and stuff, then I would have to do it before return with
  168.         //                //DrawText() or other function and return CDRF_SKIPDEFAULT
  169.         //            }
  170.         //            else
  171.         //            {
  172.         //                if (item->uItemState & CDIS_HOT) //Our mouse is over the button
  173.         //                {
  174.         //                    //Select our color when the mouse hovers our button
  175.         //                    if (hotbrush == NULL)
  176.         //                        hotbrush = CreateGradientBrush(RGB(255, 230, 0), RGB(245, 0, 0), item);
  177.  
  178.         //                    HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
  179.  
  180.         //                    HGDIOBJ old_pen = SelectObject(item->hdc, pen);
  181.         //                    HGDIOBJ old_brush = SelectObject(item->hdc, hotbrush);
  182.  
  183.         //                    RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 5, 5);
  184.  
  185.         //                    SelectObject(item->hdc, old_pen);
  186.         //                    SelectObject(item->hdc, old_brush);
  187.         //                    DeleteObject(pen);
  188.  
  189.         //                    return CDRF_DODEFAULT;
  190.         //                }
  191.  
  192.         //                //Select our color when our button is doing nothing
  193.         //                if (defaultbrush == NULL)
  194.         //                    defaultbrush = CreateGradientBrush(RGB(255, 180, 0), RGB(180, 0, 0), item);
  195.  
  196.         //                HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
  197.  
  198.         //                HGDIOBJ old_pen = SelectObject(item->hdc, pen);
  199.         //                HGDIOBJ old_brush = SelectObject(item->hdc, defaultbrush);
  200.  
  201.         //                RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 5, 5);
  202.  
  203.         //                SelectObject(item->hdc, old_pen);
  204.         //                SelectObject(item->hdc, old_brush);
  205.         //                DeleteObject(pen);
  206.  
  207.         //                return CDRF_DODEFAULT;
  208.         //            }
  209.         //        }
  210.         //        else if (some_item->idFrom == IDC_PUSHLIKE_BUTTON && some_item->code == NM_CUSTOMDRAW)
  211.         //        {
  212.         //            LPNMCUSTOMDRAW item = (LPNMCUSTOMDRAW)some_item;
  213.  
  214.         //            if (IsDlgButtonChecked(hwnd, some_item->idFrom))
  215.         //            {
  216.         //                if (item->uItemState & CDIS_HOT)
  217.         //                {
  218.  
  219.         //                    if (push_hotbrush1 == NULL)
  220.         //                        push_hotbrush1 = CreateGradientBrush(RGB(0, 0, 245), RGB(0, 230, 255), item);
  221.  
  222.         //                    HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
  223.  
  224.         //                    HGDIOBJ old_pen = SelectObject(item->hdc, pen);
  225.         //                    HGDIOBJ old_brush = SelectObject(item->hdc, push_hotbrush1);
  226.  
  227.         //                    RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 10, 10);
  228.  
  229.         //                    SelectObject(item->hdc, old_pen);
  230.         //                    SelectObject(item->hdc, old_brush);
  231.         //                    DeleteObject(pen);
  232.  
  233.         //                    return CDRF_DODEFAULT;
  234.         //                }
  235.  
  236.  
  237.         //                if (push_checkedbrush == NULL)
  238.         //                    push_checkedbrush = CreateGradientBrush(RGB(0, 0, 180), RGB(0, 222, 200), item);
  239.  
  240.  
  241.         //                HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
  242.  
  243.  
  244.         //                HGDIOBJ old_pen = SelectObject(item->hdc, pen);
  245.         //                HGDIOBJ old_brush = SelectObject(item->hdc, push_checkedbrush);
  246.  
  247.  
  248.         //                RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 10, 10);
  249.  
  250.  
  251.         //                SelectObject(item->hdc, old_pen);
  252.         //                SelectObject(item->hdc, old_brush);
  253.         //                DeleteObject(pen);
  254.  
  255.  
  256.         //                return CDRF_DODEFAULT;
  257.         //            }
  258.         //            else
  259.         //            {
  260.         //                if (item->uItemState & CDIS_HOT)
  261.         //                {
  262.         //                    if (push_hotbrush2 == NULL)
  263.         //                        push_hotbrush2 = CreateGradientBrush(RGB(255, 230, 0), RGB(245, 0, 0), item);
  264.  
  265.         //                    HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
  266.  
  267.         //                    HGDIOBJ old_pen = SelectObject(item->hdc, pen);
  268.         //                    HGDIOBJ old_brush = SelectObject(item->hdc, push_hotbrush2);
  269.  
  270.         //                    RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 10, 10);
  271.  
  272.         //                    SelectObject(item->hdc, old_pen);
  273.         //                    SelectObject(item->hdc, old_brush);
  274.         //                    DeleteObject(pen);
  275.  
  276.         //                    return CDRF_DODEFAULT;
  277.         //                }
  278.  
  279.         //                if (push_uncheckedbrush == NULL)
  280.         //                    push_uncheckedbrush = CreateGradientBrush(RGB(255, 180, 0), RGB(180, 0, 0), item);
  281.  
  282.         //                HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));
  283.  
  284.         //                HGDIOBJ old_pen = SelectObject(item->hdc, pen);
  285.         //                HGDIOBJ old_brush = SelectObject(item->hdc, defaultbrush);
  286.  
  287.         //                RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 10, 10);
  288.  
  289.         //                SelectObject(item->hdc, old_pen);
  290.         //                SelectObject(item->hdc, old_brush);
  291.         //                DeleteObject(pen);
  292.  
  293.         //                return CDRF_DODEFAULT;
  294.         //            }
  295.         //        }
  296.         //        return CDRF_DODEFAULT;
  297.         //    }
  298.  
  299.         //            
  300.         //    break;
  301. //    case WM_PAINT:
  302. //        {
  303. //            PAINTSTRUCT ps;
  304. //            BeginPaint(hWnd, &ps);
  305. //
  306. //
  307. //        //    RECT rcButton;
  308. //        //    rcButton.left   = 10;
  309. //        //    rcButton.top    = 10;
  310. //        //    rcButton.right  = rcButton.left + 5;
  311. //        //    rcButton.bottom = rcButton.top  + 50;
  312. //
  313. //            // Set the DC's background color to whatever color we want the button to be.
  314. //            /// In this case, we use a nice orange. You can use anything you want.
  315. //
  316. //            COLORREF clrOriginal = SetBkColor(ps.hdc, RGB(0, 255,0));
  317. //    //        if (clrOriginal != CLR_INVALID)
  318. //    //        {
  319. //               // Fill the button's rectangle with a solid color.
  320. //        //       ExtTextOut(ps.hdc, 10, 300, ETO_OPAQUE, &rcButton,NULL, 0, NULL);
  321. //
  322. //               // Draw the button's border.
  323. ////               DrawEdge(ps.hdc, &rcButton, EDGE_RAISED, BF_TOPLEFT | BF_BOTTOMRIGHT|BS_PUSHBUTTON);
  324. //
  325. //               // Restore the DC's original background color.
  326. ////               SetBkColor(ps.hdc, clrOriginal);
  327. //    //        }
  328. //
  329. //    //        EndPaint(hWnd, &ps);
  330. //            break;
  331. //        }
  332.  
  333.  
  334. /*#define WM_MOUSEMOVE                    0x0200*/
  335.  
  336.  
  337.  
  338.     case WM_DRAWITEM:
  339.  
  340.  
  341.  
  342.         LPDRAWITEMSTRUCT Item;
  343.         Item = (LPDRAWITEMSTRUCT)lParam;
  344.         //SelectObject(Item->hDC, CreateFont(16, 0, 0, 0, FW_NORMAL, 0, 0, 0, 
  345.             //DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, L"Arial Black"));
  346.         FillRect(Item->hDC, &Item->rcItem, CreateSolidBrush(0));
  347.         SelectObject(Item->hDC, CreateSolidBrush(0));
  348.         if (Item->itemState & ODS_SELECTED)
  349.         {
  350.             SetTextColor(Item->hDC, 0);
  351.             SelectObject(Item->hDC, CreateSolidBrush(0xFF00));
  352.             SelectObject(Item->hDC, CreatePen(PS_SOLID, 255, 0xFF00));
  353.         }
  354.         /*else if(Item->itemState & ODT_STATIC)
  355.         {
  356.                 SetTextColor(Item->hDC, 255);
  357.             SelectObject(Item->hDC, CreateSolidBrush(0xFF00));
  358.             SelectObject(Item->hDC, CreatePen(PS_SOLID, 0, 0xFF00));
  359.         }*/
  360.         else
  361.         {
  362.             SetTextColor(Item->hDC, 0x00FF00);  
  363.             SelectObject(Item->hDC, CreatePen(PS_SOLID, 2, 0x00FF00));
  364.  
  365.         }
  366.         SetBkMode(Item->hDC, TRANSPARENT);
  367.         RoundRect(Item->hDC, Item->rcItem.left, Item->rcItem.top, Item->rcItem.right, Item->rcItem.bottom, 20, 20);
  368.         //int len;
  369.         //len = GetWindowTextLength(Item->hwndItem);
  370.         //LPSTR lpBuff;
  371.         //lpBuff = new char[len+1];
  372.         //GetWindowTextA(Item->hwndItem, lpBuff, len+1);
  373.         //DrawTextA(Item->hDC, lpBuff, len, &Item->rcItem, DT_CENTER);
  374.  
  375.             break;
  376.  
  377.  
  378.  
  379.         case WM_MOUSEMOVE:
  380.  
  381.  
  382.     LPDRAWITEMSTRUCT Item;
  383.         Item = (LPDRAWITEMSTRUCT)lParam;
  384.         //SelectObject(Item->hDC, CreateFont(16, 0, 0, 0, FW_NORMAL, 0, 0, 0, 
  385.             //DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, L"Arial Black"));
  386.         FillRect(Item->hDC, &Item->rcItem, CreateSolidBrush(0));
  387.         SelectObject(Item->hDC, CreateSolidBrush(0));
  388.         if (Item->itemState & ODS_SELECTED)
  389.         {
  390.             SetTextColor(Item->hDC, 0);
  391.             SelectObject(Item->hDC, CreateSolidBrush(0xFF00));
  392.  
  393.         }    
  394.  
  395.     break;
  396.  
  397.     default:
  398.         return DefWindowProc (hWnd, msg, wParam, lParam);
  399.     }
4 Weeks Ago #1
Share this Question
Share on Google+
1 Reply


zmbd
Expert Mod 5K+
P: 5,397
arani,
That is a lot of code to expect someone to read through.

Can you please narrow down your question?
Specifically, are you receiving any errors, what IDE are you using for developement (Visual Studio, Eclipse, VIM...), what OS are you using (Unix(variant thereof), Windows(version), IOS...)
4 Weeks Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.