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);
Related
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.
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);
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);
Quick background: I have a TreeView which I have created using Windows API calls in C++ (Visual Studio 2008, though that shouldn't make a difference):
hTreeview = CreateWindowEx(0, WC_TREEVIEW, L"My Treeview", WS_CHILD | WS_VISIBLE | TVS_INFOTIP | TVS_NOHSCROLL | TVS_SHOWSELALWAYS | TVS_EDITLABELS | TVS_SINGLEEXPAND, m_tx, m_ty, m_tw, m_th, hWindow, (HMENU)2, hInstance, NULL);
I have successfully assigned an ImageList to it using:
m_hImageList = ImageList_Create(cx, cy, ILC_COLOR24, n, n);
TreeView_SetImageList(hTreeview, m_hImageList, TVSIL_NORMAL);
where cx, cy, and n are all specified (in this case, 18, 18, and 5, respectively). This all works fine, as I can see because now there is space set aside next to my items for the image.
What I am trying to accomplish is to then copy a subsection of another bitmap (from a file). The code that I have tried (but does not work) is this:
HBITMAP hSkin = (HBITMAP)LoadImage(NULL, szPathBmp, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
for (long i = 0; i < n; i++)
{
HDC dcDest = CreateCompatibleDC(NULL);
HBITMAP hIcon = CreateCompatibleBitmap(dcDest, cx, cy);
HDC dcSrc = CreateCompatibleDC(NULL);
SelectObject(dcSrc, hSkin);
BitBlt(dcDest, 0, 0, cx, cy, dcSrc, x, y, SRCCOPY);
*pIcon = ImageList_Add(m_hImageList, hIcon, NULL);
DeleteObject(hIcon);
DeleteDC(dcSrc);
DeleteDC(dcDest);
}
DeleteObject(hSkin);
I have left out the error checking code for brevity, and it can be assumed that all of the listed variables have been set somewhere else in the program (forgive me for not providing a working source file, but this is a very large project and I have tried to include only the parts that are relevant).
All I get in the Treeview is black squares (which happens to be the background color of the Treeview), so I am assuming that something is going wrong with the last block of code--the one that loads the skin and tries to BitBlt a portion of it into a new bitmap to save to the ImageList. Can anyone either tell me what I'm doing wrong, or tell me a better way of accomplishing what I'm trying to do?
I am using C++ and the Windows API exclusively, no .NET, MFC, or Windows Form Designer.
Thanks in advance for your help, and if I've left anything out, I apologize; this is one of my first posts.
There are a few problems with your code.
Firstly, when you create a new DC it starts out with a monochrome bitmap in it, so your CreateCompatibleBitmap call will also produce a monochrome one. Instead, you probably want to create the bitmap based on the window or screen DC.
Secondly, you never actually select the bitmap into dcDest, so nothing will be drawn into hIcon anyway.
Thirdly, ImageList_Add will fail if the bitmap is currently selected into a device context, so you have to deselect hIcon from dcDest before you add the icon to the image list.
Lastly, you are also neglecting to save the original bitmaps and restore them, so this will also cause a GDI leak.
Try something like this:
HDC hdcWindow = GetDC(hWnd);
HDC dcDest = CreateCompatibleDC(hDCWindow);
HBITMAP hIcon = CreateCompatibleBitmap(hDCWindow, cx, cy);
HDC dcSrc = CreateCompatibleDC(NULL);
HGDIOBJ hOldSourceBmp = SelectObject(dcSrc, hSkin);
HGDIOBJ hOldDestBmp = SelectObject(dcDest, hIcon);
BitBlt(dcDest, 0, 0, cx, cy, dcSrc, x, y, SRCCOPY);
SelectObject(dcDest, hOldDestBmp);
SelectObject(dcSrc, hOldSourceBmp);
*pIcon = ImageList_Add(m_hImageList, hIcon, NULL);
DeleteObject(hIcon);
DeleteDC(dcSrc);
DeleteDC(dcDest);
ReleaseDC(hWnd, hDCWindow);
I want to get the icons of common file types in my dll. I am using vc++. I only have the file extension and mime type of the file based on which I want to get the icon for the file.
Can someone please tell me how I can do that? (The method available in vc++ needs the user to give the path of the file for which the icon is needed. I do not have access to any such file)
Thanks.
Shell API
You can get them from the shell by calling SHGetFileInfo() along with the SHGFI_USEFILEATTRIBUTES flag - this flag allows the routine to work without requiring the filename passed in to actually exist, so if you have a file extension just make up a filename, append the extension, and pass it in.
By combining other flags, you'll be able to retrieve:
A large or small icon as determined by the system configuration: SHGFI_ICON|SHGFI_LARGEICON or SHGFI_ICON|SHGFI_SMALLICON
A large or small icon as determined by the shell configuration: SHGFI_ICON|SHGFI_LARGEICON|SHGFI_SHELLICONSIZE or SHGFI_ICON|SHGFI_SMALLICON|SHGFI_SHELLICONSIZE
The index of the icon in the shell's image list along with the appropriate image list: SHGFI_SYSICONINDEX
The path and filename of the actual module where the icon is stored (along with the icon index in that module): SHGFI_ICONLOCATION
Examples
// Load a System Large icon image
SHGetFileInfo( szFileName, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(SHFILEINFO),
SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_LARGEICON);
// Load a System Small icon image
SHGetFileInfo( szFileName, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(SHFILEINFO),
SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_SMALLICON);
// Load a Shell Large icon image
SHGetFileInfo( szFileName, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(SHFILEINFO),
SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_SHELLICONSIZE);
// Load a Shell Small icon image
SHGetFileInfo( szFileName, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(SHFILEINFO),
SHGFI_USEFILEATTRIBUTES
| SHGFI_ICON | SHGFI_SHELLICONSIZE | SHGFI_SMALLICON);
If you want to draw such an icon, use something like this:
// Draw it at its native size
DrawIconEx( hDC, nLeft, nTop, hIcon, 0, 0, 0, NULL, DI_NORMAL );
// Draw it at the System Large size
DrawIconEx( hDC, nLeft, nTop, hIcon, 0, 0, 0,
NULL, DI_DEFAULTSIZE | DI_NORMAL );
// Draw it at some other size (40x40 in this example)
DrawIconEx( hDC, nLeft, nTop, hIcon, 40, 40, 0, NULL, DI_NORMAL );
The icon handle as well as the file system path can be obtained from the SHFILEINFO structure:
typedef struct _SHFILEINFOA
{
HICON hIcon; // out: icon
int iIcon; // out: icon index
DWORD dwAttributes; // out: SFGAO_ flags
CHAR szDisplayName[MAX_PATH]; // out: display name (or path)
CHAR szTypeName[80]; // out: type name
} SHFILEINFOA;
Keep in mind that you must free the obtained icon by passing hIcon to DestroyIcon() after you're done with it.
Identify the icon information from the registry, the associate file type and program that handles the file and extract the icon from the file.
http://www.codeproject.com/KB/shell/iconextract.aspx