2025-11-27 16:46:48 +09:00

274 lines
7.4 KiB
C++

// TaskbarDlg.cpp : implementation file
//
#include "stdafx.h"
#include "utsampleabout.h"
#include "Taskbar.h"
#include "TaskbarDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTaskbarDlg dialog
CTaskbarDlg::CTaskbarDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTaskbarDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTaskbarDlg)
m_sTooltipText = _T("The Code Project - COXTaskbarIcon Demo");
m_bRouteToMe = FALSE;
m_nIcon = 0;
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//---------------- Demo Codes: all other codes were generated by AppWizard
m_TaskbarIcon.Create();
// When there is only one taskbar icon, you don't need to specify an ID.
// The ID is useful only when implementing two (or more) taskbar icons, AND you want
// to handle mouse messages from both icons within one message handler (therefore, you
// need that ID to tell which icon posted the msg). However, it might be a good idea to
// assign an ID, and check the ID in the message handler, especially when you may need
// to derive your classes (somebody else may add another taskbar icon).
// Note: you don't need to detroy a taskbar icon. It's done in its own destructor.
m_TaskbarIcon.m_pPopupOwner = this;
// let this dialog handle popup menu's message
// Note: m_pPopupOwner is NOT a member of COXTaskbarIcon. This is for demo only.
//---------------- End of this block
}
void CTaskbarDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTaskbarDlg)
DDX_Text(pDX, IDC_EDIT_TOOLTIP, m_sTooltipText);
DDX_Check(pDX, IDC_ROUTE_TO_ME, m_bRouteToMe);
DDX_Radio(pDX, IDC_ICON1, m_nIcon);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTaskbarDlg, CDialog)
//{{AFX_MSG_MAP(CTaskbarDlg)
ON_WM_PAINT()
ON_WM_SYSCOMMAND()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_TOGGLE_SHOW, OnToggleShow)
ON_BN_CLICKED(IDC_ICON1, OnIcon1)
ON_BN_CLICKED(IDC_ICON2, OnIcon2)
ON_BN_CLICKED(IDC_SET_TOOLTIP, OnSetTooltip)
ON_BN_CLICKED(IDC_ROUTE_TO_ME, OnRouteToMe)
ON_BN_CLICKED(IDC_MINIMIZE, OnMinimize)
ON_MESSAGE(WM_NOTIFY_TASKBAR_ICON, OnNotifyTaskbarIcon)
ON_COMMAND(ID_OPEN, OnOpen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTaskbarDlg message handlers
BOOL CTaskbarDlg::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);
}
}
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
return TRUE;
}
// 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 CTaskbarDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (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();
}
}
HCURSOR CTaskbarDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//---------------- Demo Codes: all other codes were generated by AppWizard
void CTaskbarDlg::OnToggleShow()
{
if(m_TaskbarIcon.IsShowing())
{
m_TaskbarIcon.Hide();
SetDlgItemText(IDC_EDIT_CODES, _T("Hide()"));
}
else
{
UpdateData(); // pushing data exchange
m_TaskbarIcon.SetIcon(m_nIcon == 0 ? IDI_ICON1 : IDI_ICON2);
// no need to specify bRefresh before showing a taskbar icon
m_TaskbarIcon.SetTooltipText(m_sTooltipText);
m_TaskbarIcon.Show();
SetDlgItemText(IDC_EDIT_CODES, _T("Show()"));
}
SetDlgItemText(IDC_TOGGLE_SHOW, m_TaskbarIcon.IsShowing() ? _T("Hide") : _T("Show"));
}
void CTaskbarDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CUTSampleAboutDlg dlgAbout(IDR_MAINFRAME,ID_DESCRIPTION_FILE);
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CTaskbarDlg::OnIcon1()
{
m_TaskbarIcon.SetIcon(IDI_ICON1); // taking the default TRUE value of bRefresh
// if the code is wriiten as
// m_TaskbarIcon.SetIcon(IDI_ICON1, FALSE);
// no instant refresh will occur.
SetDlgItemText(IDC_EDIT_CODES, _T("SetIcon(IDI_ICON1)"));
}
void CTaskbarDlg::OnIcon2()
{
m_TaskbarIcon.SetIcon(IDI_ICON2); // instantly refresh its icon
SetDlgItemText(IDC_EDIT_CODES, _T("SetIcon(IDI_ICON2)"));
}
void CTaskbarDlg::OnSetTooltip()
{
UpdateData(); // pushing data exchange
m_TaskbarIcon.SetTooltipText(m_sTooltipText); // instantly refresh its tooltip text
SetDlgItemText(IDC_EDIT_CODES,
CString(_T("SetTooltipText(\"")) + m_sTooltipText + _T("\")"));
}
void CTaskbarDlg::OnRouteToMe()
{
UpdateData(); // pushing data exchange
if (m_bRouteToMe)
{
m_TaskbarIcon.SetOwner(this); // use message handlers in this class
SetDlgItemText(IDC_EDIT_CODES, _T("SetOwner(this)"));
}
else
{
m_TaskbarIcon.SetOwner(NULL); // use message handlers in taskbar icon class
SetDlgItemText(IDC_EDIT_CODES, _T("SetOwner(NULL)"));
SetDlgItemText(IDC_EDIT_MOUSEMSG, _T(""));
}
}
void CTaskbarDlg::OnMinimize()
{
if (!m_TaskbarIcon.IsShowing()) // don't hide all things from users
{
OnToggleShow();
}
if (m_bRouteToMe) // I'm sure I can't handle messages when I'm hidden
{
((CButton*)GetDlgItem(IDC_ROUTE_TO_ME))->SetCheck(0);
m_TaskbarIcon.SetOwner(NULL); // let taskbar icon do it
SetDlgItemText(IDC_EDIT_MOUSEMSG, _T(""));
}
ShowWindow(FALSE);
// This is only one way to hide the dialog window. In real situation, you may
// want to try DestroyWindow() to conserve system resource if possible (in this
// demo, this dialog is the main window).
}
// handling taskbar icon's notification here after SetOwner(this)
LRESULT CTaskbarDlg::OnNotifyTaskbarIcon(WPARAM wParam, LPARAM lParam)
{
UINT uID = (UINT)wParam; // this is the ID you assigned to your taskbar icon
UINT uMouseMsg = (UINT)lParam; // mouse messages
if (uID != 0) return 0;
// no ID was assigned when calling Create(), therefore, if a message has some value
// in uID, it may not from the expected taskbar icon.
// In real situation, you may write it in another way:
// if (uID == IDC_MYTASKBARICON)
// ...
CString sOutput;
switch (uMouseMsg)
{
case WM_LBUTTONDOWN: sOutput = _T("WM_LBUTTONDOWN"); break;
case WM_LBUTTONDBLCLK: sOutput = _T("WM_LBUTTONDBLCLK"); break;
case WM_RBUTTONDOWN: sOutput = _T("WM_RBUTTONDOWN"); break;
case WM_RBUTTONDBLCLK: sOutput = _T("WM_RBUTTONDBLCLK"); break;
case WM_MOUSEMOVE: sOutput = _T("WM_MOUSEMOVE"); break;
}
// You may also use GetKeyState() here.
if (!sOutput.IsEmpty()) SetDlgItemText(IDC_EDIT_MOUSEMSG, sOutput);
return -1; // don't care value
}
// handling popup menu commands sent by the taskbar icon object
void CTaskbarDlg::OnOpen()
{
ShowWindow(TRUE);
BringWindowToTop();
}
//---------------- End of this block