- 注册时间
- 2004-11-1
- 最后登录
- 2018-4-24
版主
- 积分
- 548
|
发表于 2012-10-10 14:33:23
|
显示全部楼层
代码就一个文件- #include <windows.h>
- #include <gdiplus.h>
- unsigned int iconmessage = RegisterWindowMessage("IconNotify");
- #define MYWM_ICON (WM_APP+100)
- HMENU popMenu;
- NOTIFYICONDATA nid;
- struct YY
- {
-     const char* szClassName;
-     HWND hwnd;
-     MSG messages;
-     WNDCLASSEX wincl;
-     ULONG_PTR gdiplusStartupToken;
-     YY() :szClassName("YY")
-     {}
-     ~YY()
-     {
-         Gdiplus::GdiplusShutdown(gdiplusStartupToken); //消毁gdi+
-     }
-     void InitWin(HINSTANCE hThisInstance)
-     {
-         //初始化gdi+
-         Gdiplus::GdiplusStartupInput gdiInput;
-         Gdiplus::GdiplusStartup(&gdiplusStartupToken,&gdiInput,NULL);
-         wincl.hInstance = hThisInstance;
-         wincl.lpszClassName = szClassName;
-         wincl.lpfnWndProc = WindowProcedure;
-         wincl.style = CS_DBLCLKS;
-         wincl.cbSize = sizeof (WNDCLASSEX);
-         wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
-         wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
-         wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
-         wincl.lpszMenuName = NULL;
-         wincl.cbClsExtra = 0;
-         wincl.cbWndExtra = 0;
-         wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
-         if (!RegisterClassEx (&wincl))
-             return;
-         hwnd = CreateWindowEx (
-                    WS_EX_LAYERED|WS_EX_TOOLWINDOW|WS_EX_TOPMOST,
-                    szClassName,
-                    "YY Test",
-                    WS_OVERLAPPEDWINDOW,
-                    0,
-                    0,
-                    450,
-                    600,
-                    NULL,
-                    NULL,
-                    hThisInstance,
-                    NULL
-                );
-         ShowWindow (hwnd, SW_SHOWNORMAL);
-         MoveWindow(hwnd, 0, 0, 450, 600, TRUE);
-         SetPic(L"alice.png");
-         nid.cbSize = sizeof(NOTIFYICONDATA);
-         nid.hWnd = hwnd;
-         nid.uID = iconmessage;
-         nid.uFlags = NIF_ICON|NIF_MESSAGE;
-         nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
-         nid.uCallbackMessage = MYWM_ICON;
-     }
-     int Run()
-     {
-         while (GetMessage (&messages, NULL, 0, 0))
-         {
-             TranslateMessage(&messages);
-             DispatchMessage(&messages);
-         }
-         return messages.wParam;
-     }
-     void SetPic(const wchar_t* fn)
-     {
-         RECT wndRect;
-         GetWindowRect(hwnd,&wndRect);
-         SIZE wndSize = {wndRect.right-wndRect.left, wndRect.bottom-wndRect.top};
-         HDC hdc = GetDC(hwnd);
-         HDC memDC = CreateCompatibleDC(hdc);
-         HBITMAP memBitmap = CreateCompatibleBitmap(hdc, wndSize.cx, wndSize.cy);
-         SelectObject(memDC,memBitmap);
-         Gdiplus::Image image(fn);
-         Gdiplus::Graphics graphics(memDC);
-         graphics.DrawImage(&image, 0, 0, wndSize.cx,wndSize.cy);
-         HDC screenDC = GetDC(NULL);
-         POINT ptSrc = {0,0};
-         BLENDFUNCTION blendFunction;
-         blendFunction.AlphaFormat = AC_SRC_ALPHA;
-         blendFunction.BlendFlags = 0;
-         blendFunction.BlendOp = AC_SRC_OVER;
-         blendFunction.SourceConstantAlpha = 255;
-         UpdateLayeredWindow(hwnd,screenDC,&ptSrc,&wndSize,memDC,&ptSrc,0,&blendFunction,2);
-         DeleteDC(memDC);
-         DeleteObject(memBitmap);
-         ReleaseDC(hwnd, hdc);
-         ReleaseDC(0, screenDC);
-     }
-     static LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
-     {
-         switch (message)
-         {
-         case WM_CREATE:
-             popMenu = CreatePopupMenu();
-             AppendMenu(popMenu, MF_STRING, 1, "隐藏");
-             AppendMenu(popMenu, MF_STRING, 2, "恢复");
-             AppendMenu(popMenu, MF_STRING, 3, "关于");
-             AppendMenu(popMenu, MF_STRING, 4, "退出");
-             break;
-         case WM_RBUTTONDOWN:
-             POINT pt;
-             pt.x = LOWORD(lParam);
-             pt.y = HIWORD(lParam);
-             ClientToScreen(hwnd, &pt);
-             TrackPopupMenu(popMenu, TPM_RIGHTALIGN, pt.x, pt.y, 0, hwnd, NULL);
-             break;
-         case MYWM_ICON:
-             if(wParam == iconmessage)
-             {
-                 switch(lParam)
-                 {
-                 case WM_LBUTTONDBLCLK: //左双击
-                     ShowWindow(hwnd, SW_SHOWNORMAL);
-                     Shell_NotifyIcon(NIM_DELETE, &nid);
-                     break;
-                 case WM_RBUTTONDOWN: //右单击
-                     POINT pt;
-                     GetCursorPos(&pt);
-                     SetForegroundWindow(hwnd);
-                     TrackPopupMenu(popMenu, TPM_RIGHTALIGN, pt.x, pt.y, 0, hwnd, NULL);
-                     break;
-                 case WM_MOUSEMOVE: //悬停
-                     break;
-                 }
-             }
-             break;
-         case WM_COMMAND:
-             switch(LOWORD(wParam))
-             {
-             case 1:
-                 ShowWindow(hwnd, SW_HIDE);
-                 Shell_NotifyIcon(NIM_ADD, &nid);
-                 break;
-             case 2:
-                 ShowWindow(hwnd, SW_SHOWNORMAL);
-                 Shell_NotifyIcon(NIM_DELETE, &nid);
-                 break;
-             case 3:
-                 MessageBox(hwnd, "用gdi+实现透明不规则界面", "关于", MB_OK);
-                 break;
-             case 4:
-                 PostQuitMessage(0);
-                 break;
-             }
-             break;
-         case WM_DESTROY:
-             DestroyMenu(popMenu);
-             PostQuitMessage (0);
-             break;
-         default:
-             return DefWindowProc (hwnd, message, wParam, lParam);
-         }
-         return 0;
-     }
- };
- int WINAPI WinMain (HINSTANCE hThisInstance,
-                     HINSTANCE hPrevInstance,
-                     LPSTR lpszArgument,
-                     int nCmdShow)
- {
-     YY yy;
-     yy.InitWin(hThisInstance);
-     return yy.Run();
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|