I cannot load image from folder using win32 - c++

I am new to win32. I placed the bitmap image 'logo2.bmp' into the directory which .vcxproj is there. However, when I used the LoadImage() function, it seems that nothing is loaded.
I have checked my questions online, but the image still cannot be loaded after the modification.
void AddControls(HWND hwnd)
{
//some code
hLogo = CreateWindowW(L"Static", NULL, WS_VISIBLE | WS_CHILD | SS_BITMAP,
350, 60, 100, 100, hwnd, NULL, NULL, NULL);
if (hLogoImage != NULL)
{
SendMessage(hLogo, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hLogoImage);
}
else
{
MessageBox(0, L"Could not load image", L"Error", MB_OK);
}
}
void loadImages()
{
hLogoImage = (HBITMAP)LoadImage(NULL, L"logo2.bmp", IMAGE_BITMAP,
0,0,LR_LOADFROMFILE | LR_DEFAULTSIZE);
}
I expect the output is a bitmap image successfully displayed on the windows, but the 'Error' text box was displayed and no picture displayed instead.

Edit: Okay, I understand that this is a problem of file format. I better study more.

Related

How can I get icon of Windows app in CPP?

I'm trying to build an File Manager with in Win32, and I have a problem with the icons. Whenever I trying to get icon that an windows 10 app is associated with it like .png (Photos app), the icon is blank paper. What I'm doing wrong? Thanks in advance and please answer :)
BOOL InitTreeViewImageLists(HWND hwndTV) {
HIMAGELIST himl; // handle to image list
HBITMAP hbmp; // handle to bitmap
// Create the image list.
if ((himl = ImageList_Create(16,
16,
ILC_COLOR16 | ILC_MASK,
3, 0)) == NULL)
return FALSE;
// Add the open file, closed file, and document bitmaps.
HICON hIcon;
SHFILEINFO sfi;
LPCWSTR path = L"C:\\Users\\Shalev\\Desktop\\WhatsApp.lnk";
sfi = GetShellInfo(path);
HICON* iconHandles = new HICON;
hIcon = sfi.hIcon;
cout << hIcon << endl;
g_nOpen = sfi.iIcon;
ImageList_AddIcon(himl, hIcon);
sfi = GetShellInfo(L"C:\\");
hIcon = sfi.hIcon;
g_nClosed = sfi.iIcon;
ImageList_AddIcon(himl, hIcon);
sfi = GetShellInfo(L"C:\\");
hIcon = sfi.hIcon;
g_nDocument = sfi.iIcon;
ImageList_AddIcon(himl, hIcon);
// Associate the image list with the tree-view control.
TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);
return TRUE;
}
SHFILEINFO GetShellInfo(LPCWSTR path) {
SHFILEINFO sfi;
SecureZeroMemory(&sfi, sizeof(sfi));
SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | SHGFI_SHELLICONSIZE | SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES);
return sfi;
}
You can use the IShellItemImageFactory interface, something like this:
...
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); // need this somewhere when your thread begins, not for every call
...
IShellItemImageFactory* factory;
if (SUCCEEDED(SHCreateItemFromParsingName(path, nullptr, IID_PPV_ARGS(&factory))))
{
// the GetImage method defines a required size and multiple flags
// with which you can specify you want the icon only or the thumbnail, etc.
HBITMAP bmp;
if (SUCCEEDED(factory->GetImage(SIZE{ 256, 256 }, SIIGBF_ICONONLY, &bmp)))
{
... // do something with the HBITMAP
DeleteObject(bmp);
}
factory->Release();
}
...
CoUninitialize(); // call this each time you successfully called CoInitializeEx

How to add files to build and read them in Code Blocks?

I want to write only-exe application and I must load an image. What I have to do?
Project Manager: https://dawidsk.gamejolt.io/images/ScreenShot_20190727141153.png
(Sorry for bad English)
Loading image code:
void AddControls(HWND hWnd)
{
hWarning = CreateWindowW(L"Static", NULL, WS_VISIBLE | WS_CHILD | SS_BITMAP, 0, 0, windowHeight, windowWidth, hWnd, NULL, NULL, NULL);
SendMessageW(hWarning, STM_SETIMAGE, IMAGE_BITMAP,(LPARAM) hWarningImage);
}
void LoadImages()
{
hWarningImage = (HBITMAP)LoadImageW(NULL, L"warning.bmp", IMAGE_BITMAP, windowHeight, windowWidth, LR_LOADFROMFILE);
}
When I remove warning.bmp from root folder of exe, it's not displaying it.
EDIT:
Now I know that I must use resource.rc. How can I load image from it?
You can follow this document to load it:
https://learn.microsoft.com/en-us/windows/win32/menurc/bitmap-resource
syntax:
nameID BITMAP "warning.bmp"
EDIT:
After this, you could load the bitmap with the instance.
define your nameID with a value, such as
#define nameID 40001
Then loadimage with nameID and instance handle, remove the LR_LOADFROMFILE:
LoadImageW(hInstance,MAKEINTRESOURCE(nameID),IMAGE_BITMAP, windowHeight, windowWidth, 0);

Is it possible to change font for an edit control without affecting the other lines?

Hello I want to know if it is possible to change the font of an edit control for some lines only without affecting the remaining:
In my Edit control I have a text but I want some headlines and titles in bigger font and bold while the other lines are with smaller font.
I tried SendMessage(hEdit, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(0, true));
But it sets the whole text in the passed in font.
I thought some messing up with SelectObject(hDcEdit, hFont); But I don't know if it is correct and how.
A standard Edit Control (think, Notepad) does not support what you are looking for. It only supports one Font for the entire text.
What you are looking for is a RichEdit Control instead (think, Wordpad), and in particular its EM_SETCHARFORMAT message, which can be used to apply different formatting (including fonts, colors, etc) to different sections of text.
This is not working with the default Editcontrol, but you can use a Richeditcontrol
#include <Windows.h>
#include <CommCtrl.h>
HINSTANCE relib = LoadLibrary("riched32.dll");
if (relib == NULL) {
MessageBox(NULL, "couldn't load richedit32.dll", "", MB_ICONEXCLAMATION);
hEdit = CreateWindow(RICHEDIT_CLASS, "", WS_VISIBLE | WS_CHILD | ES_MULTILINE |
ES_AUTOHSCROLL | ES_AUTOVSCROLL | WS_VSCROLL | WS_HSCROLL, 0, 0, 200, 200, hWnd, NULL,
NULL, NULL);
Now to set the font to your Richeditcontrol use:
CHARFORMAT2 cf;
memset(&cf, 0, sizeof cf);
cf.cbSize = sizeof cf;
cf.dwMask = CFM_FACE;
wsprintf(cf.szFaceName, "Arial"); //Here you can set the fontname you wont (C:/Windows/Fonts)
SendMessage(hEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);

SelectObject returns NULL with hbitmap created in constructor

I have a bitmap class that has a load function for loading the bitmap from either file path or resource ID. This part works fine.
void GtBitmap::Load()
{
LPTSTR szFileName;
szFileName = (LPTSTR)m_strPath.c_str();
// Check for valid .BMP file path
if (m_strPath.size() > 0)
{
// Open .BMP file
m_pFile = fopen(m_strPath.c_str(), ("rb"));
if (m_pFile != NULL)
{
m_hBitmap = (HBITMAP)LoadImage (GetModuleHandle(NULL), szFileName, IMAGE_BITMAP, 0, 0, LR_SHARED | LR_LOADFROMFILE);
GetObject(m_hBitmap, sizeof(m_bmap), &m_bmap);
int i = 1;
}
}
else if (m_intResourceID != 0)
{
m_hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(m_intResourceID), IMAGE_BITMAP, 0, 0, LR_SHARED);
GetObject(m_hBitmap, sizeof(m_bmap), &m_bmap);
int i = 1;
}
}
However, when I try to render it in my code block, the SelectObject returns null. Here is the code for that section of the painter class.
void GtPainterGDI::GtDrawBitmap(GtRectI & target, GtBitmap & bitmap, bool blnOffset)
{
GtCanvas topCv = m_arrCanvas.back();
HDC hdcMem = CreateCompatibleDC(topCv.m_hdcParent);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, bitmap.m_hBitmap);
DWORD lastError = GetLastError();
bool success = BitBlt(hdcMem, target.GetLeft(), target.GetTop(),
target.Width(), target.Height(), hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, bitmap.m_hBitmap);
DeleteDC(hdcMem);
};
The SelectObject() returns null and the image is not drawn. I can only get the image to show up if I use a LoadImage() in that paint function. However I don't want to load the image every time I want to paint. I should be able to load the image once in the Load function or constructor of the bitmap, then use the handle in the paint function.
If anyone could please provide an example of loading an image in a constructor and then painting it elsewhere in the codes WM_PAINT or equivalent painting function I would appreciate it. The code is a new version of the GT graphical user interface library. I plan on posting a new version on codeproject in the next few days or so. I have to do some cleanup first...
Thanks in advance.
HINSTANCE parameter in LoadImage should be NULL when loading the image from file. Use GetModuleHandle(NULL) only when loading from resource.
m_hBitmap = (HBITMAP)LoadImage(NULL, m_strPath.c_str(),
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (!m_hBitmap)
{
//report error
}
Also LR_SHARED is not necessary here.
When testing for file's exist, you can use std::ifstream. Example:
#include <fstream>
...
bool test = std::ifstream(m_strPath).good();
This will test for file and close the file handle right away.
Make sure to select hbmOld before deleting hdcMem:
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, bitmap.m_hBitmap);
BitBlt(...)
//SelectObject(hdcMem, bitmap.m_hBitmap); <<= remove this
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);

Win32 LoadImage() from File Error

Sorry if this is duplicate but I can't find answer elsewhere. I am simply trying to load an image during runtime using the LoadImage() function of WINAPI. I receive the error code(8) which indicates that there is not enough storage space(error codes found here).
the file is relatively small(2.5kb) so I wonder if there is problem with my code:
void OnCreate()
{
...
HBITMAP hbmDeck = (HBITMAP)LoadImage(hInstance, L"standard.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE);
DWORD err = GetLastError();
HBITMAP hbmT = SelectBitmap(hdc, hbmDeck);
if(!hbmT)
{
MessageBox(NULL, L"Failed to LoadImage - 'hbmDeck'", L"OnCreate()", MB_OK);
PostMessage(hwnd, WM_DESTROY, NULL, NULL);
}
...
}
Yeah, some of the API return error codes are a bit cryptic and don't fit the error.
The file you are trying to load "standard.bmp" is a file on the disk NOT in the resource section right? Well to load a file from the disk, the fist parameter of LoadImage (hInst) has to be NULL and the fuLoad flag needs to include LR_LOADFROMFILE which you correctly have.