ورود

View Full Version : سوال: مشکل در ساختن System Tray Icons



abasfar
شنبه 26 شهریور 1390, 00:07 صبح
سلام
مشکل این کد چیه؟
ایکون را میسازه

// P4Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "P4.h"
#include "P4Dlg.h"
#include "shlwapi.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About
NOTIFYICONDATA m_SysTryIcon;

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
enum { IDD = IDD_ABOUTBOX };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

// Implementation
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CP4Dlg dialog




CP4Dlg::CP4Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CP4Dlg::IDD, pParent)
, m_visible(false)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CP4Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CP4Dlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_WM_DESTROY()
ON_MESSAGE (WM_USER+75,OnSystemTryMessage)
ON_WM_WINDOWPOSCHANGING()
END_MESSAGE_MAP()


// CP4Dlg message handlers

BOOL CP4Dlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
m_SysTryIcon.hWnd=this->GetSafeHwnd();
m_SysTryIcon.uID = 0x1;
m_SysTryIcon.cbSize=NOTIFYICONDATA_V2_SIZE;
m_SysTryIcon.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_SysTryIcon.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
StrCpy(m_SysTryIcon.szTip, L" ");
m_SysTryIcon.uCallbackMessage=WM_USER+75;
Shell_NotifyIcon(NIM_ADD,&m_SysTryIcon);
return TRUE; // return TRUE unless you set the focus to a control
}

void CP4Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CP4Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CP4Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}


void CP4Dlg::OnDestroy()
{
Shell_NotifyIcon(NIM_DELETE,&m_SysTryIcon);
CDialog::OnDestroy();

// TODO: Add your message handler code here
}

LRESULT CP4Dlg::OnSystemTryMessage(WPARAM wParam, LPARAM lParam)
{

if(lParam == WM_RBUTTONDOWN) //
{
CMenu mnu; //
mnu.LoadMenu(IDR_MENU1);
CMenu *PopUpMenu;
PopUpMenu=mnu.GetSubMenu(0);
SetForegroundWindow();
CPoint pt; //
GetCursorPos(&pt); //
PopUpMenu->TrackPopupMenu(TPM_RIGHTALIGN,pt.x,pt.y,this); //
}
return LRESULT();
}

void CP4Dlg::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
if(!m_visible)
lpwndpos->flags &= ~SWP_SHOWWINDOW;
CDialog::OnWindowPosChanging(lpwndpos);

// TODO: Add your message handler code here
}


اما وقتی روش کلیک میکنی تو کد زیر error میگیره


BOOL CMenu::TrackPopupMenu(UINT nFlags, int x, int y,
CWnd* pWnd, LPCRECT lpRect)
{
ASSERT(m_hMenu != NULL);

_AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
HWND hWndOld = pThreadState->m_hTrackingWindow;
HMENU hMenuOld = pThreadState->m_hTrackingMenu;
pThreadState->m_hTrackingWindow = pWnd->GetSafeHwnd();
pThreadState->m_hTrackingMenu = m_hMenu;
BOOL bOK = ::TrackPopupMenu(m_hMenu, nFlags, x, y, 0,
pThreadState->m_hTrackingWindow, lpRect);
pThreadState->m_hTrackingWindow = hWndOld;
pThreadState->m_hTrackingMenu = hMenuOld;

return bOK;
}

ASSERT(m_hMenu != NULL);
البته این تابع از wincore.cpp خود ویژوال است

mehdi.mousavi
یک شنبه 27 شهریور 1390, 00:40 صبح
سلام.


پیام خطای فوق الذکر نشون میده که Menu مورد نظر شما، Destroy شده و m_hMenu برابر NULL هستش.
برای ایجاد Icon ها در system tray، کدهای مربوطه رو در کلاسی Encapsulate کنید، که کدتون خوانا بشه.
خوشبختانه این کار رو قبلا Chris Maunder انجام داده و فقط کافیه از اون کلاس استفاده کنید (http://www.codeproject.com/KB/shell/systemtray.aspx).

موفق باشید.

pe32_64
سه شنبه 29 شهریور 1390, 14:11 عصر
من برای انجام این کار و با آیکون انیمیشنی آین کد ها را در انجمن برنامه های نوشته شده با visual c++ یک مثال قراردادم.لطفا ملاحظه کنید.
اینم لینک دریافت برنامه (http://barnamenevis.org/attachment.php?attachmentid=28573&d=1235415175)