Add status icon to image in CListCtrl - c++

I have a CListCtrl in MFC where I am appending a set of 128x128 pixel images. Now I would like to append a 16x16 small status icon (OK/NOK style) to those images. How can I do this?

I think this may not solve your problem, but is near to be a solution.
CBitmap drawBitmap;
HICON hicon= m_pImageList->ExtractIcon(ix);
drawBitmap.Attach(hicon);
CDC dc;
dc.CreateCompatibleDC(NULL);
dc.SetBkMode(TRANSPARENT);
CPoint pt;
// do your calculations: pt will be define in what part of the image the icon will appear
DrawIcon(&dc.GetSafeHdc(), pt.x, pt.y, IDI_YOUR_ICON);
DeleteDC(dc);
m_pImageList->Replace(ix, &drawBitmap, (CBitmap*)NULL);
ix is the index of the one you want to replace.

Just after posting previous answer, I discovered that it exists CImageList::SetOverlayImage

Related

Want to show colored box around Richedit control in MFC at runtime

I have an mfc application. I have some richedit controls on the dialog. I want to show a yellow colored filled frame around the controls. What is the way to do this?
I tried to create one more rich edit ctrl around the existing richedit ctrl and use SetBackgroundColor on its variable, but it colors the entire area and other richedit ctrls become invisible. Also, I want to change the surrounding color at run time.
Please help me. I am stuck with this.
There may be a better way to accomplish this, but, the following should work. If you derive your own class from CRichEditCtrl, you can leverage the WM_NCPAINT message to render the border. Something like…
void RichEdit::OnNcPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect;
GetWindowRect(&rect);
ScreenToClient(rect);
CPen pen;
pen.CreatePen(PS_SOLID, 10, RGB(255, 255, 0));
dc.SelectObject(pen);
dc.Rectangle(&rect);
CHARFORMAT cf = { 0 };
int txtLen = GetTextLength();
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_ITALIC;
SetSel(txtLen, -1); ReplaceSel("Some text");
// Apply formating to the just inserted text.
SetSel(txtLen, GetTextLength());
SetSelectionCharFormat(cf);
SetFocus();
// Do not call CRichEditCtrl::OnNcPaint() for painting messages
}
Will render the border as Yellow, and, write the corresponding text. Here’s what it will look like.

How to prevent or turn off direct2d "autoscaling" of a bitmap?

I have a problem with Direct2D bitmap scaling. I loaded a bitmap from a file using that example, then I wanted to scale bitmap by myself (fit to view saving proportions, add a shadow effect…) but Direct2D automatically scales bitmap (e.g. while resizing a window) and I do not know how to prevent this behavior.
For example, after bitmap loaded into a small window (CView) it fills the entire window correctly (according OnDraw) and when I maximize it D2D stretch my bitmap with losing bitmap quality and finally the bitmap greatly exceeds borders of the window despite my OnDraw method.
void CWDCView::OnDraw(CDC* /*pDC*/)
{
CWDCDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc) return;
HRESULT hr = S_OK;
ID2D1DeviceContext *deviceContext;
pRenderTarget->QueryInterface(&deviceContext); //ID2D1HwndRenderTarget* pRenderTarget
RECT rc = {0,0,0,0};
GetClientRect(&rc);
deviceContext->BeginDraw();
deviceContext->Clear( D2D1::ColorF( D2D1::ColorF(0xC8D2E1, 1.0f) ) );
D2D1_RECT_F rect={0,0,rc.right,rc.bottom};
deviceContext->DrawBitmap(m_pBitmap,rect); //ID2D1Bitmap *m_pBitmap
deviceContext->EndDraw();
if (hr == D2DERR_RECREATE_TARGET)
{
hr = S_OK;
ReleaseDeviceResources();
}
SafeRelease(&deviceContext);
}
So how to prevent or turn off this “autoscaling”?
I want to add one thing. If draw bitmap like that deviceContext->DrawBitmap(m_pBitmap); /*without rect*/ it draws just a part of the bitmap without fitting it into the window but when maximizing it stretch it anyway.
A render target could not be just automatically resized. In your case, you are changing the size of the window, but this doesn't cause a change of the "attached" render target size.
You should handle the resize event of your window and then you have two possibilities:
Recreate your render target with the new size.
Use ID2D1HwndRenderTarget::Resize or IDXGISwapChain::ResizeBuffers
Additionally, you can check:
Resizing Render Target Direct2D after WM_SIZE
Smooth window resizing in Windows (using Direct2D 1.1)?

Custom image toolbar WTL

I want to create a toolbar with custom image buttons, I have the images in .ico format how can I add them to the toolar in WTL? I'm trying to edit the images in the toolbar list, but there such poor quality, how can I add better quality images?
If you have a WTL Toolbar control that is already created you can attach images to it with the SetImageList() and SetHotImageList() methods of the CToolBarCtrl class. E.g.
CToolBarCtrl toolbar;
CImage image;
CBitmap bitmap;
// ... load the image into the bitmap ...
images.Create(32, 32, ILC_COLOR32 | ILC_MASK, 0, 1);
// repeat this for each image you want to use in the toolabr
images.Add(bitmap, RGB(255, 255, 255));
toolbar.SetImageList(images.Detach());
//... do the same for the hot (hover) images ...
The images can then be used by referencing the return value of the CImageList:Add() method.
Make sure you detach the image list from the CImageList class as I have done here otherwise the image list will be deleted when it goes out of scope.

How to increase thickness of slider conrol in mfc?

How can I increase the thickness of a CSliderCtrl (slider control) in MFC?
MoveWindow() can be used for any MFC control. Try this:
CRect rc;
slider.GetWindowRect(rc); // Get the slider rectangle in absolute corrdinates
rc.InflateRect(30, 30); // Do whatever you want with your rectangle;
ScreenToClient (rc); // Convert to dialogs's coordinates
slider.MoveWindow(rc); // Move it!
Update:
For further customization you have to make an owner-drawn CListCtrl. You may take this article as a good start for that

how to add bitmap image to buttons in MFC?

I am Trying to add an image to an existing button..I have done that to an extent, the problem is I can add an ownerdrawn Image but am not able to add the extact image that I want.. for the example see the below code
CButton* pBtn= (CButton*)GetDlgItem(ID_WIZBACK);
pBtn->ModifyStyle( 0, BS_ICON );
HICON hIcn= (HICON)LoadImage(
AfxGetApp()->m_hInstance,
MAKEINTRESOURCE(IDI_ICON3),
IMAGE_ICON,
0,0, // use actual size
LR_DEFAULTCOLOR
);
pBtn->SetIcon( hIcn );
with the above code am converting the bitmap to an icon to add to my button...how can I add the exact Bitmap image directly to an existing button.Please help me frnds..
Steps for assigning bitmap to button in mfc :
Create object of bitmap
Load bitmap by using LoadBitmap()
Get Handle of button using id and GetDlgItem() method
Modify style so that we can assign bitmap to it
use SetBitmap() on button's handle to assign bitmap
Code :
CBitmap bmp;
bmp.LoadBitmap( IDB_BITMAP4 );
CButton* pButton = (CButton* )GetDlgItem(IDC_BUTTON1);
pButton->ModifyStyle(0,BS_BITMAP);
pButton->SetBitmap(bmp);
I actually fixed the problem..what I did is I replaced the HICON with HBITMAP and its working perfect...basically both would work fine but in my case when I loaded the icon into the button the background of the icon was not changing...I tried Bitmap then it work great. Now am working on positioning the Image and to add text...think I could go through
you don't know how much this helped out. Thanks for posting. Also have to change a few other things to bitmap as well ...
CButton* pBtn= (CButton*)GetDlgItem(ID_MYDIALOG);
pBtn->ModifyStyle( 0, BS_BITMAP );
HBITMAP hIcn= (HBITMAP)LoadImage(
AfxGetApp()->m_hInstance,
MAKEINTRESOURCE(IDB_MYPIC),
IMAGE_BITMAP,
0,0, // use actual size
LR_DEFAULTCOLOR
);
pBtn->SetBitmap( hIcn );
You could subclass existing button using CBitmapButton::SubclassWindow, then use LoadBitmaps.
Use the button classes from the Feature Pack. They have support for showing both text and images on buttons, your regular button can't do that. Look at the 'samples' directory in your VS installation directory.
I want to add some ideas to #Amruta Ghodke 's answer:
You can resize your button using the GetWindowRect and SetWindowPos functions. See an example below:
CRect rc;
pButton->GetWindowRect(rc);
pButton->SetWindowPos(NULL, rc.left, rc.top, myWidth, myHeight, SWP_NOSENDCHANGING | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
If you want to display transparent images, use the software Pixelformer to convert your PNGs to Alpha-enabled BMPs. You will have to:
Go to Image->Properties and set RGB color with alpha channel
Export the file using format A8:R8:G8:B8 and disabled Premultiplied alpha and Top-down row order