-
February 3rd, 2019, 06:34 AM
#1
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.
-
February 3rd, 2019, 07:43 AM
#2
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.
-
February 3rd, 2019, 10:08 AM
#3
Re: Color indicator
Originally Posted by Marc G
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.
-
February 3rd, 2019, 12:21 PM
#4
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.
-
February 3rd, 2019, 01:55 PM
#5
Re: Color indicator
Originally Posted by Arjay
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
-
February 4th, 2019, 04:29 AM
#6
Re: Color indicator
Originally Posted by Marc G
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?
-
February 5th, 2019, 02:48 AM
#7
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.
-
February 5th, 2019, 03:30 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|