Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. SetBkColor in win32

SetBkColor in win32

Scheduled Pinned Locked Moved C / C++ / MFC
comhelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M_Menon
    wrote on last edited by
    #1

    Hi everybody i am new with win32 app. I want to set background color and color inside the controls. I have no idea how it works. Definitely not from Properties as we do in web app. I saw many methods and all i tried the site http://www.functionx.com/win32/Lesson16.htm which is i guess the best for win32 apps. I see some methods in this page but i dont how i can call this methods in my OnInitDialog() method. I see at places HDC hDC; WPARAM wParam; hDC = (HDC)wParam; hDC->SetBkColor(hDC,RGB(200, 0, 0)); but then if i write this code in myOnInitDialog() then i need to pass this parameters HDC, WPARAM which i dont. I believe it must be preety simple, please give me your suggestion if any, Any help would be appreciated. Mady

    H D G 3 Replies Last reply
    0
    • M M_Menon

      Hi everybody i am new with win32 app. I want to set background color and color inside the controls. I have no idea how it works. Definitely not from Properties as we do in web app. I saw many methods and all i tried the site http://www.functionx.com/win32/Lesson16.htm which is i guess the best for win32 apps. I see some methods in this page but i dont how i can call this methods in my OnInitDialog() method. I see at places HDC hDC; WPARAM wParam; hDC = (HDC)wParam; hDC->SetBkColor(hDC,RGB(200, 0, 0)); but then if i write this code in myOnInitDialog() then i need to pass this parameters HDC, WPARAM which i dont. I believe it must be preety simple, please give me your suggestion if any, Any help would be appreciated. Mady

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #2

      Did you see WM_CTLCOLOR message.


      WhiteSky


      M 1 Reply Last reply
      0
      • H Hamid Taebi

        Did you see WM_CTLCOLOR message.


        WhiteSky


        M Offline
        M Offline
        M_Menon
        wrote on last edited by
        #3

        Firstly thanks for the immediate response. Now I had seen this (WM_CTLCOLOR ), but i dont remember exactly in which part? I see everytime they are mentioning about two main methods those are 1. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow). 2.LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) I have copied them in my .cpp file. But unfortunately i dont know where to call these methods. These methods are never being called. I guess they should be called on OnInitDialog(); but then i am not using any of these parameters in my code such as HINSTANCE, LPSTR or HDC. If you want please let me know i shall paste my complete code here for better assistance. Mady

        D 1 Reply Last reply
        0
        • M M_Menon

          Firstly thanks for the immediate response. Now I had seen this (WM_CTLCOLOR ), but i dont remember exactly in which part? I see everytime they are mentioning about two main methods those are 1. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow). 2.LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) I have copied them in my .cpp file. But unfortunately i dont know where to call these methods. These methods are never being called. I guess they should be called on OnInitDialog(); but then i am not using any of these parameters in my code such as HINSTANCE, LPSTR or HDC. If you want please let me know i shall paste my complete code here for better assistance. Mady

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          mady1380 wrote:

          I have copied them in my .cpp file.

          You don't need either if you are using MFC.


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          1 Reply Last reply
          0
          • M M_Menon

            Hi everybody i am new with win32 app. I want to set background color and color inside the controls. I have no idea how it works. Definitely not from Properties as we do in web app. I saw many methods and all i tried the site http://www.functionx.com/win32/Lesson16.htm which is i guess the best for win32 apps. I see some methods in this page but i dont how i can call this methods in my OnInitDialog() method. I see at places HDC hDC; WPARAM wParam; hDC = (HDC)wParam; hDC->SetBkColor(hDC,RGB(200, 0, 0)); but then if i write this code in myOnInitDialog() then i need to pass this parameters HDC, WPARAM which i dont. I believe it must be preety simple, please give me your suggestion if any, Any help would be appreciated. Mady

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            mady1380 wrote:

            I see at places HDC hDC; WPARAM wParam; hDC = (HDC)wParam; hDC->SetBkColor(hDC,RGB(200, 0, 0));

            You may have seen it, but it definitely won't work. For an example of how to change the font of a static control, see the Extras section of this article. Changing the color requires only a very minor change.


            "A good athlete is the result of a good and worthy opponent." - David Crow

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            M 1 Reply Last reply
            0
            • D David Crow

              mady1380 wrote:

              I see at places HDC hDC; WPARAM wParam; hDC = (HDC)wParam; hDC->SetBkColor(hDC,RGB(200, 0, 0));

              You may have seen it, but it definitely won't work. For an example of how to change the font of a static control, see the Extras section of this article. Changing the color requires only a very minor change.


              "A good athlete is the result of a good and worthy opponent." - David Crow

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              M Offline
              M Offline
              M_Menon
              wrote on last edited by
              #6

              Thanks David. Will try and get back to you whatever the result is. Regards Mady

              1 Reply Last reply
              0
              • M M_Menon

                Hi everybody i am new with win32 app. I want to set background color and color inside the controls. I have no idea how it works. Definitely not from Properties as we do in web app. I saw many methods and all i tried the site http://www.functionx.com/win32/Lesson16.htm which is i guess the best for win32 apps. I see some methods in this page but i dont how i can call this methods in my OnInitDialog() method. I see at places HDC hDC; WPARAM wParam; hDC = (HDC)wParam; hDC->SetBkColor(hDC,RGB(200, 0, 0)); but then if i write this code in myOnInitDialog() then i need to pass this parameters HDC, WPARAM which i dont. I believe it must be preety simple, please give me your suggestion if any, Any help would be appreciated. Mady

                G Offline
                G Offline
                ghle
                wrote on last edited by
                #7

                Mady1380 - You have to intercept the method called when the color of the controls is determined - OnCtlColor(). OnCtlColor is called each time a control is being painted/repainted. Examine nCtlColor to see what type of control is being painted. The following code makes the background of an edit control either yellow when it has the focus, or normal white when it does not have the focus. It also forces the text color to black. Add this method to your dialog class. BEGIN_MESSAGE_MAP(CMyDlg, CDialog)   //{{AFX_MSG_MAP(CMyDlg)   **ON_WM_CTLCOLOR()**   //}}AFX_MSG_MAP END_MESSAGE_MAP() ... /****************************************************************  * OnCtlColor()  ****************************************************************/ HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {   HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);   if (CTLCOLOR_EDIT == nCtlColor)   {     if (GetFocus() == pWnd)     {       // Set the text color to black.       pDC->SetTextColor(RGB(0, 0, 0));       // Set the background to YELLOW.       pDC->SetBkColor(RGB(255, 255, 0));       // Set the background mode so background will show thru.       pDC->SetBkMode(OPAQUE);       return CreateSolidBrush(RGB(255,255,0)); // Yellow for active edit box     }     else     {       // Set the text color to black.       pDC->SetTextColor(RGB(0, 0, 0));       // Set the background to WHITE.       pDC->SetBkColor(RGB(255, 255, 255));       // Set the background mode so background will show thru.       pDC->SetBkMode(OPAQUE);       return CreateSolidBrush(RGB(255, 255, 255)); // White for inactive edit box     }   }   return hbr; } Experiment to get a better feeling by changing background colors, text colors, types of controls (EDIT, STATIC, etc.). Hope this helps. Gary

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups