2006年12月30日星期六

给对话框中的CTreeCtrl控件添加右键菜单

给对话框中的CTreeCtrl控件添加右键菜单,有几种方法,一种是从CTreeCtrl派生一个类,在这个类的WM_RBUTTONDOWN中弹出右键菜单,但这样做会增加类。另一个方法就是在对话框的WM_CONTEXTMENU消息中,弹出右键菜单:

void TreeTestDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
    if (pWnd == &m_treeCtrl) {
        UINT flags;
        m_treeCtrl.ScreenToClient(&point);
        HTREEITEM hItem = m_treeCtrl.HitTest(point, &flags);
        if (hItem) {
            m_treeCtrl.SelectItem(hItem);
            showTreePopMenu(point, hItem);
        }
    }
}

其中,showTreePopMenu,用来弹出右键菜单:
void TreeTestDlg::showTreePopMenu(CPoint& point, HTREEITEM hItem)
{
    CMenu menu;
    CMenu* pMenu = NULL;
    menu.LoadMenu(IDR_TREE_MENU);
    pMenu = menu.GetSubMenu(0); // 0表示取第一个子菜单
    if (!pMenu) return;
    GetCursorPos(&point);
    pMenu->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);
}

如何让搜索引擎收录我的站点

方法一:主动向搜索引擎提交自己的网址
向百度、Google、Yahoo提交网址是不用花钱的,其提交页面分别为:
http://www.baidu.com/search/url_submit.html
http://www.google.com/intl/zh-CN/add_url.html
http://www.yisou.com/search_submit.html?source=yisou_www_hp

方法二:和同类且已被收录的网站做链接
这 个很简单了,如果你的网站刚刚建成,而朋友的网站已经被几个搜索引擎收录了,那么就和他交换一下 首页链接,这样,下次搜索引擎抓取他网站上的内容的时候就会“顺便”发现你的网站(起到跳板的作用),并予以收录。需要注意的是,这种友情链接应以文本链 接和logo的形式存在,而不要采用图片热点或flash的形式。另外就是回避那种通过CGI程序管理友情链接的网站,这种网站通常把链接存在数据库中, 随意排序、变换位置,导致搜索引擎无法正常的找到你的网址。

方法三:合理、合法的对网站进行SEO(搜索引擎优化)
关于网站优化方面的文章网上很多,不过很可惜,良莠不齐,新旧不分,同时因为SEO是一项经验、技术并重的业务,所以别人也很难或不愿意把自己掌握的东西告诉你,因此,只能靠自己分辨识别了。关于这部分,请查看页面地址 http://www.dfm369.com/20050925/20050512001.html

纯资源DLL的编写

纯资源的DLL就是只包含资源的DLL,例如:图标,位图,字符串,声音,视频,对话框等。使用纯资源DLL可以节约可执行文件的大小,可以被所有的应用程序所共享,从而提高系统性能。纯资源DLL的编写比普通的DLL要简单的多:
1) 创建一个WIN32 DLL工程,不是MFC的DLL
2) 创建一个资源文件 *.RC,添加到资源DLL的工程中去
3) 添加一个初始化DLL的原文件:

#include "windows.h"
extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID)
{
return 1;
}

这是纯资源DLL所必须需的代码,保存这个文件为*.CPP。编译这个资源DLL。

在应用程序显示的调用这个DLL,使用LoadLibrary函数装入资源DLL,FindResource和LoadResource来装入各种资源,或者使用下列的特定的资源装入函数:
FormatMessage
LoadAccelerators
LoadBitmap
LoadCursor
LoadIcon
LoadMenu
LoadString
当资源使用结束,应用程序须调用FreeLibrary函数来释放资源。

调用编写好的资源DLL的步骤如下:
首先在应用程序中声明一个DLL的句柄,HINSTANCE m_hLibrary;在OnCreate( )
函数中调用LoadLirbrary( ),在OnDestory( )中调用FreeLibrary()。

2006年12月28日星期四

使用c/c++判断文件是否存在

// Test whether a file is exist or not
bool fileExists(const char* fileName)
{
    if (!fileName) return false;


#ifdef WIN32
    struct _stat buf;
    int result;

    result = _stat(fileName, &buf);

    if (result) {
        return false;
    }

    return (buf.st_mode & _S_IFMT) == _S_IFREG;
#else
    struct stat buf;
    int result;

    result = stat(fileName, &buf);

    if (result) {
        return false;
    }

    return (buf.st_mode & S_IFMT) == S_IFREG;
#endif
}

Win32 API SHFileOperation

The win32 API SHFileOperation can be used to delete a whole directory or copy a directory from a path to another, below is an example:

// Delete the whole directory
BOOL DelDir(LPCTSTR lpszPath)
{
    char fPath[MAX_PATH];
    memset(fPath, 0, sizeof(fPath));

    strcpy(fPath, lpszPath);

    // Note : 目录不能以\或/结尾
    for (int i = strlen(fPath) - 1; i >= 0; i--) {
        if (fPath[i] == '\\' || fPath[i] == '/') {
            fPath[i] = 0;
        } else {
            break;
        }
    }

    SHFILEOPSTRUCT FileOp;
    FileOp.fFlags = FOF_NOCONFIRMATION;
    FileOp.hNameMappings = NULL;
    FileOp.hwnd = NULL;
    FileOp.lpszProgressTitle = NULL;
    FileOp.pFrom = fPath;
    FileOp.pTo = NULL;
    FileOp.wFunc = FO_DELETE;
    return SHFileOperation(&FileOp) == 0;
}

// Copy Directory or file, 注意字符串fromPath与toPath必须以"\0\0"结尾,
// 且目录的文件分割符是'\',不是'/'
BOOL copy(LPCTSTR fromPath, LPCTSTR toPath)
{
    SHFILEOPSTRUCT FileOp;
    FileOp.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
    FileOp.hNameMappings = NULL;
    FileOp.hwnd = NULL;
    FileOp.lpszProgressTitle = NULL;
    FileOp.pFrom = fromPath;
    FileOp.pTo = toPath;
    FileOp.wFunc = FO_COPY;
    return SHFileOperation(&FileOp) == 0;
}

2006年12月25日星期一

Use Blit in wxWindows

wxDC::Blit is used to copy from a source DC to this DC, and it can be used to erase a shape but no affect the backgroud. This is very useful in case you need to erase a shape but not want to invoke Refresh or Invalidate since such methods may bring flickering. Below is an example:

wxMemoryDC memDC;

// Draw an ellipse as background
dc.SetBrush(wxBrush(wxColour(255, 0, 0)));
dc.DrawEllipse(10, 10, 100, 100);

// Draw a line
wxBitmap bmp(150, 1);
memDC.SelectObject(bmp);
dc.Blit(5, 60, 150, 1, &memDC, 0, 0, wxEQUIV);

// Erase the line
dc.Blit(5, 60, 150, 1, &memDC, 0, 0, wxINVERT);

2006年12月18日星期一

Use select as "sleep"

The function "select" can be used as a "timer", and it is more accurate than "sleep". And in most program, select but not sleep is used. Below is a example:

#include "sys/time.h"
#include "unistd.h"
#include "stdio.h"

int main(void)
{
  timeval tv;
  timeval tv0;
  timeval tv1;
  float timeuse;

  tv.tv_sec = 10;
  tv.tv_usec = 0;

  gettimeofday(&tv0, NULL);
  // Here select is same as "sleep(10)",
  // but more accurate than sleep

  select(0, NULL, NULL, NULL, &tv);
  gettimeofday(&tv1, NULL);

  timeuse = 1000000*(tv0.tv_sec - tv1.tv_sec) +
    tv0.tv_usec - tv1.tv_usec;
  timeuse /= 1000000;
  printf("The time used is %f\n", timeuse);
}

Exectue External Command in Python

"""This program test how to invoke command"""

import os
os.system("ls -l")

Use SVN Client API in VC

1) First, download the following libs:

svn-win32-1.4.2_dev.zip

subversion-deps-1.4.2.zip

svn-win32-libintl.zip

db-4.4.20-win32.zip

2) Second, add the following libs as link lib

libsvn_client-1.lib libsvn_delta-1.lib libsvn_diff-1.lib libsvn_fs-1.lib libsvn_fs_base-1.lib libsvn_fs_fs-1.lib libsvn_ra-1.lib libsvn_ra_dav-1.lib libsvn_ra_local-1.lib libsvn_ra_svn-1.lib libsvn_repos-1.lib libsvn_subr-1.lib libsvn_wc-1.lib libapr.lib libaprutil.lib xml.lib libneon.lib intl3_svn.lib libdb44s.lib WS2_32.Lib shfolder.lib

Note: the shfolder.lib is a must, or will get the following errors in link time:
libsvn_subr-1.lib(config_win.obj) : error LNK2001: unresolved external symbol __imp__SHGetFolderPathA@20  
libsvn_subr-1.lib(config_win.obj) : error LNK2001: unresolved external symbol __imp__SHGetFolderPathW@20 
Debug/mini_client.exe : fatal error LNK1120: 2 unresolved externals

3) Third, click the "project->setting..." menu item, and in the C/C++ tab, select "code generation" Category, and set "Use runtime library as "Debug Multithreaded Dll" or "Multithreaded Dll"。or will get the following errors in link time:
Linking...  
MSVCRT.lib(MSVCRT.dll) : error LNK2005: _strncmp already defined in LIBC.lib(strncmp.obj)  
MSVCRT.lib(MSVCRT.dll) : error LNK2005: _free already defined in LIBC.lib(free.obj)  
MSVCRT.lib(MSVCRT.dll) : error LNK2005: _strchr already defined in LIBC.lib(strchr.obj)  
MSVCRT.lib(MSVCRT.dll) : error LNK2005: _calloc already defined in LIBC.lib(calloc.obj) 
MSVCRT.lib(MSVCRT.dll) : error LNK2005: _malloc already defined in LIBC.lib(malloc.obj)  
MSVCRT.lib(MSVCRT.dll) : error LNK2005: __close already defined in LIBC.lib(close.obj) 
…...
MSVCRT.lib(MSVCRT.dll) : error LNK2005: __write already defined in LIBC.lib(write.obj) 
LINK : warning LNK4098: defaultlib "MSVCRT" conflicts with use of other libs; use /NODEFAULTLIB:library  
Debug/mini_client.exe : fatal error LNK1169: one or more multiply defined symbols found  
Error executing link.exe.

4) Finally, Download minimal_client.c from http://svn.collab.net/repos/svn/tags/1.3.2/tools/examples/, it is a example described how to use svn client api.