MFC小技巧积累

1.WinDef.h中定义了MAX_PATH :


#define MAX_PATH          260


WinBase.h中定义了GetModuleFileName:(获取程序的全路径完整名称)


WINBASEAPI
DWORD
WINAPI
GetModuleFileNameA(
    __in_opt HMODULE hModule,
    __out_ecount_part(nSize, return + 1) LPSTR lpFilename,
    __in     DWORD nSize
    );


WINBASEAPI
DWORD
WINAPI
GetModuleFileNameW(
    __in_opt HMODULE hModule,
    __out_ecount_part(nSize, return + 1) LPWSTR lpFilename,
    __in     DWORD nSize
    );


#ifdef UNICODE
#define GetModuleFileName  GetModuleFileNameW
#else
#define GetModuleFileName  GetModuleFileNameA
#endif // !UNICODE


2.GetModuleFileName与MAX_PATH结合使用获取程序的绝对路径全名称

wchar_t buff[MAX_PATH];

GetModuleFileName(AfxGetInstanceHandle(), buff, MAX_PATH);


3.DirNameOf宏:获取全路径中路径部分,不包含文件名

#define DirNameOf(string) (string).Mid(0, (string).ReverseFind(L'\\') + 1)


4.WinUser.h中定义了wsprintf:


WINUSERAPI
int
WINAPIV
wsprintfA(
    __out LPSTR,
    __in __format_string LPCSTR,
    ...);
WINUSERAPI
int
WINAPIV
wsprintfW(
    __out LPWSTR,
    __in __format_string LPCWSTR,
    ...);
#ifdef UNICODE
#define wsprintf  wsprintfW
#else
#define wsprintf  wsprintfA
#endif // !UNICODE


5.

wsprintf 用法

wsprintf:
Writes formatted data to the specified buffer. Any arguments are converted and copied to the output buffer according to the corresponding format specification in the format string. The function appends a terminating null character to the characters it writes, but the return value does not include the terminating null character in its character count.
If the function succeeds, the return value is the number of characters stored in the output buffer, not counting the terminating null character.


wsprintf接收三个参数,第一个参数,是缓冲区,第二个参数是格式,第三个参数是根据格式对应的各个参数值。调用wsprintf(,,),那么,就完成了字符串内容到缓冲区的拷贝。

6. wsprintf用法举例子--用于进行跟踪调试。

#define TraceDebug(text, param) \
{ \
wchar_t tracebuf[256]; \
wsprintf((LPTSTR)tracebuf, _T(text), (param)); \
OutputDebugString((LPCTSTR)tracebuf); \
}


7. WinBase.h有对于OutputDebugString的定义:


WINBASEAPI
VOID
WINAPI
OutputDebugStringA(
    __in_opt LPCSTR lpOutputString
    );
WINBASEAPI
VOID
WINAPI
OutputDebugStringW(
    __in_opt LPCWSTR lpOutputString
    );
#ifdef UNICODE
#define OutputDebugString  OutputDebugStringW
#else
#define OutputDebugString  OutputDebugStringA
#endif // !UNICODE


8.


在C/C++中,宏定义是由define完成的,define中有三个特殊的符号值得我们注意:

1. #:在宏展开的时候会将#后面的参数替换成字符串,如:

  #define p(exp) printf(#exp);

   调用p(asdfsadf)的时候会将#exp换成"asdfsadf"

2. ##:将前后两个的单词拼接在一起。例如《The C Programming Language》中的例子:

  #define cat(x,y) x##y

  调用cat(var, 123)展开后成为var123.

3. #@:将值序列变为一个字符

  #define ch(c) #@c

  调用ch(a)展开后成为'a'.

自己写了一小段测试程序:

#define A(a,b) a##b
#define B(a) #a
#define C(a) #@a

#include <iostream>

using namespace std;

void main()
{
int v = 0,v1 = 1,v12 = 2;//v stands for var
cout << A(v1,2) << endl;
cout << B(v1) << endl;
cout << C(v) << endl;
}

结果为:

1

v1

v

在c++面对对象化编程还可以这样用:

#define A

public://此处加一些成员,并且可以在成员名上用##下一些功夫

class Object

{

     A

};


9.tchar.h中有如下定义:


#define __T(x)      L ## x

#define _T(x)       __T(x)
#define _TEXT(x)    __T(x)


/* Program */


#define _tmain      wmain
#define _tWinMain   wWinMain
#define _tenviron   _wenviron
#define __targv     __wargv

10.非常不错的博客网站。

有百度登录分析。

http://www.crifan.com/


11.

#pragma deprecated()接一个函数名字,可以指定该函数过期了。调用过期函数,编译器会警告。警告是4995.

要去掉这个警告,可以通过代码中的这个操作:

#pragma marning(disable : 4995)

也可以在VS中配置。


You can use #pragma warning as shown on that MSDN page:

#pragma warning(disable: 4995)

Or, you can turn the warning off for the whole project in the project's properties (right click project -> Properties -> C/C++ -> Advanced -> Disable Specific Warnings). On the command line, you can achieve the same effect using/wd4995.


// C4995.cpp
// compile with: /W3
#include <stdio.h>

// #pragma warning(disable : 4995)
void func1(void)
{
    printf("\nIn func1");
}

int main() 
{
    func1();
    #pragma deprecated(func1)
    func1();   // C4995
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值