Chinaunix首页 | 论坛 | 博客
  • 博客访问: 746564
  • 博文数量: 128
  • 博客积分: 7079
  • 博客等级: 少将
  • 技术积分: 1326
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-16 08:53
文章分类

全部博文(128)

文章存档

2011年(3)

2010年(12)

2009年(9)

2008年(23)

2007年(61)

2006年(20)

我的朋友

分类: C/C++

2007-05-09 11:00:20

VC小技巧-----托盘区图标操作
经常能够看到软件运行后在托盘产生图标 ,其实也就是对结构 NOTIFYICONDATA 的设置
再调用Shell_NotifyIcon就能完成
            NOTIFYICONDATA notifycd;
           notifycd.cbSize=sizeof(NOTIFYICONDATA);
            notifycd.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);//图标资源
            notifycd.hWnd=m_hWnd;
            notifycd.uID=IDR_MAINFRAME;
            notifycd.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
            lstrcpy( notifycd.szTip, _T( "" ) );                                //默认的tooltip上的文字
            notifycd.uCallbackMessage = WM_SHELLNOTIFY;         //一旦在任务栏上产生图标,就回触发一定的消息
           
            Shell_NotifyIcon(NIM_ADD,¬ifycd);
任务栏图标上的消息处理映射
    ON_MESSAGE( WM_SHELLNOTIFY, OnShellNotify)
 
显示,删除,修改函数详细操作
//在托盘区显示图标
BOOL AddNotifyIcon(UINT Icon,LPCTSTR sztip, UINT ID)
{
    HICON hIcon;
    hIcon=AfxGetApp()->LoadIcon(Icon);
    NOTIFYICONDATA idata;
    idata.cbSize=sizeof(NOTIFYICONDATA);
    idata.hIcon=hIcon;
    CWnd *pWnd=AfxGetMainWnd();
    idata.hWnd=GetSafeHwnd();
    strcpy(idata.szTip,sztip);
    idata.uCallbackMessage=WM_SHELLNOTIFY;
    idata.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
    idata.uID=ID;
       return Shell_NotifyIcon(NIM_ADD,&idata);
}
//在托盘区删除图标
BOOL DeleteNotifyIcon(UINT Icon,LPCTSTR sztip, UINT ID)
{
    HICON hIcon;
    hIcon=AfxGetApp()->LoadIcon(Icon);
     NOTIFYICONDATA idata;
    idata.cbSize=sizeof(NOTIFYICONDATA);
    idata.hIcon=hIcon;
    CWnd *pWnd=AfxGetMainWnd();
    idata.hWnd=GetSafeHwnd();
    strcpy(idata.szTip,sztip);
    idata.uCallbackMessage=WM_SHELLNOTIFY;
    idata.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
    idata.uID=ID;
       return Shell_NotifyIcon(NIM_DELETE,&idata);   
}
//在托盘区修改图标
BOOL ModifyNotifyIcon(UINT Icon,LPCTSTR sztip, UINT ID)
{
    HICON hIcon;
    hIcon=AfxGetApp()->LoadIcon(Icon);
    NOTIFYICONDATA idata;
    idata.cbSize=sizeof(NOTIFYICONDATA);
    idata.hIcon=hIcon;
    CWnd *pWnd=AfxGetMainWnd();
    idata.hWnd=GetSafeHwnd();
    strcpy(idata.szTip,sztip);
    idata.uCallbackMessage=WM_SHELLNOTIFY;
    idata.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
    idata.uID=ID;
       return Shell_NotifyIcon(NIM_MODIFY,&idata);
}
VC小技巧---禁止同一应用程序同时运行
有时候为了避免不必要的错误,应防止同一应用程序被打开两个实例
以下一个函数可以达到此项目的,挺有用的!
BOOL C××App::AlreadyRunning()
{
    BOOL bFound = FALSE;
    // Try to create a mutex with the app's name
    HANDLE hMutexOneInstance = ::CreateMutex(NULL,TRUE,_T(AfxGetAppName()));
    // Already there...means that we are already running an instance
    if(::GetLastError() == ERROR_ALREADY_EXISTS)
        bFound = TRUE;
    // Release the mutex
    if(hMutexOneInstance)
        ::ReleaseMutex(hMutexOneInstance);
    return(bFound);
}
VC小技巧 ----只要在
BOOL C**App::InitInstance()
{
    // Is it already running?
    if(AlreadyRunning())
    {
        // Yep...get out now
        AfxMessageBox(IDS_ALREADY_RUNNING,MB_ICONWARNING);
        return(FALSE);
    }
    。。。。。
}
 
实现窗体透明
The SetLayeredWindowAttributes function sets the opacity and transparency color key of a layered window.

 //=======================================
  SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,
 GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
 HINSTANCE hInst = LoadLibrary("User32.DLL"); //显式加载DLL
 if(hInst)
 {           
  typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);         
  MYFUNC fun = NULL;    
  fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");//取得SetLayeredWindowAttributes函数指针
  if(fun)fun(this->GetSafeHwnd(),0,100,2);    
  FreeLibrary(hInst);
 }
//=========================================

 
SetLayeredWindowAttributes Function
--------------------------------------------------------------------------------
The SetLayeredWindowAttributes function sets the opacity and transparency color key of a layered window.
Syntax
BOOL SetLayeredWindowAttributes(          HWND hwnd,
    COLORREF crKey,
    BYTE bAlpha,
    DWORD dwFlags
);Parameters
hwnd
[in] Handle to the layered window. A layered window is created by specifying WS_EX_LAYERED when creating the window with the CreateWindowEx function or by setting WS_EX_LAYERED via SetWindowLong after the window has been created.
crKey
[in] COLORREF structure that specifies the transparency color key to be used when composing the layered window. All pixels painted by the window in this color will be transparent. To generate a COLORREF, use the RGB macro.
bAlpha
[in] Alpha value used to describe the opacity of the layered window. Similar to the SourceConstantAlpha member of the BLENDFUNCTION structure. When bAlpha is 0, the window is completely transparent. When bAlpha is 255, the window is opaque.
dwFlags
[in] Specifies an action to take. This parameter can be one or more of the following values.
LWA_COLORKEY
Use crKey as the transparency color.
LWA_ALPHA
Use bAlpha to determine the opacity of the layered window.
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
阅读(1261) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~