CDateTimeCtrl background RRS feed

All replies

  • If you're taking about the Edit part, a way is to subclass it and handle WM_PAINT (WM_PRINTTransparentBlt for example to change the background color)

    A test :

    Thursday, July 9, 2020 11:45 AM
  •  Yes thank you,But i am using DTS_UPDOWN  spin buttons for  CDateTimeCtrl  and i wouldlike to set background color for it also.Can you help me in it 

    Thursday, July 9, 2020 5:19 PM
  • It is the same thing with  DTS_UPDOWN :

    Thursday, July 9, 2020 5:36 PM
  • Hi,

    But WM_PAINT only seems to work for window of picker and it skips the spin button while painting.

    Thursday, July 9, 2020 5:59 PM
  • The spin button is a child Up-down Control that you can paint too

    Test :

    • Proposed as answer by Naomi N Thursday, July 9, 2020 7:29 PM
    Thursday, July 9, 2020 7:19 PM
  • Can you share me the strip of code  showing how you did that .That would be a great help

    Friday, July 10, 2020 2:35 AM
  • I tested quickly in a C++/ Win32 app

    I subclassed the controls, handled WM_PAINT only, where I replaced colors with this function (I modified it a bit for the Up-down Control as colors are not uniform)=>

    (can also be done with TransparentBlt to avoid the DIB section)

    void ReplaceColor(HWND hWnd, COLORREF nOldColor, COLORREF nNewColor)
    	RECT rect;
    	GetWindowRect(hWnd, &rect);
    	MapWindowPoints(NULL, hWnd, (LPPOINT)&rect, 2);
    	long nWidth = rect.right - rect.left, nHeight = rect.bottom - rect.top;
    	HDC hDC = GetDC(hWnd);
    	HDC hDCMem = CreateCompatibleDC(hDC);
    	int nX = nWidth, nY = nHeight;
    	BITMAPINFO bi = { 0 };
    	bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
    	bi.bmiHeader.biWidth = nX;
    	bi.bmiHeader.biHeight = nY;
    	bi.bmiHeader.biPlanes = 1;
    	bi.bmiHeader.biBitCount = 32;
    	bi.bmiHeader.biCompression = BI_RGB;
    	BYTE *pbBitmap;
    	HBITMAP hBitmap = CreateDIBSection(hDCMem, &bi, DIB_RGB_COLORS, (void**)&pbBitmap, NULL, 0);
    	if (hBitmap)
    		HBITMAP hBitmapOld = (HBITMAP)SelectObject(hDCMem, hBitmap);
    		RGBQUAD *pRGB;
    		LONG nPixels;
    		int nPixelX = 0, nPixelY = 0;
    		for (pRGB = (RGBQUAD *)pbBitmap, nPixels = nX * nY; nPixels > 0; ++pRGB, --nPixels)
    			ULONG* pSrc = (ULONG*)pRGB;
    			ULONG nRed = GetRValue(*pSrc);
    			ULONG nGreen = GetGValue(*pSrc);
    			ULONG nBlue = GetBValue(*pSrc);
    			if (nRed == GetRValue(nOldColor) && nGreen == GetGValue(nOldColor) && nBlue == GetBValue(nOldColor))
    				pRGB->rgbRed = GetRValue(nNewColor);
    				pRGB->rgbGreen = GetGValue(nNewColor);
    				pRGB->rgbBlue = GetBValue(nNewColor);
    		BitBlt(hDC, rect.left, rect.top, nWidth, nHeight, hDCMem, 0, 0, SRCCOPY);
    		SelectObject(hDCMem, hBitmapOld);
    	ReleaseDC(hWnd, hDC);

    • Edited by Castorix31 Friday, July 10, 2020 6:04 AM
    Friday, July 10, 2020 5:59 AM
  • Thank you for previous solution.

    Is it possible to change width of spin button.If we can can you show how.

    Friday, July 10, 2020 9:11 AM
  • Thank you for previous solution.

    Is it possible to change width of spin button.If we can can you show how.

    Like for any control, with APIs like SetWindowPos

    , after having got its handle (I used GetWindow(hDTP, GW_CHILD)) :

    Friday, July 10, 2020 9:24 AM
  • I tried it but it doesn't seems to work as i want .The icon disappears too .Do you know any other approach.
    Saturday, July 11, 2020 1:19 PM
  • I tried it but it doesn't seems to work as i want .The icon disappears too .Do you know any other approach.

    Which icon ?

    You can see in the picture that I could increase the width of the Up-down control with SetWindowPos

    (I moved it to left too, as the size is bigger)
    • Edited by Castorix31 Saturday, July 11, 2020 1:40 PM
    Saturday, July 11, 2020 1:25 PM
  • Yes ,sizing worked well.

    The setting of background color to spin buttons seems to make the icon disappear .

    Can you tell me some other approach to it .

    Saturday, July 11, 2020 2:32 PM
  • I don't see which icon you're talking about

    The DateTimePicker control has the Up-Down control as child, which draws arrows with DrawThemeBackground or DrawFrameControl,
    (that I colored in red above for the tests, either the background or the arrows, by replacing colors with the ReplaceColor function)
    Saturday, July 11, 2020 5:01 PM
  • Hi ,

    I mighthave gone through it wrongly.I am not sure about the subclassing and directly set background in WM_PAINT.Can you show the subclassing concept too.

    Thank you

    Sunday, July 12, 2020 9:25 AM
  • There is nothing different from what I posted, I just handle WM_PAINT

    For example, for the DTP control (same thing for the Up-Down control, with another ReplaceColor function to change colors differently) =>

    BOOL bRet = SetWindowSubclass(hDTP, DTPSubclassProc, 0, 0);

    LRESULT CALLBACK DTPSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
    	switch (uMsg)
    		case WM_PAINT:
    			PAINTSTRUCT ps;
    			BeginPaint(hWnd, &ps);	
    			// White => Green
    			//ReplaceColor(hWnd, RGB(255, 255, 255), RGB(0, 255, 0));
    			ReplaceColor(hWnd, GetSysColor(COLOR_WINDOW), RGB(0, 255, 0));
    			EndPaint(hWnd, &ps);
    			return 0;
    	return DefSubclassProc(hWnd, uMsg, wParam, lParam);

    Sunday, July 12, 2020 11:20 AM