18,124
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <shellapi.h>
#include "resource.h"
#define NM_NOTIFYICON_CLICKED 100
extern WINBASEAPI HWND WINAPI GetConsoleWindow();
HINSTANCE hInst;
BOOL addNotifyIcon(HWND hWnd)
{
BOOL result;
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uFlags = NIF_ICON | NIF_MESSAGE;
nid.uCallbackMessage = NM_NOTIFYICON_CLICKED;
hInst = (HINSTANCE)GetModuleHandle(NULL);
nid.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_NOTIFY_ICON));
result = Shell_NotifyIcon(NIM_ADD, &nid);
return result;
}
int main()
{
HWND hWnd;
MSG msg;
int wmId;
BOOL shouldExit = FALSE;
printf("Hello world!\n");
hWnd = GetConsoleWindow();
addNotifyIcon(hWnd);
while(!shouldExit)
{
// if (GetMessage(&msg, NULL, 0, 0) != 0)
if (PeekMessage(&msg, hWnd, 0, 0, PM_NOREMOVE))
{
printf("message peeked.\n");
switch(msg.message)
{
case WM_COMMAND:
wmId = LOWORD(msg.wParam);
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
shouldExit = TRUE;
break;
default:
return DefWindowProc(hWnd, msg.message, msg.wParam, msg.lParam);
}
break;
case NM_NOTIFYICON_CLICKED:
switch (LOWORD(msg.lParam))
{
case WM_CONTEXTMENU:
{
POINT const pt = { LOWORD(msg.wParam), HIWORD(msg.wParam) };
HMENU hMenu = GetSubMenu(LoadMenu(hInst, MAKEINTRESOURCE(IDR_POPUP_MENU)), 0);
// our window must be foreground before calling TrackPopupMenu or the menu will not disappear when the user clicks away
SetForegroundWindow(hWnd);
// respect menu drop alignment
UINT uFlags = TPM_RIGHTBUTTON;
if (GetSystemMetrics(SM_MENUDROPALIGNMENT) != 0)
{
uFlags |= TPM_RIGHTALIGN;
}
else
{
uFlags |= TPM_LEFTALIGN;
}
TrackPopupMenuEx(hMenu, uFlags, pt.x, pt.y, hWnd, NULL);
DestroyMenu(hMenu);
break;
}
case WM_LBUTTONDBLCLK:
ShowWindow(hWnd, SW_RESTORE);
SetForegroundWindow(hWnd);
break;
default:
break;
}
break;
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
system("pause");
return 0;
}
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif
#define IDI_NOTIFY_ICON 101
#define IDR_POPUP_MENU 103
#define IDM_EXIT 40000
// Generated by ResEdit 1.6.6
// Copyright (C) 2006-2015
// http://www.resedit.net
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"
//
// Menu resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDR_POPUP_MENU MENU
{
POPUP "TrayMenu"
{
MENUITEM "E&xit", IDM_EXIT
}
}
//
// Icon resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDI_NOTIFY_ICON ICON "studyNotifyArea.ico"