Using the code below results in that sometimes an icon remains in a tray right after call to removeIconFromTray method and disappears only after a user moves over an icon in tray.
void CMyDlg::addIconToTray()
{
static HICON hIcon = ::LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
NOTIFYICONDATA data;
data.cbSize = sizeof(data);
data.hIcon = hIcon;
data.hWnd = m_hWnd;
strcpy (data.szTip, m_sTrayIconTip.c_str());
data.uFlags = NIF_ICON | NIF_TIP;
data.uID = (UINT)this;
Shell_NotifyIcon (NIM_ADD, &data);
}
void CMyDlg::removeIconFromTray()
{
NOTIFYICONDATA data;
data.cbSize = sizeof(data);
data.hWnd = m_hWnd;
data.uID = (UINT)this;
Shell_NotifyIcon (NIM_DELETE, &data);
}
Whats wrong in this code and how to achieve that an icon disappears from a tray as soon as a code deleting it form there finished working?
One obvious problem is that you are failing to initialize your struct. You should do this:
NOTIFYICONDATA data = { 0 };
Other than that check for errors and call GetLastError to find out what caused any error.
According to MSDN:
Shell_NotifyIcon function
Deletes an icon from the status area. NOTIFYICONDATA structure pointed to by lpdata uses the ID originally assigned to the icon when it was added to the notification area (NIM_ADD) to identify the icon to be deleted.
So, you should pass the same data of NOTIFYICONDATA to Shell_NotifyIcon function.
void CMyDlg::addIconToTray()
{
static HICON hIcon = ::LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
NOTIFYICONDATA data;
data.cbSize = sizeof(data);
data.hIcon = hIcon;
data.hWnd = m_hWnd;
strcpy (data.szTip, m_sTrayIconTip.c_str());
data.uFlags = NIF_ICON | NIF_TIP;
data.uID = (UINT)this;
Shell_NotifyIcon (NIM_ADD, &data);
}
void CMyDlg::removeIconFromTray()
{
NOTIFYICONDATA data;
data.cbSize = sizeof(data);
data.hIcon = hIcon;
data.hWnd = m_hWnd;
strcpy (data.szTip, m_sTrayIconTip.c_str());
data.uFlags = NIF_ICON | NIF_TIP;
data.uID = (UINT)this;
Shell_NotifyIcon (NIM_DELETE, &data);
}
This will work properly.
Or, save the data to a member variable.
As DavidHeffernan said, you should zero-initialize your data struct (you should ALWAYS zero-init any struct that you pass to a Win32 API function), eg:
NOTIFYICONDATA data = {0};
Or:
NOTIFYICONDATA data;
ZeroMemory(&data, sizeof(data));
This way, any unused fields have consistent and predictable values. In your case, when calling NIM_DELETE, you are not initializing data, so its uFlags field will have random bits, which is likely to cause Shell_NotifyIcon() to misinterpret the content of your NOTIFYICONDATA and fail, and thus your icon is not removed.
Related
I use TaskDialogIndirect() to display more advanced Error Messages. I can customize the buttons, icons, and more.
The problem is that, sometimes it makes these invisible empty dialog boxes.
I need it to be reliable. I am wondering why this is even happening in the first place.
Example of it failing (there is no visible window):
Here is the code that makes the dialogs (not production code):
int MessageBoxPosL(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType, int X, int Y)
{
TaskDialogData data;
data.X = X;
data.Y = Y;
TASKDIALOGCONFIG config = {};
config.cbSize = sizeof(config);
config.hwndParent = hWnd;
config.pszWindowTitle = lpCaption;
config.pszContent = lpText;
// configure other settings as desired, based on uType...
config.pfCallback = &TaskDialogCallback;
config.lpCallbackData = (LONG_PTR)&data;
config.dwFlags = TDF_ENABLE_HYPERLINKS;
config.hFooterIcon = LoadIcon(NULL, IDI_ERROR);
config.dwCommonButtons = ButtonActive(TDCBF_YES_BUTTON) | ButtonActive(TDCBF_NO_BUTTON) | ButtonActive(TDCBF_OK_BUTTON) | ButtonActive(TDCBF_RETRY_BUTTON) | ButtonActive(TDCBF_CLOSE_BUTTON);
config.pszMainIcon = SetIcon();
int button = 0;
TaskDialogIndirect(&config, &button, NULL, NULL);
return button;
}
The problem was the flag TDF_ENABLE_HYPERLINKS. Adding hyperlinked text that is too long caused the dialog to spawn outside of the desktop view.
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
I've got a treeview listing files that are dropped upon it.
When I make a new treeview item, I'd like to store the address of the file as a string in that item, and retrieve it for various nefarious purposes at a later point in time.
Looking at the TVITEM structure in Microsoft docs, apparently LPARAM is the place to store a value:
lParam
Type: LPARAM
A value to associate with the item.
So, I have gone ahead and done that:
TVITEM tvi;
tvi.mask = TVIF_TEXT;
tvi.pszText = const_cast<char *> (str0.c_str());
tvi.cchTextMax = sizeof(tvi.pszText);
tvi.lParam = (LPARAM) foo; // SETTING LPARAM HERE, foo IS A const char *
TVINSERTSTRUCT tvis;
tvis.item = tvi;
tvis.hInsertAfter = 0;
tvis.hParent = hti0;
// Send message to update tree, and return tree item.
return TreeView_InsertItem(tvw_filelist_, &tvis);
Then, when I try to retrieve my value...
HTREEITEM htiSel = TreeView_GetSelection(tvw_filelist_);
TVITEM tvItem;
tvItem.hItem = htiSel;
TreeView_GetItem(tvw_filelist_, &tvItem);
const char * info = (const char *) tvItem.lParam;
MessageBox(NULL, info, "Alert", MB_OK);
...I just get garbage, indicating my pointer went out of scope or is taking a nap or something. The size of that pointer is always 4.
Is this the right way to do what I'm trying to do? If so, what's going on?
Of course, take the time to post a question after a long time trying to figure it out, and the answer shows up in seconds.
Turns out the TVITEM mask needs to include TVIF_PARAM, similar to this question.
If I change the above code to:
tvi.mask = TVIF_TEXT | TVIF_PARAM;
it works as expected.
I'm still not sure if this is the recommended use for LPARAM, though.
struct CustomTreeData
{
LPSTR str; // or even std::string to forget about memory managment
// TODO: any other data you need
};
...
TVITEM tvi;
tvi.mask = TVIF_TEXT | TVIF_PARAM;
CustomTreeData* myDataPtr = new CustomTreeData; // the memory should be free later
myDataPtr->str = stringWhatIWant; // And don't forget to alloc memory for str!
tvi.lParam = (LPARAM) myDataPtr;
I don't check this code, but it should work. Happy coding :)
I want to load an animated cursor stored in .ani format, which is described to be a RIFF archive/container, from memory without writing the memory to a temporary file. Thus far I am able to parse the .ani file structure and load the individual frames as a normal icon with the aid of CreateIconFromResourceEx LookupIconIdFromDirectoryEx
One of the problems that proofs difficult is the actual composition of these frames and the animation data (jiffy-rate etc) as there appears to be no entries in the Windows API to do so. Documentation or written knowledge over the subject seems to be limited to loading non-animated icons/cursors from memory.
Similar questions such as 'Load an embedded animated Cursor from the Resource' express the desire to load an animated cursor from an embeddable resource. However i am not able to reproduce a workable solution from that either. Partly because the resource compiler in visual studio 2008 & 2010 doesn't support .ani (only ico and cur) files and therefor embedding it simply results in a 1:1 copy of the bytes as they were in the original file as opposed to .cur and .ico files which get decomposed into multiple resources. The subsequent call to CreateIconFromResource as shown in both answers does not work because the data it expects is the icon/cursor data of a single directive in a icon archive and not a RIFF-based file structure.
To summarize I'm interested in any information regarding the structures of animated cursors (in memory) or otherwise relevant pointers that could assist me in pursuing my goal.
After re-evaluating loading an animated cursor from an embeddable resource as pointed out by mfc I have found a solution that allows me to load the cursors from an arbitrary memory address.
Data from an embeddable resource and data read from a file into memory are exactly identical. Therefor it suggests that CreateIconFromResource should work on normal regular memory as well. However there is one fundamental difference. Memory of the embeddable resources reside in special sections in the executable which are often padded to the nearest 4096-byte boundary. Memory allocated at runtime contains garbage values.
Now the solution that I found working exploits this by simply allocating a guard-band of zero-filled bytes. In my own test cases I have found that 8 is the minimum which also happens to be the size of a chunk in the riff container. Coincidence? What i suspect is that this is a bug and the algorithm happens to work for embeddable resources due to it's alignment restrictions within the dll/executable.
const int guardbandSize = 8;
FILE* fs = fopen("action.ani", "rb");
fseek(fs, 0,SEEK_END); int dwSize = ftell(fs); fseek(fs, 0,SEEK_SET);
char* memory = new char[dwSize + guardbandSize];
fread(memory, 1, dwSize, fs); memset(memory + dwSize, 0, guardbandSize);
fclose(fs);
cursor = (HCURSOR)CreateIconFromResource((PBYTE)memory,dwSize,FALSE,0x00030000);
delete memory;
Here is an overview of various ways to load an animated cursors.
#include <Windows.h>
#include <stdio.h>
#include "resource2.h"
void* hWnd;
bool visible = true;
bool running = true;
LRESULT CALLBACK WndProcInternal( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) ;
HCURSOR cursor = 0;
void main()
{
//Setup configuration
const int Width = 640, Height = 480;
const int Method = 4;
//Setup window class
WNDCLASS wcd;
wcd.style = CS_PARENTDC | CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcd.lpfnWndProc = (WNDPROC)WndProcInternal;
wcd.cbClsExtra = 0;
wcd.cbWndExtra = 0;
wcd.hInstance = GetModuleHandle(NULL);
wcd.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wcd.hCursor = LoadCursor(NULL, IDC_ARROW);
wcd.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
wcd.lpszMenuName = NULL;
wcd.lpszClassName = TEXT("AnimatedIcon");
//Register the window class
if(!RegisterClass(&wcd))
{
MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error!"),MB_ICONEXCLAMATION | MB_OK);
FatalExit(-1);
}
//Create a window
if (!(hWnd=CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("AnimatedIcon"), TEXT("AnimatedIcon"),
WS_VISIBLE | WS_CAPTION | WS_MINIMIZEBOX | WS_THICKFRAME | WS_MAXIMIZEBOX | WS_SYSMENU,
0, 0, Width, Height, NULL, NULL, NULL, NULL)))
{
MessageBoxA(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
FatalExit(-1);
}
if( Method == 1 )
{
//Method 1: Load cursor directly from a file
cursor = LoadCursorFromFileA("action.ani");
}
if( Method == 2 )
{
//Method 2: Load cursor from an resource section in the executable.
cursor = LoadCursor(0, MAKEINTRESOURCE(IDR_ANICURSORS1));
}
if( Method == 3 )
{
//Method 3: Manually locate the resource section in the executable & create the cursor from the memory.
HINSTANCE hInst=GetModuleHandle(0);
HRSRC hRes=FindResourceA(hInst,MAKEINTRESOURCEA(IDR_ANICURSORS1),"ANICURSORS");
DWORD dwSize=SizeofResource(hInst,hRes);
HGLOBAL hGlob=LoadResource(hInst,hRes);
LPBYTE pBytes=(LPBYTE)LockResource(hGlob);
cursor = (HCURSOR)CreateIconFromResource(pBytes,dwSize,FALSE,0x00030000);
}
if( Method == 4 )
{
//Method 4: Load the cursor from a file into memory & create the cursor from the memory.
const int guardbandSize = 8;
FILE* fs = fopen("action.ani", "rb");
fseek(fs, 0,SEEK_END); int dwSize = ftell(fs); fseek(fs, 0,SEEK_SET);
char* memory = new char[dwSize + guardbandSize];
fread(memory, 1, dwSize, fs); memset(memory + dwSize, 0, guardbandSize);
fclose(fs);
cursor = (HCURSOR)CreateIconFromResource((PBYTE)memory,dwSize,FALSE,0x00030000);
delete memory;
}
//Set the cursor for the window and display it.
SetClassLong((HWND)hWnd, GCL_HCURSOR, (LONG)cursor);
while (running)
{
MSG wmsg;
if (PeekMessage(&wmsg, (HWND)hWnd, 0, 0, PM_REMOVE))
{
TranslateMessage(&wmsg);
DispatchMessage(&wmsg);
}
}
}
LRESULT CALLBACK WndProcInternal( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if( uMsg == WM_DESTROY )
{
PostQuitMessage(1);
running = false;
}
return (long)DefWindowProc((HWND)hWnd,uMsg,(WPARAM)wParam,(LPARAM)lParam);
}
Import your animated cursor as a custom resource.
Give it a text name, eg "MyType".
Then load the cursor with:
HCURSOR hCursor = LoadAnimatedCursor(IDR_MYTYPE1, _T("MyType"));
/* ======================================================== */
HCURSOR LoadAnimatedCursor(UINT nID, LPCTSTR pszResouceType)
{
HCURSOR hCursor = NULL;
HINSTANCE hInstance = AfxGetInstanceHandle();
if (hInstance)
{ HRSRC hResource = FindResource(hInstance, MAKEINTRESOURCE(nID), pszResouceType);
DWORD dwResourceSize = SizeofResource(hInstance, hResource);
if (dwResourceSize>0)
{ HGLOBAL hRsrcGlobal = LoadResource(hInstance, hResource);
if (hRsrcGlobal)
{ LPBYTE pResource = (LPBYTE)LockResource(hRsrcGlobal);
if (pResource)
{ hCursor = (HCURSOR)CreateIconFromResource(pResource, dwResourceSize, FALSE, 0x00030000);
UnlockResource(pResource);
}
FreeResource(hRsrcGlobal);
}
}
}
return hCursor;
}
I'm writing add-on for Internet Explorer 9 and I have to change default context menu to my own. I'm writing a BHO in C++ and I'm using ATL. I managed to handle event of showing context menu (HTMLDocumentEvents2::oncontextmenu), but I can't display my own one. Here is the code fired when you click right mouse button:
VARIANT_BOOL STDMETHODCALLTYPE CSpellCheckerBHO::OnContextMenu( IHTMLEventObj *pEvtObj)
{
HMENU contextMenu = CreatePopupMenu();
MENUITEMINFO item_info = { 0 };
item_info.cbSize = sizeof(MENUITEMINFO);
item_info.fMask = MIIM_TYPE | MIIM_ID;
item_info.fType = MFT_STRING;
item_info.wID = 0;
item_info.dwTypeData = L"TEST";
item_info.cch = 4;
BOOL result = InsertMenuItem(contextMenu, 0, FALSE, &item_info);
HWND browserHandle = 0;
HRESULT hr = _webBrowser->get_HWND((LONG_PTR*)&browserHandle);
result = TrackPopupMenuEx(contextMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, 0,0, browserHandle , NULL);
return VARIANT_FALSE;
}
_webBrowser is a pointer to IWebBrowser2 object, I got it from SetSite function.
The standard context menu is not displayed (due to returning VARIANT_FALSE), but TrackPopupMenuEx does nothing and returns 0.
Do you know what I am doing wrong? I need simple menu with some text items.
I fgured it out. Igor Tandetnik helped me on IE addon forum. HWND was from different proccess and TrackPopupMenuEx expects HWND belonging to the calling thread. Here's the code that works:
VARIANT_BOOL STDMETHODCALLTYPE CSpellCheckerBHO::OnContextMenu( IHTMLEventObj *pEvtObj)
{
HMENU contextMenu = CreatePopupMenu();
MENUITEMINFO item_info = { 0 };
item_info.cbSize = sizeof(MENUITEMINFO);
item_info.fMask = MIIM_ID | MIIM_STRING;
item_info.wID = 0;
item_info.dwTypeData = L"TEST";
item_info.cch = 4;
BOOL result = InsertMenuItem(contextMenu, 0, TRUE, &item_info);
CComPtr<IDispatch> dispDoc;
_webBrowser->get_Document(&dispDoc);
CComQIPtr<IOleWindow> oleWindow = dispDoc;
HWND browserHandle;
oleWindow->GetWindow(&browserHandle);
CComQIPtr<IHTMLEventObj2> htmlEventObj = pEvtObj;
long x, y;
htmlEventObj->get_screenX(&x);
htmlEventObj->get_screenY(&y);
result = TrackPopupMenuEx(contextMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, browserHandle , NULL);
return VARIANT_FALSE;
}