Sunday, May 4, 2008

Change color of CStatic object.

.h file
(add this function to your class)
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
// Called to change color of text
.cpp file
(add this to your message map)
ON_WM_CTLCOLOR()
(add this to the file too)
HBRUSH CYourDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
// Call original functions
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

// Create a blue color
COLORREF textColor = RGB(0, 0, 255);

// Set text color
if (pWnd->GetDlgCtrlID() == yourstatic)
{
pDC->SetTextColor(textColor);

}

// Return result of original function
return hbr;

}

Tuesday, April 22, 2008

麻烦的OCCI

在Windows环境下用OCCI编程真是一件很麻烦的事情,尽管到目前为止,我还没有在UNIX环境下测试过OCCI,数据库厂商(尤其是这种大型数据库)对于UNIX环境的重视和支持程度要远远超过了Windows环境,料想不会有太大的问题。

我用的环境是Windows XP SP2 5.1.2600,Oracle 10g 10.2.0.1.0,Visual C++ 2005(VS8)。按照惯例,在VS8里面添加了include路径(通常是%ORACLE_HOME%\oci\include),以及新的链接路径(通常是%ORACLE_HOME%\oci\lib)。编译通过、链接通过,运行时出现异常,通过try - catch机制捕获到的异常为“ORA-24960: 属性OCI_ATTR_USERNAME的长度大于最大允许长度255。”在各个论坛上以及OTN上对这个问题讨论的很多,感觉很大程度上这个问题似乎与VC++和Oracle的版本匹配有关。于是从OTN(Oracle C++ Call Interface - Downloads)上可以下载到用于Visual C++ 2005(VS8)上的相关库文件,并且按照安装说明把这些库文件解压缩在%ORACLE_HOME%\oci\lib\MSVC\vc8目录中,重新编译通过、链接通过,运行时出现了同样的问题(ORA-24960: 属性OCI_ATTR_USERNAME的长度大于最大允许长度255。)

此时注意到安装文件中提示在DEBUG编译模式中要使用oraocci10d.lib而不是oraocci10.lib作为链接库。调整以后出现了新的问题,首先是说找不到oraocci10d.dll,把该目录加入环境变量PATH之后(这时要注意一点,必须重新启动VS,系统的环境变量才会生效,换句话说,VS只在开始运行时检查一下环境变量。),又说找不到MSVCR80D.dll。而此时注意到Release版本的编译已经没有问题了,但是用Release版本会给调试过程带来很多麻烦,很不甘心。

如果直接把$(VCInstallDir)VC\redist\Debug_NonRedist\x86\Microsoft.VC80.DebugCRT加入PATH,编译链接通过,报错没有通过manifest加载动态链接库,于是耐心地阅读了MSDN相关文章,将Porject->Properties中的Configuration Properties\C/C++\Code Generation\Runtime Library设置为/MDd (Multi-threaded Debug DLL),出现新错误,/MDd选项与静态链接MFC库选项矛盾,于是在stdafx.h中添加“#define _AFXDLL”,最终所有问题得到解决。

所以尽管经过很多尝试,过程极其复杂,但结论总结起来十分简单:
1、不要选择使用静态链接MFC;
2、从OTN(Oracle C++ Call Interface - Downloads)下载VC8的OCCI库文件;
3、将OCCI库文件安装到%ORACLE_HOME%\oci\lib\MSVC\vc8目录中,将该目录加入环境变量;
4、Debug编译版本中选择oraocci10d.lib作为链接库文件,并使用/MDd编译选项。Release版本中选择oraocci.lib作为链接库文件,并使用/MD编译选项。

Sunday, April 20, 2008

OCCI with Visual Studio 2005

只写了一句:
Environment *env;

编译错误:
1>d:\documents\visual studio 2005\projects\oci_test\oci_test\oci_testdlg.h(16) : error C2143: syntax error : missing ';' before '*'
1>d:\documents\visual studio 2005\projects\oci_test\oci_test\oci_testdlg.h(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\documents\visual studio 2005\projects\oci_test\oci_test\oci_testdlg.h(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

解决方法:
using namespace oracle::occi;

Friday, April 18, 2008

回到Firefox

Apple的Safari 3.1不仅有了Windows版本,而且号称“世界上最快的浏览器”。它的出现让我有了些动摇,然而试用了不到一周,我就又回到了熟悉的Firefox。Safari的界面很酷,但是不仅不快,而且内存占用高达200~300M,许多插件无法使用,更无法容忍的是Bug太多!Apple,盛名之下,其实难副。

Sunday, April 6, 2008

Using GDI+ in MFC Applications

1. In Visual C++ 2005 go to Project->Properties, Type gdiplus.lib into the Configuration Properties/Linker/Input/Additional Dependencies.
2. Add the following line to stdafx.h:
#include <gdiplus.h>

using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
3. Intialize the GDI+ resources. Add this to your CWinApp derived class as member:
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
At InitInstance(), add:
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

// note: GdiplusStartup() should be added before the
// following sentence:
CWinApp::InitInstance();
4. Your application is ready to consume GDI+ now.
5. Upon exit, release GDI+ resources. Add the following line to ExitInstance():
GdiplusShutdown(gdiplusToken);

Thursday, April 3, 2008

Pidgin


Pidgin是一个能同时支持多个IM协议的即时通讯软件客户端,即可以使用它来同时登陆MSN、Yahoo!、Google Talk、ICQ、Sametime 等的即时通讯服务,而且可以同时登陆多个同类型的帐号。Pidgin的前身是鼎鼎大名的Gaim,它一直是Linux系统中最受欢迎的IM客户端,Pidgin不仅有Linux版,而且还有Windows版,所以Windows的用户同样可以享用这个很棒的、免费的、而且没有任何广告的软件。

使用中文版Pidgin

在Windows中第一次启动Pidgin的时候,会发现显示的都是英文,而且找不到设置中文的地方,但只要简单地配置一下,就能把中文显示出来。
  1. 设置环境变量:PIDGINLANG=zh_CN
  2. 运行regedit进入Windows注册表,找到HKEY_CURRENT_USER\Software\pidgin,将Installer Language键设置为2052即可(通常初始值为1033)。
解决Pidgin中文显示乱码问题

在使用中,有可能会发现中文显示成乱码的现象,可以下载gtkrc.zh_CN.utf-8,将它解压后放在x:Program Files\Common Files\GTK\2.0\etc\gtk-2.0文件夹下即可。而如果收到的消息是乱码,只要在主窗口菜单“工具”->“首选项”对话框中选中“对话”选项卡,取消“收到的消息显示格式”选项就可以了。

关于用Pidgin登陆QQ

Pidgin登录QQ时,有时会遇到中文输入变慢,甚至整个系统都会变慢,接收消息时更是显得系统接近崩溃。虽然这种情况并不是每次都会发生,但对于QQ爱好者无疑是一个噩梦。幸好我已经很久不用QQ了。

Pidgin 更新的速度还是很快的,所以以上的问题也许很快就会随着新版本的推出而消失,以后使用pidgin会变得更加容易。

Monday, March 31, 2008

Constructor Initialization List

Look at this piece of code, is it strange to you?
class A
{
private:
size_t i_;

public:
A( size_t i ): i_( i ) { };
};
Does the constructor look strange? In fact, it has no problem at all. It's different to the following constructor:
A( size_t i )
{
i_ = i;
}
Because in the first case the value of i_ will be set before entering constructor, while in the second case the value of i_ will be undetermined when the constructor is created.
You should always use a constructor list if possible as it is much more efficient than constructing ( ie default constructor) and then assigning. One important thing to remember is that the elements in your constructor list are initialized in the order that they were declared, NOT the order you write them, so:
class A
{
private:
int i_;
int j_;

public:
A( int i ): j_( i ), i_( 2 * j_ ) { }
};
It is undefined behaviour since i_ is constructed before j_, so at the point the program executes 2 * j_, j_ does not exist. However,
class A
{
int i_;
int j_;

public:
A( int i ): j_( 2 * i_ ), i_( i ) { }
};
It is absolutely fine, but it is confusing for a human to read, because it looks like it is being done in the wrong order. Because of this, when writing constructors, an excellent habit to get in to is to write the variables in the same order as you have declared them. Most of the time it makes no difference, but every now and again it will save you from creating a rather subtle bug.