#define ID_TUOPAN WM_USER + 1
#define ID_TUOPAN_CALLBACK WM_USER + 2
#define ID_TUOPAN_OPEN WM_USER + 3
#define ID_TUOPAN_EXIT WM_USER + 4
LRESULT CALLBACK WndProc ( HWND, UINT, WPARAM, LPARAM );
LRESULT CALLBACK ChildMenu_WndProc ( HWND, UINT, WPARAM, LPARAM );
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// ...注册主窗口类和创建窗口
wndclass.lpfnWndProc = ChildMenu_WndProc;
wndclass.lpszClassName = cm_ClassName;
wndclass.lpszMenuName = NULL;
if (!RegisterClassEx(&wndclass)){
MessageBox(NULL, TEXT("注册子窗口类失败!"), szAppName, MB_ICONERROR);
return 0;
}
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
TCHAR szbuffer[] = TEXT("你好 World!");
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
DrawTextEx(hdc, szbuffer, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER, NULL);
EndPaint(hwnd, &ps);
return 0;
case WM_CREATE:
// 系统托盘
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hwnd;
nid.uID = ID_TUOPAN;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = ID_TUOPAN_CALLBACK;
nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
StringCchCopy(nid.szTip, ARRAYSIZE(nid.szTip), szAppName);
Shell_NotifyIcon(NIM_ADD, &nid);
// // 子窗口1 , 这里取消注释后WM_COMMAND和WM_CLOSE、WM_DESTROY包括子窗口过程函数的都会出错
// HWND ChildMenu_hwnd = CreateWindowEx(
// 0, cm_ClassName, cm_ClassName,
// WS_CHILD | WS_VISIBLE,
// 0, 0, 100, 100,
// hwnd, NULL, NULL, NULL);
// if (!ChildMenu_hwnd){
// MessageBox(NULL, TEXT("创建子窗口——ChildMenu_hwnd失败!"), szAppName, MB_ICONERROR);
// return 0;
// }
return DefWindowProc(hwnd, message, wParam, lParam);
case ID_TUOPAN_CALLBACK:
switch (lParam){
case WM_LBUTTONDBLCLK:
ShowWindow(hwnd, SW_SHOW);
break;
case WM_RBUTTONDOWN:
POINT pt;
GetCursorPos(&pt);
HMENU hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, ID_TUOPAN_OPEN, TEXT("打开"));
AppendMenu(hMenu, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
AppendMenu(hMenu, MF_STRING, ID_TUOPAN_EXIT, TEXT("Exit"));
TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hwnd, NULL);
DestroyMenu(hMenu);
break;
}
return 0;
case WM_COMMAND:
switch (wParam){
case ID_TUOPAN_OPEN:
ShowWindow(hwnd, SW_SHOW);
break;
case ID_TUOPAN_EXIT:
DestroyWindow(hwnd);
break;
}
return 0;
case WM_CLOSE:
ShowWindow(hwnd, SW_HIDE);
return 0;
case WM_DESTROY:
Shell_NotifyIcon(NIM_DELETE, &nid);
// DestroyWindow(ChildMenu_hwnd);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
WM_COMMAND和WM_CLOSE、WM_DESTROY红线,编译器错误提示gcc: jump to case label