How to get file icon using C++ - c++

I want to add Icon to treeview node, using C++. I want to get the icons from system, I tried
I tried with,
PMString ucPath("C:\\path\\to\\file.extension");
SHFILEINFO info;
::SHGetFileInfo(ucPath.GrabTString(), FILE_ATTRIBUTE_NORMAL, &info, sizeof(info),
SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON);
iconView->SetRsrcID((RsrcID) info.hIcon);
::DestroyIcon(info.hIcon);
where, SetResrcID ,PMString are the InDesing API and iconView is the controlView of the Tree, I am not getting what's going wrong, if anyone has idea please suggest.
Thanks,
Praveen Mamdge

Here is the codes what I'm using in my application, you should change the icon to a bitmap.
PMString ucPath("C:\\path\\to\\file.extension");
SHFILEINFO info;
::SHGetFileInfo(ucPath.GrabTString(), FILE_ATTRIBUTE_NORMAL, &info, sizeof(info),
SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON);
ICONINFO stIconInfo;
GetIconInfo(s_sfi.hIcon, &stIconInfo);
HBITMAP hBmp = stIconInfo.hbmColor;
DestroyIcon(s_sfi.hIcon) ;
The best way to do it is using the system icon index with SHGFI_SYSICONINDEX.

Some thing like this,
Extract icon from file first.
SHFILEINFO stFileInfo;
SHGetFileInfo( file,
FILE_ATTRIBUTE_NORMAL,
&stFileInfo,
sizeof( stFileInfo ),
SHGFI_ICON | SHGFI_LARGEICON );
Then add to imagelist and use the index to set icon.
m_nIndex = m_ilLargeIcons.Add( stFileInfo.hIcon );

This is your code snippet, observe line by line:
PMString ucPath("C:\path\to\file.extension"); SHFILEINFO info;
::SHGetFileInfo(ucPath.GrabTString(), FILE_ATTRIBUTE_NORMAL, &info, sizeof(info),
SHGFI_ICON |
SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON);
iconView->SetRsrcID((RsrcID) info.hIcon);
::DestroyIcon(info.hIcon);
After this line: iconView->SetRsrcID((RsrcID) info.hIcon);, you called ::DestroyIcon that destroyed that icon you stored.

Related

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);

Selecting and Highlighting an Item from List View

I would like to select and highlight an item from a list view control and am using the following code
#include <Windows.h>
#include <commctrl.h>
int main() {
//Hardcoded Handle to the ListView Windows of Add Printer Dialog
HWND hwndListView = (HWND)0x000206D6;
DWORD dwProcessID;
::GetWindowThreadProcessId( hwndListView, &dwProcessID );
HANDLE process=OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE|PROCESS_QUERY_INFORMATION, FALSE, dwProcessID);
LVITEM lvi;
LVITEM* _lvi=(LVITEM*)VirtualAllocEx(process, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE);
lvi.state = LVIS_FOCUSED | LVIS_SELECTED ;
lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED ;
lvi.mask = LVIF_STATE;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
::SendMessage(hwndListView, LVM_SETITEMSTATE, (WPARAM)0, (LPARAM)_lvi);
VirtualFreeEx(process, _lvi, 0, MEM_RELEASE);
}
The result I am getting is
instead of the item getting selected and highlighted
Please let me know what might be going wrong
There are restrictions on which processes can set focus on a window, and chances are that the app selecting the ListView items does not satisfy those restrictions while the dialog is active. For example, the HWND being focused must be attached to the calling thread's message queue. So the highlighting app will have to use AttachThreadInput() before calling SetFocus() on another app's windows.

How to print bold string in C++?

I got an old application which was written in a C++. I have 0 experience with it but I am suppose to make some changes in app. One of them is to change some text. Problem is that part of updated text needs to be bold, but i have no idea how to do that. I googled but with no much success. Only think I now is to go to new line with \nand new tab with \t.
Any clever advise?
EDIT:
Example of code:
BEGIN
STRING1 "First Example"
STRING2 "Second Example"
And place where STRING1 is used:
// WelcomeTip ---------------------------------------------//
LPSTR idsWelcomeTip = (LPSTR)GlobalAlloc(GPTR, sizeof(CHAR) * 4098 );
LoadString( waveInDlg->hInstance, STRING1, idsWelcomeTip, 4098 );
waveInDlg->hwndWelcomeTip = CreateWindow(
"STATIC",
idsWelcomeTip,
WS_CHILD | WS_VISIBLE | SS_LEFT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
waveInDlg->hwnd,
NULL,
waveInDlg->hInstance,
NULL
);
SetWindowLongPtr(waveInDlg->hwndWelcomeTip, GWLP_USERDATA ,(LONG)waveInDlg );
SendMessage(waveInDlg->hwndWelcomeTip, WM_SETFONT , (WPARAM)waveInDlg->hFontDefault , TRUE );
ShowWindow(waveInDlg->hwndWelcomeTip, SW_HIDE);
GlobalFree( (HGLOBAL)idsWelcomeTip );
Thanks,
Ile
There is no concept of bold text in C++, there may be in a particular device that displays character text, for example rich-text-format or HTML tagging or a terminal screen. The latter usually involves sending some "escape sequence" relevant to that particular terminal.
OK, I've knocked up some code that should give an overview of what you're after, I've not managed to compile it as I'd need to write a lot more to test, but it should point you in the right direction:
// Create the font you need
LOGFONT lf;
zeromemory(&lf, sizeof(LOGFONT))
lf.lfHeight = 20; // 20 pixel high font
lf.lfWeight = FW_BOLD;
strcpy(lf.lfFaceName, "Arial");
HFONT hFont = ::CreateFondIndirect(&lf);
// Set the control to use this font
SendMessage(waveInDlg->hwndWelcomeTip, WM_SETFONT, (WPARAM)hFont, NULL);
I hope this helps.
Please go through the below link for help
http://msdn.microsoft.com/en-us/library/dd162499(VS.85).aspx
Yes, you have to override WM_PAINT in your dialog class and call drawtext function.
Use DrwaText API in WM_PAINT message handler.dc.DrawText (_T ("Hello, MFC"), -1, &rect,
DT_SINGLELINE ¦ DT_CENTER ¦ DT_VCENTER);
use DrawTextEx method.
For more inforamtion go through the follwoing link
ms-help://MS.MSDNQTR.v90.en/gdi/fontext_4pbs.htm

How to get a folder's open icon with SHGetFileInfo() method?

This is what I did:
LPMALLOC malloc;
LPITEMIDLIST pidl;
SHFILEINFO FileInfo;
SFGAOF sfGao;
if (SUCCEEDED(SHGetMalloc(&malloc))
{
if (SUCCEEDED(SHParseDisplayName(strDirPath, NULL, &pidl, SFGAO_FOLDER, &sfGao)))
{
SHGetFileInfo((LPCWSTR)(PCHAR(pidl)), 0, &FileInfo, sizeof(FileInfo), SHGFI_PIDL | SHGFI_ICON);
CDC* pDC = GetWindowDC();
pDC->DrawIcon(10, 10, FileInfo.hIcon);
ReleaseDC(pDC);
}
malloc->Free(pidl);
}
malloc->Release();
Here's the problem: I found that I can get the icon of a folder easily with this approach. But I could not get its open icon, when I set the fourth parameter of SHGetFileInfo method to be SHGFI_PIDL | SHGFI_OPENICON. The hIcon of FileInfo is always NULL, and I don't know why.
Can anyone tell me how to fix the problem?
From the comments:
I have resolved the problem. Modify the fourth parameter from SHGFI_PIDL | SHGFI_OPENICON to SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_ICON | SHGFI_OPENICON, then I was able to retrieve the open icon of the specified folder.

Get icons for common file types

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