把一些乱七八糟的东西堆起来,然后就...不过这个怪物还会长.
WindowDemoDlg.cpp文件
// WinddowDemoDlg.cpp : 实现文件 // #include "stdafx.h" #include "WinddowDemo.h" #include "WinddowDemoDlg.h" #include "afxdialogex.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialogEx { public: CAboutDlg(); // 对话框数据 enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) END_MESSAGE_MAP() // CWinddowDemoDlg 对话框 CWinddowDemoDlg::CWinddowDemoDlg(CWnd* pParent /*=NULL*/) : CDialogEx(CWinddowDemoDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CWinddowDemoDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_BTN_NOTIFY_ICON, m_notify_icon); DDX_Control(pDX, IDC_EDIT1, m_info); } BEGIN_MESSAGE_MAP(CWinddowDemoDlg, CDialogEx) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDOK, &CWinddowDemoDlg::OnBnClickedOk) ON_BN_CLICKED(IDC_BTN_NOTIFY_ICON, &CWinddowDemoDlg::OnBnClickedBtnNotifyIcon) ON_MESSAGE(WM_NOTIFYICON, &CWinddowDemoDlg::OnNotifyicon) ON_BN_CLICKED(IDC_BTN_SW_WINDOW, &CWinddowDemoDlg::OnBnClickedBtnSwWindow) ON_COMMAND(ID_SHOW_HIDE, &CWinddowDemoDlg::OnNotifyIconShowHide) ON_COMMAND(ID_NOTIFYICON_SET, &CWinddowDemoDlg::OnNotifyiconSet) ON_COMMAND(ID_NOTIFYICON_EXIT, &CWinddowDemoDlg::OnNotifyiconExit) ON_BN_CLICKED(IDC_BTN_CLIBOARD, &CWinddowDemoDlg::OnBnClickedBtnCliboard) ON_WM_CHANGECBCHAIN() ON_WM_DRAWCLIPBOARD() ON_BN_CLICKED(IDC_BTN_SHORT_CUT, &CWinddowDemoDlg::OnBnClickedBtnShortCut) ON_COMMAND(ID_FILE_OPEN, &CWinddowDemoDlg::OnFileOpen) ON_COMMAND(ID_EXIT, &CWinddowDemoDlg::OnExit) ON_COMMAND(ID_ABout, &CWinddowDemoDlg::OnAbout) END_MESSAGE_MAP() // CWinddowDemoDlg 消息处理程序 BOOL CWinddowDemoDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // 将“关于...”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 // TODO: 在此添加额外的初始化代码 //初始化托盘信息 m_IconData.cbSize = sizeof(NOTIFYICONDATA); m_IconData.uID = IDR_MAINFRAME; m_IconData.hWnd = this->GetSafeHwnd(); m_IconData.hIcon = m_hIcon; m_IconData.uCallbackMessage = WM_NOTIFYICON; m_IconData.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE; _tcscpy_s(m_IconData.szTip, TEXT("WindowsDemo")); //注意必须先加载菜单,然后再设置状态栏 //显示菜单栏 m_menu.LoadMenu(IDR_MENU2); SetMenu(&m_menu); ////创建状态栏 if(m_StatusBar.Create(WS_CHILD|WS_VISIBLE|SBT_OWNERDRAW, CRect(0, 0, 0, 0), this, ID_STATUS_BAR)) { m_StatusBar.EnableAutomation(); //右队齐,开始的长度. CRect rt; m_StatusBar.GetClientRect(&rt); int nParts = 2; int width[3] = {rt.Width()/nParts, -1}; //设置分栏 int i = 0; VERIFY(m_StatusBar.SetParts(nParts, width)); //获取系统信息 //系统版本 OSVERSIONINFO osInfo; osInfo.dwOSVersionInfoSize = sizeof(osInfo); CString str; GetVersionEx(&osInfo); switch (osInfo.dwPlatformId) { case VER_PLATFORM_WIN32s: str = TEXT("Windows 3.1 OS"); break; case VER_PLATFORM_WIN32_WINDOWS: { if(osInfo.dwMinorVersion == 0) str = TEXT("Windows 95"); else str = TEXT("Windows 98"); } break; case VER_PLATFORM_WIN32_NT: str = TEXT("Windows NT "); break; default: str = TEXT("Windows CE"); break; } m_StatusBar.SetText(str, i++, 0); //IP信息 WSADATA wsaData; WSAStartup(MAKEWORD(2,2), &wsaData); char szHostName[MAX_PATH]; gethostname(szHostName, MAX_PATH); hostent *hHost = gethostbyname(szHostName); if(hHost != NULL) m_StatusBar.SetText(TEXT("本机网络信息:")+CString(szHostName)+inet_ntoa (*(struct in_addr *)hHost->h_addr_list[0]), i++, 0); WSACleanup(); } //Creates a status bar control and attaches it to a CStatusBarCtrl object. //m_StatusBar.Create(WS_CHILD|WS_VISIBLE|SBT_OWNERDRAW, CRect(0,0,0,0), this, ID_STATUS_BAR); //CRect rt; //m_StatusBar.GetClientRect(&rt); //int nParts = 3; //int width[3] = {rt.Width()/nParts, rt.Width()/nParts,rt.Width()/nParts}; //int iPaneDim[3] = {rt.Width()/nParts, rt.Width()/nParts,-1}; //m_StatusBar.SetParts(3,iPaneDim); //m_StatusBar.SetText(_T("aaaaa"),0,0); //m_StatusBar.SetText(_T("bbbbb"),1,0); //m_StatusBar.SetText(_T("ccccc"),2,0); return TRUE; // 除非将焦点设置到控件,否则返回 TRUE } void CWinddowDemoDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialogEx::OnSysCommand(nID, lParam); } } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序, // 这将由框架自动完成。 void CWinddowDemoDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // 使图标在工作区矩形中居中 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; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialogEx::OnPaint(); } } //当用户拖动最小化窗口时系统调用此函数取得光标 //显示。 HCURSOR CWinddowDemoDlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); } void CWinddowDemoDlg::OnBnClickedOk() { //CDialogEx::OnOK(); } void CWinddowDemoDlg::OnBnClickedBtnNotifyIcon() { AddNotifyIcon(); } BOOL CWinddowDemoDlg::AddNotifyIcon(void) { CString str; //获取按钮标题 GetDlgItemText(IDC_BTN_NOTIFY_ICON, str); if(str.Find(TEXT("添加")) >=0 ) { str = TEXT("删除托盘图标"); SetDlgItemText(IDC_BTN_NOTIFY_ICON, str); return Shell_NotifyIcon(NIM_ADD, &m_IconData); } else { str = TEXT("添加托盘图标"); SetDlgItemText(IDC_BTN_NOTIFY_ICON, str); return Shell_NotifyIcon(NIM_DELETE, &m_IconData); } } afx_msg LRESULT CWinddowDemoDlg::OnNotifyicon(WPARAM wParam, LPARAM lParam) { switch (lParam) { case WM_RBUTTONUP: { //右键显示托盘菜单 CMenu menu; CPoint point; //获取当前点 GetCursorPos(&point); menu.LoadMenu(IDR_MENU1); //获取托盘菜单项 CMenu *pIconMenu = menu.GetSubMenu(0); //显示子菜单 pIconMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, this, NULL); menu.DestroyMenu(); } break; case WM_LBUTTONDOWN: { if (!IsWindowVisible()) { ShowWindow(SW_SHOW); } } break; default: break; } return 0; } void CWinddowDemoDlg::OnBnClickedBtnSwWindow() { CString str; //没有添加托盘不允许隐藏窗口 GetDlgItemText(IDC_BTN_NOTIFY_ICON, str); if(str.Find(TEXT("添加")) >=0 ) return; ShowWindow(SW_HIDE); } void CWinddowDemoDlg::OnNotifyIconShowHide() { if(IsWindowVisible()) ShowWindow(SW_HIDE); else ShowWindow(SW_SHOW); } void CWinddowDemoDlg::OnNotifyiconSet() { MessageBox(TEXT("还没有添加任何相关功能哦~"), TEXT("待续"), MB_OK | MB_ICONINFORMATION); } void CWinddowDemoDlg::OnNotifyiconExit() { if(!IsWindowVisible()) return; CString str; str = TEXT("添加托盘图标"); SetDlgItemText(IDC_BTN_NOTIFY_ICON, str); Shell_NotifyIcon(NIM_DELETE, &m_IconData); } void CWinddowDemoDlg::OnBnClickedBtnCliboard() { //剪贴板的监视必须响应OnDrawClipboard()函数,其他的可选 CString str; GetDlgItemText(IDC_BTN_CLIBOARD, str); if(str.Find(TEXT("不")) < 0) { //设置本窗口到剪贴板监视链,并保存下一窗口 m_hNextClipboard = SetClipboardViewer(); str = TEXT("不监视剪贴板"); SetDlgItemText(IDC_BTN_CLIBOARD, str); } else { str = TEXT("监视剪贴板"); SetDlgItemText(IDC_BTN_CLIBOARD, str); //删除本窗口的监视 ChangeClipboardChain(m_hNextClipboard); m_hNextClipboard = NULL; } } //接铁板链变化 void CWinddowDemoDlg::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter) { CDialogEx::OnChangeCbChain(hWndRemove, hWndAfter); if(hWndRemove == m_hNextClipboard) m_hNextClipboard = hWndAfter; } //剪贴板内容变化响应函数 void CWinddowDemoDlg::OnDrawClipboard() { CDialogEx::OnDrawClipboard(); CString strInfo; unsigned int anFormats[] = {CF_TEXT}; unsigned int nFormat = GetPriorityClipboardFormat(anFormats,sizeof(anFormats)); //剪贴板内容为文本 if(nFormat == CF_UNICODETEXT) { HGLOBAL hMem; OpenClipboard(); if(hMem = ::GetClipboardData(CF_TEXT)) { LPWSTR lpszText = (LPWSTR)GlobalLock(hMem); GlobalUnlock(hMem); strInfo += TEXT("剪贴板内容:\r\n"); strInfo += lpszText; strInfo += TEXT("\r\n"); } } else if(nFormat == CF_TEXT) { HGLOBAL hMem; OpenClipboard(); if(hMem = ::GetClipboardData(CF_TEXT)) { LPSTR lpszText = (LPSTR)GlobalLock(hMem); GlobalUnlock(hMem); strInfo += TEXT("剪贴板内容:\r\n"); strInfo += lpszText; strInfo += TEXT("\r\n"); } } else { strInfo += TEXT("非文本数据!\r\n"); } ////清楚剪贴板内容 //EmptyClipboard(); ////设置剪贴板内容 ////写入数据 //HGLOBAL hMen; //TCHAR strText[256] = TEXT("clipboard changed by WindowDemo & IT05"); //// 分配全局内存 //hMen = GlobalAlloc(GMEM_MOVEABLE, ((_tclen(strText)+1)*sizeof(TCHAR))); //// 把数据拷贝考全局内存中 //// 锁住内存区 //LPSTR lpStr = (LPSTR)GlobalLock(hMen); //// 内存复制 //memcpy(lpStr, strText, ((_tclen(strText)+1)*sizeof(TCHAR))); //// 字符结束符 //lpStr[_tclen(strText)] = (TCHAR)0; //// 释放锁 //GlobalUnlock(hMen); //SetClipboardData(CF_TEXT, hMen); //显示信息 m_info.SetWindowText(strInfo); CloseClipboard(); } void CWinddowDemoDlg::OnBnClickedBtnShortCut() { TCHAR szPath[MAX_PATH]; //获取当前路径 GetModuleFileName(NULL, szPath, MAX_PATH); //创建快捷方式 CreateDesktopShotCut(TEXT("WindowDemo的快捷方式"), szPath); } BOOL CWinddowDemoDlg::CreateDesktopShotCut(CString strName, CString strSourcePath) { //初始化com if(FAILED(CoInitialize(NULL))) return FALSE; int i; TCHAR Path[MAX_PATH+1]; CString strDestDir; i=CSIDL_DESKTOPDIRECTORY; LPITEMIDLIST pidl; LPMALLOC pShell; if(SUCCEEDED(SHGetMalloc(&pShell))) { if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,i,&pidl))) { if(!SHGetPathFromIDList(pidl,Path)) { pShell->Free(pidl); ::CoUninitialize(); return FALSE; } pShell->Release(); strDestDir = Path; strDestDir += TEXT("\\"); //设置桌面快捷方式的名字 strDestDir += strName; strDestDir += ".lnk"; IShellLink* psl; if(SUCCEEDED(CoCreateInstance (CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(LPVOID*)&psl))) { //设置快捷方式的目标位置 psl->SetPath(strSourcePath); //比如目标位置为C:\windows\a.txt 起始位置就应该设置 //为C:\windows否则会导致不可预料的错误 //如果是文件夹的快捷方式起始位置和目标位置可以设置为一样 //设置快捷方式的起始位置 psl->SetWorkingDirectory(strSourcePath); IPersistFile* ppf; if(SUCCEEDED(psl->QueryInterface(IID_IPersistFile,(LPVOID*)&ppf))) { TCHAR wsz[MAX_PATH]; //设置桌面快捷方式的名字 //MultiByteToWideChar(CP_THREAD_ACP,MB_PRECOMPOSED,strDestDir,-1,wsz,MAX_PATH); _tcscpy_s(wsz, sizeof(wsz)/sizeof(TCHAR), strDestDir); if(SUCCEEDED(ppf->Save(wsz,TRUE)))//保存快捷方式到桌面 { ppf->Release(); psl->Release(); ::CoUninitialize(); return TRUE; } else { ppf->Release(); psl->Release(); ::CoUninitialize(); return FALSE; } } else { ppf->Release(); psl->Release(); ::CoUninitialize(); return FALSE; } } else { ::CoUninitialize(); return FALSE; } } else { ::CoUninitialize(); return FALSE; } } else { ::CoUninitialize(); return FALSE; } } void CWinddowDemoDlg::OnFileOpen() { CFileDialog fp(TRUE); fp.DoModal(); } void CWinddowDemoDlg::OnExit() { exit(0); } void CWinddowDemoDlg::OnAbout() { CAboutDlg dlg; dlg.DoModal(); }WindowDemoDlg.h文件
// WinddowDemoDlg.h : 头文件 // #pragma once #include "afxwin.h" //宏定义托盘消息 #define WM_NOTIFYICON (WM_USER+0X101) #define ID_STATUS_BAR 0X102 // CWinddowDemoDlg 对话框 class CWinddowDemoDlg : public CDialogEx { // 构造 public: CWinddowDemoDlg(CWnd* pParent = NULL); // 标准构造函数 // 对话框数据 enum { IDD = IDD_WINDDOWDEMO_DIALOG }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 //变量定义 private: NOTIFYICONDATA m_IconData; CStatusBarCtrl m_StatusBar; // 实现 protected: HICON m_hIcon; HWND m_hNextClipboard; // 生成的消息映射函数 virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); DECLARE_MESSAGE_MAP() public: afx_msg void OnBnClickedOk(); CButton m_notify_icon; CEdit m_info; afx_msg void OnBnClickedBtnNotifyIcon(); BOOL AddNotifyIcon(void); protected: afx_msg LRESULT OnNotifyicon(WPARAM wParam, LPARAM lParam); public: afx_msg void OnBnClickedBtnSwWindow(); afx_msg void OnNotifyIconShowHide(); afx_msg void OnNotifyiconSet(); afx_msg void OnNotifyiconExit(); afx_msg void OnBnClickedBtnCliboard(); afx_msg void OnChangeCbChain(HWND hWndRemove, HWND hWndAfter); afx_msg void OnDrawClipboard(); afx_msg void OnBnClickedBtnShortCut(); BOOL CreateDesktopShotCut( CString strName, CString strSourcePath ); CMenu m_menu; afx_msg void OnFileOpen(); afx_msg void OnExit(); afx_msg void OnAbout(); };
种子地址:http://download.csdn.net/detail/jiejiaozhufu/4950226