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
Related
can you tell me how to make a picture fit in a static control, i mean like if you create a static control for viewing pictures and if the picture quality or size of picture is bigger than control then it re size the static control with the size of picture. i could create the control and set the picture to it alright. but i don't know how to make it fit on control. this is how i create control and set picture to it.
Code:
HWND static_con(HWND hWnd, HINSTANCE hInst){
HWND Static_Pic;
Profile_Pic = CreateWindow("STATIC", NULL, SS_BITMAP|WS_CHILD|WS_VISIBLE|WS_TABSTOP, 5,5,33,33, hWnd, NULL, hInst, NULL);
HBITMAP hBmp = (HBITMAP)LoadImage(NULL, "camera1.jpg", IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(hBmp == NULL){
MessageBox(NULL, "Error while loading image", "Error", MB_OK|MB_ICONERROR);
}
SendMessage(Static_Pic, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);
return 0;
}
and then i call the function in WM_CREATE handler which creates it successfully and now i don't know how to make it fit on control, i really appreciate if you could tell me how to make the picture fit on control.
You can use SS_REALSIZECONTROL
From Microsoft's documentation.
SS_REALSIZECONTROL - Adjusts the bitmap to fit the size of the static control.
You can also manually scale the image. Get the size of the control where image is to go by using GetWindowRect(), then by using StretchBlt() scale the image so that its dimensions match that of the source, then do STM_SETIMAGE.
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
This question already has an answer here:
Is there a Windows API for adding badges to taskbar icons?
(1 answer)
Closed 9 years ago.
I have a C++ win32 program, and I'd like to edit the taskbar icon at runtime to display alerts, etc about the program, however I'm not too experienced with the win32 api, and I haven't been able to find anything online. The closest I've found is http://www.windows-tech.info/17/52a5bfc45dac0ade.php which tells how to load the icon off the disc at runtime and change it.
I would like to do what they do in this question: Create an icon in memory with win32 in python but in C++ and without an external library.
I would like to do what they do in this question: Create an icon in memory with win32 in python but in C++ and without an external library
Since the accepted answer uses the wxWidgets library, which is just a wrapper of the Win32 API, the solution translates pretty nicely.
All you need to do is create a bitmap in memory using the CreateCompatibleBitmap function. Then you can draw into that bitmap using the standard GDI drawing functions. Finally, you create the icon using the CreateIconIndirect function.
The hardest part is keeping track of your resources and making sure that you free them all when you're finished to prevent memory leaks. It's way better if it's all wrapped up in a library that makes use of RAII to ensure the objects are properly freed, but if you're writing C code in C++, it would look like this:
HICON CreateSolidColorIcon(COLORREF iconColor, int width, int height)
{
// Obtain a handle to the screen device context.
HDC hdcScreen = GetDC(NULL);
// Create a memory device context, which we will draw into.
HDC hdcMem = CreateCompatibleDC(hdcScreen);
// Create the bitmap, and select it into the device context for drawing.
HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen, width, height);
HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmp);
// Draw your icon.
//
// For this simple example, we're just drawing a solid color rectangle
// in the specified color with the specified dimensions.
HPEN hpen = CreatePen(PS_SOLID, 1, iconColor);
HPEN hpenOld = (HPEN)SelectObject(hdcMem, hpen);
HBRUSH hbrush = CreateSolidBrush(iconColor);
HBRUSH hbrushOld = (HBRUSH)SelectObject(hdcMem, hbrush);
Rectangle(hdcMem, 0, 0, width, height);
SelectObject(hdcMem, hbrushOld);
SelectObject(hdcMem, hpenOld);
DeleteObject(hbrush);
DeleteObject(hpen);
// Create an icon from the bitmap.
//
// Icons require masks to indicate transparent and opaque areas. Since this
// simple example has no transparent areas, we use a fully opaque mask.
HBITMAP hbmpMask = CreateCompatibleBitmap(hdcScreen, width, height);
ICONINFO ii;
ii.fIcon = TRUE;
ii.hbmMask = hbmpMask;
ii.hbmColor = hbmp;
HICON hIcon = CreateIconIndirect(&ii);
DeleteObject(hbmpMask);
// Clean-up.
SelectObject(hdcMem, hbmpOld);
DeleteObject(hbmp);
DeleteDC(hdcMem);
ReleaseDC(NULL, hdcScreen);
// Return the icon.
return hIcon;
}
Adding the error checking and the code to draw something interesting onto the bitmap is left as an exercise for the reader.
As I said in a comment above, once you have created the icon, you can set the icon for a window by sending it a WM_SETICON message and passing the HICON as the LPARAM:
SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
You can also specify ICON_SMALL in order to set the window's small icon. If you set only a big icon, it will be scaled down to create the small icon automatically. However, if you set only the small icon, the window will continue to use the default icon as its big icon. Big icons typically have a dimension of 32x32, while small icons typically have a dimension of 16x16. This is not, however, guaranteed, so do not hardcode these values. If you need to determine them, call the GetSystemMetrics function with SM_CXICON and SM_CYICON to retrieve the width and height of big icons, or SM_CXSMICON and SM_CYSMICON to retrieve the width and height of small icons.
A fairly good tutorial on drawing in Windows using GDI is available here. I recommend reading it thoroughly if this is your first time doing this and have no prior experience with GDI.
I'm trying to create transparency in my application.
For instance the window of my app is square and i want to make it round, by hiding parts of the window.
My code looks something like this:
HDC hdcMask = nullptr;
HBITMAP hBMP = (HBITMAP)LoadImageW(nullptr, L"C:\\mask.bmp", IMAGE_BITMAP, 150, 160, LR_LOADFROMFILE);
SelectObject(hdcMask, hBMP);
HWND hWnd = GetActiveWindow();
HDC hdcWindow = GetDC(hWnd);
TransparentBlt(hdcWindow, 0, 0, 150, 160, hdcWindow, 0, 0, 150, 160, RGB(0,0,0));
where mask.bmp is a bitmap where white is what i want to be transparent and black is what I want to be visible.
After applying this code, nothings happens. What am I doing wrong ? Is there another method to obtain the desired result?
Note: I need this code to work on Windows XP OS or later.
There are several ways of making a window transparent and/or translucent.
SetWindowRgn will make parts of a window transparent.
SetLayeredWindowAttributes can make parts of a window transparent, and can also apply translucency to the whole of the rest of the window.
UpdateLayeredWindow can give individual windows different amounts of translucency.
Why don't you use a different format than bmp where you can also include alpha data?
You can use a PixelFormat32bppPARGB Bitmap, use Bitmap::LockBits an Bitmap::Unlockbits
to keep the file's format and avoid having the alpha setting overwritten.
I need to draw a system-like cursor that I simply can control the position of.
In other words, I need to draw a transparent image that looks just like the system cursor and I need it to be rendered on top of all other windows.
I've tried multiple approaches, but they all seem to have some downside.
I've figured out that I can load the cursor image by using LoadImage() and passing the resource OCR_NORMAL and casting it into a HBITMAP.
HICON NormalCursor = (HICON)LoadImage(NULL, MAKEINTRESOURCE(OCR_NORMAL), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
Then getting the "desktop" HDC
hDC = GetDC(NULL);
Then I can try to draw it using DrawIconEx()
DrawIconEx(hDC, (int)x, 0, NormalCursor, 0, 0, NULL, NULL, DI_DEFAULTSIZE | DI_NORMAL);
The DI_NORMAL flag is supposed to combine the DI_IMAGE & DI_MASK flags giving me a transparent image/icon/cursor, but this is my result on the desktop:
Not to mention that if it moves it creates trails.
By making a transparent window using SetLayeredWindowAttributes like this:
SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), 50, LWA_COLORKEY);
And having the background color of my window to be black, I can remove the background from the window. But due to doing alpha based on a color I get ugly black pixels around my cursor icon.
Can I make the background of a window transparent in some other way than using a color mask?
How do I draw a transparent cursor on top of all windows properly?
I would recommend that you do make your own window, and do something like what's described at http://www.codeproject.com/KB/GDI-plus/CsTranspTutorial3.aspx . It's in C#, but most of it is just win32 calls. It does a nice job of variable transparency, too, not just 0%/100%.
Isn't the outline of the cursor black? Is the problem just that you're making the outline transparent too? Why don't you just change the transparency color (and the background color of the window) to anything other than black or white?