SetBkColor in win32
-
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
-
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
Did you see
WM_CTLCOLOR
message.
WhiteSky
-
Did you see
WM_CTLCOLOR
message.
WhiteSky
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
-
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
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
-
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
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
-
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
-
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
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