新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     >>计算机科学论坛<<     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论高级C/C++编程、代码重构(Refactoring)、极限编程(XP)、泛型编程等话题
    [返回] 计算机科学论坛计算机技术与应用『 C/C++编程思想 』 → 常用的编程技巧! 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 5177 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 常用的编程技巧! 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客楼主
    发贴心情 常用的编程技巧!

    纯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);


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/6/29 11:12:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客2
    发贴心情 
    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

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2009/6/29 11:13:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 C/C++编程思想 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/4 0:46:13

    本主题贴数2,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    474.854ms