-- 作者:卷积内核
-- 发布时间:6/29/2009 11:12:00 AM
-- 常用的编程技巧!
纯SDK实现的托盘类 #include "StdAfx.h" #include <shellapi.h> #pragma comment(lib, "shell32.lib") #pragma once class CTrayIcon { public: CTrayIcon(); ~CTrayIcon(); // Set notify message window BOOL SetNotifyWnd(HWND hWnd, UINT nMessage); BOOL SetIcon(HICON hIcon);// Set tray icon BOOL SetIcon(UINT nID);// Set tray icon by resource HICON GetIcon() const;// Get tray icon handle BOOL SetToolTip(LPCTSTR lpszText);// Set tooltip text BOOL SetToolTip(UINT nID);// Set tooltip text by resource UINT GetToolTip(LPTSTR lpszText, int nLength) const;// Get tooltip text #if (_WIN32_IE >= 0x0500) BOOL SetBalloonTip(LPCTSTR lpszTitle, LPCTSTR lpszMsg, UINT nTimeout=3000); BOOL GetBalloonTip(LPTSTR lpszTitle, LPTSTR lpszMsg, UINT* pTimeout) const; #endif BOOL ShowTray(BOOL bShow); private: NOTIFYICONDATA m_nid; }; CTrayIcon::CTrayIcon() { memset(&m_nid, 0, sizeof(NOTIFYICONDATA)); m_nid.cbSize = sizeof(NOTIFYICONDATA); } CTrayIcon::~CTrayIcon() { ShowTray(FALSE); } BOOL CTrayIcon::SetNotifyWnd(HWND hWnd, UINT nMessage) { if(!::IsWindow(hWnd)) return FALSE; m_nid.hWnd = hWnd; if(nMessage > 0) { m_nid.uCallbackMessage = nMessage; if(!(m_nid.uFlags & NIF_MESSAGE)) m_nid.uFlags |= NIF_MESSAGE; } else { m_nid.uCallbackMessage = 0; if(m_nid.uFlags & NIF_MESSAGE) m_nid.uFlags &= ~NIF_MESSAGE; } return Shell_NotifyIcon(NIM_ADD, &m_nid); } BOOL CTrayIcon::SetIcon(HICON hIcon) { ASSERT(hIcon != NULL); if(!(m_nid.uFlags & NIF_ICON)) m_nid.uFlags |= NIF_ICON; m_nid.hIcon = hIcon; return Shell_NotifyIcon(NIM_MODIFY, &m_nid); } BOOL CTrayIcon::SetIcon(UINT nID) { return SetIcon(::LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(nID))); } HICON CTrayIcon::GetIcon() const { return m_nid.hIcon; } BOOL CTrayIcon::SetToolTip(LPCTSTR lpszText) { if(!(m_nid.uFlags & NIF_TIP)) m_nid.uFlags |= NIF_TIP; lstrcpy(m_nid.szTip, lpszText); return Shell_NotifyIcon(NIM_MODIFY,&m_nid); } BOOL CTrayIcon::SetToolTip(UINT nID) { TCHAR szText[128]; ::LoadString(GetModuleHandle(NULL), nID, szText, 128); return SetToolTip(szText); } UINT CTrayIcon::GetToolTip(LPTSTR lpszText, int nLength) const { lstrcpy(lpszText, m_nid.szTip); return lstrlen(lpszText); } #if (_WIN32_IE >= 0x0500) BOOL CTrayIcon::SetBalloonTip(LPCTSTR lpszTitle, LPCTSTR lpszMsg, UINT nTimeout/*=3000*/); { if(!(m_nid.uFlags & NIF_INFO)) m_nid.uFlags |= NIF_INFO; m_nid.uTimeout = nTimeout; lstrcpy(m_nid.szInfoTitle, lpszTitle); lstrcpy(m_nid.szInfo, lpszMsg); return Shell_NotifyIcon(NIM_MODIFY, &m_nid); } #endif #if (_WIN32_IE >= 0x0500) BOOL CTrayIcon::GetBalloonTip(LPTSTR lpszTitle, LPTSTR lpszMsg, UINT* pTimeout) const { lstrcpy(lpszTitle, m_nid.szInfoTitle); lstrcpy(lpszMsg, m_nid.szInfo); *pTimeout = m_nid.uTimeout; return TRUE; } #endif BOOL CTrayIcon::ShowTray(BOOL bShow) { return Shell_NotifyIcon( bShow ? NIM_ADD : NIM_DELETE, &m_nid ); } 1.改变CToolTipCtrl提示信息时间 SendMessage(hWndTooltip,TTM_SETDELAYTIME,(WPARAM)(DWORD)TTDT_INITIAL,(LPARAM)(INT) MAKELONG(250,0)); 2.判断操作系统版本 //console方式编译运行 #include <windows.h> #include <stdio.h> BOOL DisplaySystemVersion() { OSVERSIONINFOEX osvi; BOOL bOsVersionInfoEx; // Try calling GetVersionEx using the OSVERSIONINFOEX structure. // // If that fails, try using the OSVERSIONINFO structure. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) ) { // If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO. osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) return FALSE; } switch (osvi.dwPlatformId) { case VER_PLATFORM_WIN32_NT: // Test for the product. if ( osvi.dwMajorVersion <= 4 ) printf("Microsoft Windows NT "); if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) printf ("Microsoft Windows 2000 "); if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) printf ("Microsoft Windows XP "); // Test for product type. if( bOsVersionInfoEx ) { if ( osvi.wProductType == VER_NT_WORKSTATION ) { if( osvi.wSuiteMask & VER_SUITE_PERSONAL ) printf ( "Personal " ); else printf ( "Professional " ); } else if ( osvi.wProductType == VER_NT_SERVER ) { if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) printf ( "DataCenter Server " ); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) printf ( "Advanced Server " ); else printf ( "Server " );} } else { HKEY hKey; char szProductType[80]; DWORD dwBufLen; RegOpenKeyEx( HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Control\\ProductOptions",0, KEY_QUERY_VALUE, &hKey ); RegQueryValueEx( hKey, "ProductType", NULL, NULL,(LPBYTE) szProductType, &dwBufLen); RegCloseKey( hKey ); if ( lstrcmpi( "WINNT", szProductType) == 0 ) printf( "Professional " ); if ( lstrcmpi( "LANMANNT", szProductType) == 0 ) printf( "Server " ); if ( lstrcmpi( "SERVERNT", szProductType) == 0 ) printf( "Advanced Server " ); } // Display version, service pack (if any), and build number. if ( osvi.dwMajorVersion <= 4 ) { printf ("version %d.%d %s (Build %d)\n", osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); } else { printf ("%s (Build %d)\n", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); } break; case VER_PLATFORM_WIN32_WINDOWS: if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) { printf ("Microsoft Windows 95 "); if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' ) printf("OSR2 " ); } if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) { printf ("Microsoft Windows 98 "); if ( osvi.szCSDVersion[1] == 'A' ) printf("SE " ); } if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) { printf ("Microsoft Windows Me "); } break; case VER_PLATFORM_WIN32s: printf ("Microsoft Win32s "); break; } return TRUE; } int main() { DisplaySystemVersion(); } 3.更改系统日期格式 char buff[]="yyyy-MM-dd"; SetLocaleInfo(LOCALE_SYSTEM_DEFAULT,LOCALE_SSHORTDATE ,buff); 4.检测电脑最后一次输入 BOOL GetLastInputInfo( PLASTINPUTINFO plii);
|