dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Color indicator

  1. #1
    Join Date
    Feb 2019
    Posts
    4

    Color indicator

    I can't figure out how to implement an element on the dialog box that shows the current color of something.
    I tried to do this with the Picture control, but I cannot fill the bitmap with the desired color.
    Code:
    void DrawPicture(HWND hDlg, HDC hdc)
    {
    	RECT rcPicture;
    
    	HWND hPicture = GetDlgItem(hDlg, IDC_PICTURE);
    	GetWindowRect(hPicture, &rcPicture);
    	int width = rcPicture.right - rcPicture.left;
    	int height = rcPicture.bottom - rcPicture.top;
    	HDC hMemDC = CreateCompatibleDC(hdc);
    	HBITMAP bPicture = CreateCompatibleBitmap(hdc, width, height);
    	SelectObject(hMemDC, bPicture);
    	HBRUSH hBrush = CreateSolidBrush(RGB(58, 37, 11));
    	SelectObject(hMemDC, hBrush);
    	//BitBlt(hdc, 450, 450, width, height, hMemDC, 0, 0, SRCCOPY);
    	
    	SendMessage(hPicture, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bPicture);
    }
    The result of what I want to get is shown on a screenshot from Photoshop as a large cyan square. Please tell me how to implement it correctly.
    Name:  pic.jpg
Views: 115
Size:  18.7 KB

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,148

    Re: Color indicator

    One way is to just implement the drawing in the OnPaint handler of your dialog. For example:
    Code:
    void CMyDialog::OnPaint()
    {
    	CPaintDC dc(this);
    
    	CRect rect;
    	GetDlgItem(IDC_PICTURE_CONTROL)->GetWindowRect(rect);
    	ScreenToClient(rect);
    	dc.FillSolidRect(rect, RGB(255, 255, 0));
    		
    	CDialogEx::OnPaint();
    }
    This queries the size and location of the rectangle to draw from the IDC_PICTURE_CONTROL control on the dialog.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Feb 2019
    Posts
    4

    Re: Color indicator

    Quote Originally Posted by Marc G View Post
    One way is to just implement the drawing in the OnPaint handler of your dialog. For example:
    Code:
    void CMyDialog::OnPaint()
    {
    	CPaintDC dc(this);
    
    	CRect rect;
    	GetDlgItem(IDC_PICTURE_CONTROL)->GetWindowRect(rect);
    	ScreenToClient(rect);
    	dc.FillSolidRect(rect, RGB(255, 255, 0));
    		
    	CDialogEx::OnPaint();
    }
    This queries the size and location of the rectangle to draw from the IDC_PICTURE_CONTROL control on the dialog.
    I thought about this, but then it would be inconvenient to catch clicking on this element. And I decided that this idea is not very appropriate. Is it still possible to solve this without drawing a rectangle?
    Well, the code will not work for me because its the MFC, but there will be no problems coming down.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,002

    Re: Color indicator

    Painting has nothing to do with intercepting mouse clicks. Btw, Marc's code snippet may be in MFC, and you may be coding in native WinAPI, but MFC is just a wrapper over native, so it shouldn't be a problem handling the paint message natively.

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,148

    Re: Color indicator

    Quote Originally Posted by Arjay View Post
    Painting has nothing to do with intercepting mouse clicks. Btw, Marc's code snippet may be in MFC, and you may be coding in native WinAPI, but MFC is just a wrapper over native, so it shouldn't be a problem handling the paint message natively.
    Right, my mistake. My code is indeed using MFC syntax, but as Arjay said, it's pretty much straightforward to translate that to Win32 API calls.

    To intercept mouse clicks, you can just handle mouse events in your dialogs.

    Alternative, if you want true custom controls, maybe this article can help you: https://www.codeproject.com/articles...i-the-painting
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  6. #6
    Join Date
    Feb 2019
    Posts
    4

    Re: Color indicator

    Quote Originally Posted by Marc G View Post
    Right, my mistake. My code is indeed using MFC syntax, but as Arjay said, it's pretty much straightforward to translate that to Win32 API calls.

    To intercept mouse clicks, you can just handle mouse events in your dialogs.

    Alternative, if you want true custom controls, maybe this article can help you: https://www.codeproject.com/articles...i-the-painting
    Yes, transfer to the MFC is not a problem.
    And intercept click on this element with using WM_LBUTTONDOWN and check the coordinates of the location?

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,148

    Re: Color indicator

    Yes, that's one way to detect clicks.
    As said, if you want to encapsulate it further, you might want to look at custom control. Here is a good set of articles.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  8. #8
    Join Date
    Feb 2019
    Posts
    4

    Re: Color indicator

    I got it. Thank you all for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)