Handling the hotkey for menu item - c++

I have created a new menu item "Extra" and add it to the window's main menu.
HMENU menu = GetMenu(hWnd);
HMENU popup = CreatePopupMenu();
AppendMenu(menu, MF_STRING | MF_POPUP, (UINT)popup, L"Extra");
SetMenu(hWnd, popup);
Next I inserted new item to "Extra" item with hotkey:
#define IDM_ITEM1 12310
and:
MENUITEMINFOW mmi;
mmi.cbSize = sizeof(MENUITEMINFOW);
mmi.fMask = MIIM_STRING | MIIM_ID;
mmi.fType = MFT_STRING;
mmi.dwTypeData = L"First item\tCtrl+N";
mmi.wID = IDM_ITEM1;
InsertMenuItemW(popup, 1, true, &mmi);
And handle "First item" click:
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDM_ITEM1:
{
MessageBox(0, L"First", L"", MB_OK);
break;
}
}
break;
}
And now when I click on "First item" a message box is appeared.
But when I press Ctrl+N hotkey - nothing happens. Why? How to fix this (without using accelerator resources)?

without using accelerator resources ?
You need to create an array of ACCEL and then call the CreateAcceleratorTable function:
ACCEL s_accel[2] = {{FCONTROL | FVIRTKEY, TEXT('C'), IDM_COLOR},
{FCONTROL | FVIRTKEY, TEXT('Q'), IDM_QUIT}};
HACCEL h_accel = CreateAcceleratorTable(s_accel, 2);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(hwnd, h_accel, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
DestroyAcceleratorTable(h_accel);

Related

C++ when pressing close: minimize to system tray and keep running

I have an app in c++ for windows, which should minimize the window of the command line when the user presses the close button. It shouldn't be in the taskbar anymore and have an icon in the system tray.
What I mean is: when user presses close button, the program should only "hide" like i described.
I can only manage to make the program have an icon in the tray while running, but can't make it stay running when x is pressed
Thanks for help!
this is my code so far:
#include <iostream>
#include <Windows.h> // needed for console window and system tray functionality
// global variables
NOTIFYICONDATA trayIcon; // structure for the tray icon
HWND hwnd = GetConsoleWindow(); // handle to the console window
// function prototypes
void minimizeToTray(); // function to minimize the console window to the system tray
int main()
{
// set up the tray icon
trayIcon.cbSize = sizeof(NOTIFYICONDATA);
trayIcon.hWnd = hwnd;
trayIcon.uID = 1;
trayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
trayIcon.hIcon = (HICON)LoadImage(NULL, "icon.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE); // specify the icon file
trayIcon.uCallbackMessage = WM_USER + 1; // message identifier for tray icon clicks
trayIcon.uVersion = NOTIFYICON_VERSION_4;
strcpy_s(trayIcon.szTip, "Program Running");
// add the tray icon to the system tray
Shell_NotifyIcon(NIM_ADD, &trayIcon);
std::cout << "Program running..." << std::endl;
// set up a message loop to handle tray icon clicks and window messages
MSG msg;
while (true) // infinite loop
{
// check for messages
while (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE))
{
// if the user clicks the close button, minimize the window to the tray
if (msg.message == WM_CLOSE)
{
minimizeToTray();
continue; // skip the rest of the message loop
}
// if the user clicks the tray icon, restore the window
if (msg.message == WM_USER + 1)
{
ShowWindow(hwnd, SW_RESTORE);
}
// pass the message to the default window procedure
DispatchMessage(&msg);
}
// do other tasks here
}
// remove the tray icon before exiting the program
Shell_NotifyIcon(NIM_DELETE, &trayIcon);
return 0;
}
// function to minimize the console window to the system tray
void minimizeToTray()
{
// hide the console window
ShowWindow(hwnd, SW_HIDE);
// update the tray icon
trayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
Shell_NotifyIcon(NIM_MODIFY, &trayIcon);
}
When the user "closes" the window it should just hide and not close entirely, like ms teams or discord do
After testing your code, it definitely fails. It cannot get the message WM_CLOSE. Just as Hans Passant said, you can use SetConsoleCtrlHandler() to attached to the console receive the signal.
Here is my code. It runs well.
It used ShellExecuteW() API to restart the program to implement minimization.
Setting SW_MINIMIZE, it will minimize in the task bar.
Setting SW_HIDE, it will minimize in the system tray. But it cannot be opened again.
#include <Windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <iostream>
NOTIFYICONDATA trayIcon;
HWND hwnd = GetConsoleWindow();
BOOL WINAPI ConsoleHandler(DWORD CEvent)
{
char mesg[128];
switch (CEvent)
{
case CTRL_C_EVENT:
MessageBox(NULL,
L"CTRL+C received!", L"CEvent", MB_OK);
break;
case CTRL_BREAK_EVENT:
MessageBox(NULL,
L"CTRL+BREAK received!", L"CEvent", MB_OK);
break;
case CTRL_CLOSE_EVENT:
ShellExecuteW(NULL, L"open", L"yourexe.exe", NULL, NULL, SW_MINIMIZE);
break;
case CTRL_LOGOFF_EVENT:
MessageBox(NULL,
L"User is logging off!", L"CEvent", MB_OK);
break;
case CTRL_SHUTDOWN_EVENT:
MessageBox(NULL,
L"User is logging off!", L"CEvent", MB_OK);
break;
}
return TRUE;
}
int main()
{
trayIcon.cbSize = sizeof(NOTIFYICONDATA);
trayIcon.hWnd = hwnd;
trayIcon.uID = 1;
trayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
trayIcon.hIcon = (HICON)LoadImage(NULL, L"icon1.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
trayIcon.uCallbackMessage = WM_USER + 1;
trayIcon.uVersion = NOTIFYICON_VERSION_4;
Shell_NotifyIcon(NIM_ADD, &trayIcon);
std::cout << "Program running..." << std::endl;
MSG msg;
if (SetConsoleCtrlHandler(
(PHANDLER_ROUTINE)ConsoleHandler, TRUE) == FALSE)
{
printf("Unable to install handler!\n");
return -1;
}
while (true)
{
while (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE))
{
if (msg.message == WM_CLOSE)
{
continue;
}
if (msg.message == WM_USER + 1)
{
ShowWindow(hwnd, SW_RESTORE);
}
DispatchMessage(&msg);
}
}
Shell_NotifyIcon(NIM_DELETE, &trayIcon);
return 0;
}

Can't quit TrackPopupMenu

I'm trying to create a popup menu which appear while typing on an edit control to show suggestion. The problem is I can't continue typing as the popup wouldn't disappear. The only way is to click outside the popup. It's every inconvenient if every character you typed you have to click outside to keep writing.
I've tried SetForeGround before calling TrackPopupMenu but it didn't work.
if (HIWORD(wParam) == EN_CHANGE)
{
HMENU suggest = CreatePopupMenu();
WCHAR text[50];
GetWindowText(tagTxt, text, 50);
if (wcslen(text) > 0)
{
for (int i = 0; i < Tag.size(); i++)
{
if (wcsncmp(Tag[i].tagName, text, wcslen(text)) == 0)
AppendMenu(suggest, MF_STRING, NULL, Tag[i].tagName);
}
POINT curPoint;
GetCursorPos(&curPoint);
SetForegroundWindow(hWnd);
UINT code = TrackPopupMenu(suggest,TPM_RETURNCMD | TPM_NONOTIFY, curPoint.x, curPoint.y, 0, hWnd, NULL);
SendMessage(hWnd, WM_NULL, 0, 0);
}
}

Using WINAPI how do I change the value of a checkbox button?

Currently I have a checkbox created in WM_CREATE:
hwndButtonPollFlag =
CreateWindow(
TEXT("BUTTON"),
TEXT(sA.getMonitor(monitorSelected)->szDevice),
WS_CHILD | WS_VISIBLE | SS_WHITERECT | BS_CHECKBOX,
0,
0,
0,
0,
hwnd,
(HMENU)IDB_PollFlag,
hInstance,
NULL);
I am trying to change it's value whenever another button is clicked with:
if (sA.getScreenArray(monitorSelected)->getPollFlag())
{
SetWindowLongPtr(hwndButtonPollFlag, GCL_STYLE, WS_VISIBLE | BST_CHECKED);
}
else
{
SetWindowLongPtr(hwndButtonPollFlag, GCL_STYLE, WS_VISIBLE | BST_UNCHECKED);
}
SetWindowText(hwndButtonPollFlag, TEXT(sA.getMonitor(monitorSelected)->szDevice));
This does change the text displayed next to the checkbox but not the actual state of the button. Also I would like the checkbox to have only two states (checked or unchecked) is there any other way to create that effect other than in the button return have something along the lines of:
switch (HIWORD(wParam))
{
case BST_CHECKED:
sA.getScreenArray(monitorSelected)->setPollFlag(true);
return 0;
case BST_INDETERMINATE:
if (sA.getScreenArray(monitorSelected)->getPollFlag())
{
SetWindowLongPtr(hwndButtonPollFlag, GCL_STYLE, WS_VISIBLE | BST_UNCHECKED);
}
else
{
SetWindowLongPtr(hwndButtonPollFlag, GCL_STYLE, WS_VISIBLE | BST_CHECKED);
}
return 0;
case BST_UNCHECKED:
sA.getScreenArray(monitorSelected)->setPollFlag(false);
return 0;
}
EDIT: As Mark Ransom said I used messages with the BM_GETCHECK and BM_SETCHECK flag as so:
case IDB_MONITOR:
monitorSelected = LOWORD(lParam);
if (sA.getScreenArray(monitorSelected)->getPollFlag())
{
SendMessage(hwndButtonPollFlag, BM_SETCHECK, BST_CHECKED, NULL);
}
else
{
SendMessage(hwndButtonPollFlag, BM_SETCHECK, BST_UNCHECKED, NULL);
}
SetWindowText(hwndButtonPollFlag, TEXT(sA.getMonitor(monitorSelected)->szDevice));
return 0;
case WM_COMMAND:
//sA.getScreenArray(monitorSelected)->setPollFlag(LOWORD(lParam));
switch (LOWORD(wParam))
{
case IDB_PollFlag:
if (SendMessage(GetDlgItem(hwnd, IDB_PollFlag), BM_GETCHECK, 0, 0) == BST_CHECKED)
{
SendMessage(hwndButtonPollFlag, BM_SETCHECK, BST_CHECKED, NULL);
sA.getScreenArray(monitorSelected)->setPollFlag(true);
}
else {
SendMessage(hwndButtonPollFlag, BM_SETCHECK, BST_UNCHECKED, NULL);
sA.getScreenArray(monitorSelected)->setPollFlag(false);
}
break;
}
return 0;
You need to send the BM_SETCHECK message.
SendMessage(hwndButtonPollFlag, BM_SETCHECK, BST_CHECKED, 0);
BST_CHECKED and BST_UNCHECKED aren't window styles, they're simply flag values used by the CheckDlgButton and IsDlgButtonChecked API functions. CheckDlgButton is the function to call to change its state.
(Or, you can send BM_SETCHECK and BM_GETCHECK messages directly to the button for the same effect).

How to change menu item text?

I need to change menu item text on runtime. I've try to use GetMenuItemInfo() and SetMenuItemInfo():
case WM_NOTIFYICONMSG:
switch (lParam) {
case WM_LBUTTONDBLCLK:
someAction();
break;
case WM_RBUTTONDOWN:
{
POINT point;
GetCursorPos(&point);
HMENU hMenu;
HMENU hMenuTrackPopup;
hMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU));
if (hMenu) {
MENUITEMINFOA menuitem = { sizeof(MENUITEMINFOA) };
GetMenuItemInfoA(hMenu, IDM_EXIT, false, &menuitem);
menuitem.dwTypeData = "New text here";
SetMenuItemInfoA(hMenu, IDM_EXIT, false, &menuitem);
hMenuTrackPopup = GetSubMenu(hMenu, 0);
TrackPopupMenu(hMenuTrackPopup, 0, point.x, point.y, 0, hWnd, NULL);
DestroyMenu(hMenu);
}
}
break;
default:
break;
}
break;
But it doesn't work, text doesn't changed. What I am doing wrong? How to implement it?
As #HansPassant pointed out the solution is:
You are not using MENUITEMDATA correctly, you forgot to set the fMask member. Read the MSDN article for the struct for details
and then:
add menuitem.fMask = MIIM_TYPE | MIIM_DATA; and it works well
I can't take credit for this solution but am providing it here so that the next person that needs an answer to that question can easily find it without parsing the comments' section

RichEdit's EM_AUTOURLDETECT message recognizes link, but I can't click it

I have a RichEdit control in a dialog box. The RichEdit control displays RTF text. EM_AUTOURLDETECT causes the RichEdit control to properly format and recognize the hyperlink. When the mouse hovers over the link, the pointer changes to a hand, but the browser doesn't launch once the link is clicked.
Am I missing some kind of event handler code?
case WM_INITDIALOG:
{
// Create Richedit
HWND hwndRE = CreateWindowA("RichEdit20A", "", WS_CHILD | WS_BORDER | WS_VSCROLL | ES_READONLY | ES_MULTILINE, 10, 10, 480, 220, hDlgWnd, 0, hInst, 0);
SendMessage(hwndRE ,EM_AUTOURLDETECT,(WPARAM)TRUE,(LPARAM)0);
//SendMessage(hwndRE ,EM_SETEVENTMASK, 1, ENM_LINK | ENM_CHANGE);
ShowWindow(hwndRE, SW_SHOWNORMAL);
SETTEXTEX SetTextEx;
char* aboutdata = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Verdana;}}\\viewkind4\\uc1\\pard\\qc\\b\\f0\\fs20 www.whateverdomain.com} ");
SendMessage(hwndRE, EM_SETTEXTEX,(WPARAM)&SetTextEx, (LPARAM)(LPCTSTR)aboutdata);
return TRUE;
}
You can try something like this:
case WM_NOTIFY:
switch (((LPNMHDR)lParam)->code)
{
case EN_LINK:
ENLINK * enLinkInfo = (ENLINK *)lParam;
if (enLinkInfo->msg == WM_LBUTTONUP)
{
// code which gets clicked URL using enLinkInfo->chrg and saves it in
// "urlString"
ShellExecute(NULL, "open", urlString, NULL, NULL, SW_SHOWNORMAL);
}
break;
.... // More cases on WM_NOTIFY switch.
}
break;
Basically, when the WM_NOTIFY code is EN_LINK, you get the clicked URL and launch it using ShellExecute.
Have a look at EN_LINK:
http://msdn.microsoft.com/en-us/library/bb787970(VS.85).aspx