Skip to content

Instantly share code, notes, and snippets.

@wontoncc
Created February 10, 2014 09:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wontoncc/8912875 to your computer and use it in GitHub Desktop.
Save wontoncc/8912875 to your computer and use it in GitHub Desktop.
An executable to popup a balloon tip in notification area on Windows, written in C++.
// To popup a balloon tip in notification area.
//
// Usage:
// notification.exe [title] [content] [timeout]
// * custom icon can be used when named "notify.ico", which should stay
// in the current directory.
//
// How to compile:
// g++ notification.cpp -o notification.exe -lshlwapi
// Passed on mingw(http://nuwen.net/mingw.html).
#include <windows.h>
#include <tchar.h>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include "Shlwapi.h"
#define TRAYICON_ID 100
#define WM_NICB WM_USER+100
const char g_szClassName[] = "NOTIFYICON";
std::string szCmdline;
LPSTR g_lpCmdline;
NOTIFYICONDATA NotifyIconSetting;
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
void BalloonTip(HWND hwnd){
NotifyIconSetting.cbSize = sizeof(NOTIFYICONDATA);
NotifyIconSetting.hWnd = hwnd;
NotifyIconSetting.uID = TRAYICON_ID;
NotifyIconSetting.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
NotifyIconSetting.uCallbackMessage = WM_NICB;
NotifyIconSetting.hIcon = LoadIcon(NULL, IDI_INFORMATION);
if(PathFileExists("notify.ico")){
NotifyIconSetting.hIcon =
(HICON)::LoadImage(NULL, "notify.ico", IMAGE_ICON,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
}
Shell_NotifyIcon(NIM_ADD, &NotifyIconSetting);
NotifyIconSetting.uFlags = NIF_INFO;
std::vector<std::string> vCmdlineParts = split(szCmdline, ' ');
int i = 0;
std::string szNotificationTitle = "Usage";
std::string szNotificationContent =
"Cmdline Arguments:\n1: Title;\n2: Content;\n3: Timeout (defalut 5000ms);\n* Custom Icon named \"notify.ico\".";
int iTimeout = 5000;
for(std::string sz : vCmdlineParts){
switch(i++){
case 0:
szNotificationTitle = sz;
break;
case 1:
szNotificationContent = sz;
break;
case 2:
iTimeout = std::stoi(sz);
break;
}
}
_tcscpy(NotifyIconSetting.szInfoTitle, szNotificationTitle.c_str());
_tcscpy(NotifyIconSetting.szInfo, szNotificationContent.c_str());
Shell_NotifyIcon(NIM_MODIFY, &NotifyIconSetting);
Sleep(iTimeout);
Shell_NotifyIcon(NIM_DELETE, &NotifyIconSetting);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_CREATE:
BalloonTip(hwnd);
exit(0);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdline, int nCmdShow){
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
g_lpCmdline = lpCmdline;
szCmdline = lpCmdline;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)){
MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(0, g_szClassName, "",
0, 0, 0, 0, 0,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL){
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// ShowWindow(hwnd, nCmdShow);
// UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment