编程技巧搜集(1)

0.纯SDK实现的托盘类
/**********************************************************************
* File Name: TrayIcon.cpp
* Module Name: CTrayIcon
* Copyright (C) by LQ messager
* Description: System taskbar tray icon control
* Author: LiJun
* Create Data: 2005-11-18  Update Data: 2006-11-16
* E-Mail: mailto:notoldtree@126.com
*
* NOTE: WIN32 SDK edition
*********************************************************************/
#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);

5.获得汉字字符串拼音首字母
#include "stdio.h"
#include "string.h"
#define TBYTE unsigned char

void FirstLetter(int nCode, char& strRet);
void GetFirstLetter(char* strName, char* strFirstLetter)
{
    TBYTE ucHigh, ucLow;
    int  nCode;
    char strRet;
 memset(strFirstLetter, 0, sizeof(strFirstLetter));
 int i;
 int len = 0;
    for (i=0; i < (int)strlen(strName); i++)
    {
        if ((TBYTE)strName[i] < 0x80)
            continue;
  
        ucHigh = (TBYTE)strName[i];
        ucLow  = (TBYTE)strName[i+1];
        if ( ucHigh < 0xa1 || ucLow < 0xa1)
            continue;
        else
            // Treat code by section-position as an int type parameter,
            // so make following change to nCode.
            nCode = (ucHigh - 0xa0) * 100 + ucLow - 0xa0;
  
        FirstLetter(nCode, strRet);
  strFirstLetter[len] = strRet;
        i++;
  len++;
    }
 strFirstLetter[len] = 0;
}

void FirstLetter(int nCode, char& strLetter)
{
 if(nCode >= 1601 && nCode < 1637) strLetter = 'A';
 if(nCode >= 1637 && nCode < 1833) strLetter = 'B';
 if(nCode >= 1833 && nCode < 2078) strLetter = 'C';
 if(nCode >= 2078 && nCode < 2274) strLetter = 'D';
 if(nCode >= 2274 && nCode < 2302) strLetter = 'E';
 if(nCode >= 2302 && nCode < 2433) strLetter = 'F';
 if(nCode >= 2433 && nCode < 2594) strLetter = 'G';
 if(nCode >= 2594 && nCode < 2787) strLetter = 'H';
 if(nCode >= 2787 && nCode < 3106) strLetter = 'J';
 if(nCode >= 3106 && nCode < 3212) strLetter = 'K';
 if(nCode >= 3212 && nCode < 3472) strLetter = 'L';
 if(nCode >= 3472 && nCode < 3635) strLetter = 'M';
 if(nCode >= 3635 && nCode < 3722) strLetter = 'N';
 if(nCode >= 3722 && nCode < 3730) strLetter = 'O';
 if(nCode >= 3730 && nCode < 3858) strLetter = 'P';
 if(nCode >= 3858 && nCode < 4027) strLetter = 'Q';
 if(nCode >= 4027 && nCode < 4086) strLetter = 'R';
 if(nCode >= 4086 && nCode < 4390) strLetter = 'S';
 if(nCode >= 4390 && nCode < 4558) strLetter = 'T';
 if(nCode >= 4558 && nCode < 4684) strLetter = 'W';
 if(nCode >= 4684 && nCode < 4925) strLetter = 'X';
 if(nCode >= 4925 && nCode < 5249) strLetter = 'Y';
 if(nCode >= 5249 && nCode < 5590) strLetter = 'Z';
}

void main()
{
 char strName[10], strRes[5];
    strcpy(strName, "汗!瀑布汗!");
 memset(strRes, 0, sizeof(strRes));
    GetFirstLetter(strName, strRes);
 printf("%s/n", strRes);
}

6.消除窗口子控件在窗体大小改变时的闪烁
在create窗口的时候类型加上WS_CLIPCHILDREN

7.读取大于2G的文件
__int64 myFileSeek (HANDLE hf, __int64 distance, DWORD MoveMethod)
{
   LARGE_INTEGER li;

   li.QuadPart = distance;

   li.LowPart = SetFilePointer (hf,
                                li.LowPart,
                                &li.HighPart,
                                MoveMethod);

   if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError()
       != NO_ERROR)
   {
      li.QuadPart = -1;
   }

   return li.QuadPart;
}

8.操作控制台
GetConsoleScreenBufferInfo
 返回窗口大小、屏幕缓冲区大小和颜色属性
 
SetConsoleWindowInfo
 改变控制台窗口的大小
 
SetConsoleScreenBufferSize
 该表控制台屏幕缓冲区的大小
 
SetConsoleTextAttribute
 设置颜色属性
 
SetConsoleTitle
 设置控制台窗口的标题
 
GetConsoleTitle
 获得控制窗口的标题
 

进程可以使用 FreeConsole 函数来分离继承的控制台或通过 AllocConsole 创建的控制台。

9.清空回收站
SHEmptyRecycleBin(NULL, NULL,SHERB_NOCONFIRMATION |SHERB_NOPROGRESSUI | SHERB_NOSOUND);

10.获得一段时间内cpu的晶振次数
在Intel   Pentium以上级别的CPU中,有一个称为“时间戳(Time   Stamp)”的部件,它以64位无符号整型数的格式,记录了自CPU上电以来所经过的时钟周期数。
inline   unsigned   __int64   GetCycleCount()  
{  
    __asm   RDTSC  
}

11.提高应用程序权限
windows应用程序好像不能提高自身的权限,但它有一个未公开的函数:CreateProcessWithLogonW,它可以用administrator身份运行一个进程
CreateProcessWithLogonW API的定义如下:
BOOL CreateProcessWithLogonW(
LPCWSTR , // 用户乙的账号(Account)
LPCWSTR , //用户乙的域(Domain)
LPCWSTR , // 用户乙的密码(Password)
DWORD , // logon option
LPCWSTR , // executable module name
LPWSTR , // command-line string
DWORD , // creation flags
LPVOID , // new environment block
LPCWSTR , // current directory name
LPSTARTUPINFOW , // startup information
LPPROCESS_INFORMATION // process information
);
另外,你也可用命令行的:runas
如:  
  runas   /env   /user:user@domain.microsoft.com   "notepad   /"my   file.txt/""  

=================================================================
有关 CreateProcessWithLogonW 参考:
http://blog.csdn.net/wxjgeorge/archive/2005/04/15/349601.aspx

12.枚举一个进程中已经打开的文件句柄
调用ntdll.dll的微软没有公布的函数
DWORD NtQuerySystemInformation( DWORD dwRecordType,
                 PDWORD pdwHandleList,
                 DWORD dwNumBytes,
                 PDWORD pdwNumBytesRet );

可以返回所有的句柄表,然后根据句柄类型过滤出文件句柄
1 NtQuerySystemInformation
2 NtQueryObject

13.
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值