2008年10月11日星期六

找出執行程式的檔案路徑 (Linux 和 Windows 版本)

#include "string.h"
#include "stdio.h"

#ifndef __WIN32__
#include "unistd.h"
#include "sys/types.h"
#endif

char getApplicationPath(char *pPath, int nMaxPathSize)
{
char proc_path[20], buf[256], *lastchar;
int len;

#ifdef __WIN32__
len = GetModuleFileName(NULL, buf, 256);

if(len == 0) {
// GetModuleFileName error
return -1;
}

if((len >= 256) || (len >= nMaxPathSize)) {
//insufficient size
return -2;
}

// terminate the returned string first
buf[len] = 0;

// then remove the application name
lastchar = strrchr(buf, '\\');
#else
sprintf(proc_path, "/proc/%d/exe", getpid());
len = readlink(proc_path, buf, 256);

if(ret < 0) {
// readlink error
perror("readlink");
return -1;
}

if((len >= 256) || (len >= nMaxPathSize)) {
// insufficient size
return -2;
}

// terminate the returned string first
buf[len] = 0;

// then remove the application name
lastchar = strrchr(buf, '/');
#endif

if(lastchar != NULL) {
*lastchar = 0;
} else {
// application name not found
return -3;
}

strcpy(pPath, buf);

// success
return 0;
}

2008年10月10日星期五

wxWidgets 的 StretchBlt 方案

如果用過 Win32 SDK 的人都知道有一個 StretchBlt() function 可以將原有的 bitmap 放大或縮小再顯示出來. 可是在 wxWidgets 卻沒有此功能. 如果要在 wxWidgets 做出類似功能, 我們可以:

1. 將原來的 wxBitmap object 用 wxBitmap::ConvertToImage() 轉為 wxImage object
2. 用 wxImage::Rescale() 改變大小
3. 再用新的 wxImage object 產生新的 wxBitmap object
4. 用 wxDC::DrawBitmap() 顯示新的 wxBitmap object