How to add tray icon behind "start" tray icon, the same as "Search" or "Cortana"

DJoker 1 Reputation point
2022-10-12T13:31:44.333+00:00

what windows api i can use

Windows development | Windows API - Win32
Developer technologies | C++
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Tong Xu - MSFT 2,546 Reputation points Microsoft External Staff
    2022-10-13T06:51:21.43+00:00

    Hi,@DJoker
    Welcome to Microsoft Q&A!
    Is it to add the application icon to the taskbar?

    This does not require the use of windows APIs,just right-click on the icon and select “pin to taskbar”.
    250052-%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE-2022-10-13-142837.png

    If you want to create an icon for a new application, refer to the following documentation:

    https://learn.microsoft.com/en-us/windows/win32/menurc/using-icons

    If you have other questions, you can ask me anytime.
    Tong


  2. Tong Xu - MSFT 2,546 Reputation points Microsoft External Staff
    2022-10-14T09:58:07.6+00:00

    If you are adding after the start bar just as mentioned above, create a desktop icon and then pin to taskbar.
    I wrote a sample based on your needs. It is to create a tray icon on the right side of the taskbar, with two options, and double-click the icon to exit.
    Check that if it meets your needs.
    #include<windows.h>
    #include<cstdio>
    #include<stdlib.h>

    #include <windows.h>  
    #define IDR_PAUSE 12  
    #define IDR_START 13  
      
    #include <shellapi.h>  
    #pragma   comment(lib,   "shell32.lib")  
      
    LPCTSTR szAppName = TEXT("project");  
    LPCTSTR szWndName = TEXT("project");  
    HMENU hmenu;  
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)  
    {  
    	NOTIFYICONDATA nid;  
    	UINT WM_TASKBARCREATED;  
    	POINT pt;//recive the cordinate of mouse  
    	int xx;//recive the value  
    	// do not modified TaskbarCreated  
    	WM_TASKBARCREATED = RegisterWindowMessage(TEXT("TaskbarCreated"));  
    	switch (message)  
    	{  
    	case WM_CREATE:  
    		nid.cbSize = sizeof(nid);  
    		nid.hWnd = hwnd;  
    		nid.uID = 0;  
    		nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;  
    		nid.uCallbackMessage = WM_USER;  
    		nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);  
    		lstrcpy(nid.szTip, szAppName);  
    		Shell_NotifyIcon(NIM_ADD, &nid);  
    		hmenu = CreatePopupMenu();  
    		AppendMenu(hmenu, MF_STRING, IDR_PAUSE, "stop");//two option   
    		AppendMenu(hmenu, MF_STRING, IDR_START, "go on");  
    		break;  
    	case WM_USER:  
    		if (lParam == WM_LBUTTONDOWN)  
    			MessageBox(hwnd, TEXT("Win32 API doubleclick exit!"), szAppName, MB_OK);  
    		if (lParam == WM_LBUTTONDBLCLK)//doubleclick exit.  
    			SendMessage(hwnd, WM_CLOSE, wParam, lParam);  
    		if (lParam == WM_RBUTTONDOWN)  
    		{  
    			GetCursorPos(&pt);  
    			::SetForegroundWindow(hwnd);  
    			EnableMenuItem(hmenu, IDR_PAUSE, MF_GRAYED);//grey section 1  
    			xx = TrackPopupMenu(hmenu, TPM_RETURNCMD, pt.x, pt.y, NULL, hwnd, NULL);  
    			if (xx == IDR_PAUSE) MessageBox(hwnd, TEXT("section 1"), szAppName, MB_OK);  
    			if (xx == IDR_START) MessageBox(hwnd, TEXT("section 2"), szAppName, MB_OK);  
    			if (xx == 0) PostMessage(hwnd, WM_LBUTTONDOWN, NULL, NULL);  
    			MessageBox(hwnd, TEXT("right click"), szAppName, MB_OK);  
    		}  
    		break;  
    	case WM_DESTROY:  
    		Shell_NotifyIcon(NIM_DELETE, &nid);  
    		PostQuitMessage(0);  
    		break;  
    	default:  
    		if (message == WM_TASKBARCREATED)  
    			SendMessage(hwnd, WM_CREATE, wParam, lParam);  
    		break;  
    	}  
    	return DefWindowProc(hwnd, message, wParam, lParam);  
    }  
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,  
    	LPSTR szCmdLine, int iCmdShow)  
    {  
    	HWND hwnd;  
    	MSG msg;  
    	WNDCLASS wndclass;  
    	HWND handle = FindWindow(NULL, szWndName);  
    	if (handle != NULL)  
    	{  
    		MessageBox(NULL, TEXT("Application is already running"), szAppName, MB_ICONERROR);  
    		return 0;  
    	}  
    	wndclass.style = CS_HREDRAW | CS_VREDRAW;  
    	wndclass.lpfnWndProc = WndProc;  
    	wndclass.cbClsExtra = 0;  
    	wndclass.cbWndExtra = 0;  
    	wndclass.hInstance = hInstance;  
    	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);  
    	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);  
    	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);  
    	wndclass.lpszMenuName = NULL;  
    	wndclass.lpszClassName = szAppName;  
    	if (!RegisterClass(&wndclass))  
    	{  
    		MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);  
    		return 0;  
    	}  
      
    	hwnd = CreateWindowEx(WS_EX_TOOLWINDOW,  
    		szAppName, szWndName,  
    		WS_POPUP,  
    		CW_USEDEFAULT,  
    		CW_USEDEFAULT,  
    		CW_USEDEFAULT,  
    		CW_USEDEFAULT,  
    		NULL, NULL, hInstance, NULL);  
    	ShowWindow(hwnd, iCmdShow);  
    	UpdateWindow(hwnd);  
    	while (GetMessage(&msg, NULL, 0, 0))  
    	{  
    		TranslateMessage(&msg);  
    		DispatchMessage(&msg);  
    	}  
    	return msg.wParam;  
    }  
    

    Caution, it must be launched as a Windows desktop program.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.