IntersectRect、wcsrchr、CComPtr、GetFileAttributes

本文介绍了如何使用GetFileAttributes函数获取文件系统属性,探讨了IntersectRect函数在图形操作中的应用,以及CComPtr在COM接口指针管理中的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

IntersectRect    两矩形相交形成的新矩形

The IntersectRect function calculates the intersection of two source rectangles and places the coordinates of the intersection rectangle into the destination rectangle. If the source rectangles do not intersect, an empty rectangle (in which all coordinates are set to zero) is placed into the destination rectangle.

BOOL IntersectRect(
  LPRECT lprcDst,        // intersection buffer
  CONST RECT*lprcSrc1,  // first rectangle
  CONST RECT*lprcSrc2   // second rectangle
);
 
Parameters
lprcDst
[out] Pointer to the RECT structure that is to receive the intersection of the rectangles pointed to by the lprcSrc1 and lprcSrc2 parameters. This parameter cannot be NULL.
lprcSrc1
[in] Pointer to the RECT structure that contains the first source rectangle.
lprcSrc2
[in] Pointer to the RECT structure that contains the second source rectangle.
Return Values

If the rectangles intersect, the return value is nonzero.

If the rectangles do not intersect, the return value is zero.

Windows NT/2000/XP: To get extended error information, call GetLastError.

Remarks

Because applications can use rectangles for different purposes, the rectangle functions do not use an explicit unit of measure. Instead, all rectangle coordinates and dimensions are given in signed, logical values. The mapping mode and the function in which the rectangle is used determine the units of measure.  

 

wcsrchr 获取一个字符在字符串中最后一次出现的位置

Scan a string for the last occurrence of a character.

 
char *strrchr(
   const char *str,
   int c 
); // C only
char *strrchr(
   char *str,
   int c 
); // C++ only
const char *strrchr(
   const char *str,
   int c ); // C++ only wchar_t *wcsrchr( const wchar_t *str, wchar_t c ); // C only wchar_t *wcsrchr( wchar_t *str, wchar_t c ); // C++ only const wchar_t *wcsrchr( const wchar_t *str, wchar_t c ); // C++ only unsigned char *_mbsrchr( const unsigned char *str, unsigned int c ); // C only unsigned char *_mbsrchr( unsigned char *str, unsigned int c ); // C++ only const unsigned char *_mbsrchr( const unsigned char *str, unsigned int c ); // C++ only unsigned char *_mbsrchr_l( const unsigned char *str, unsigned int c, _locale_t locale ); // C only unsigned char *_mbsrchr_l( unsigned char *str, unsigned int c, _locale_t locale ); // C++ only const unsigned char *_mbsrchr_l( const unsigned char *str, unsigned int c, _locale_t locale ); // C++ only

Parameters

str

Null-terminated string to search.

c

Character to be located.

locale

Locale to use.

Return Value

Returns a pointer to the last occurrence of c in str, or NULL if c is not found.

 

CComPtr用法

COM接口指针很危险,因为使用过程中需要每一个使用者都要严格并且正确的AddRef和Release,一旦出现问题,就会造成对象不能被正常释放,或者对象被重复删除,造成程序崩溃。所以使用COM接口,必须小心翼翼才行。
但是,即使所有的代码中,都正确的AddRef和Release,也不一定能保证万无一失,例如:
void SomeApp( IHello * pHello )
{
IHello* pCopy = pHello;
pCopy->AddRef(); 
OtherApp();
pCopy->Hello();
pCopy->Release();
}
看起来好像无懈可击,但是假设OtherApp中抛出了异常,那么pCopy->Release不就被跳过去了吗?
幸好,所有的问题都从简单到复杂,再从复杂到简单的,因为我们有CComPtr!

CComPtr被称为智能指针,是ATL提供的一个模版类,能够从语法上自动完成AddRef和Release。(源代码在atlbase.h中)
CComPtr的用法很简单,以IHello*为例,将程序中所有接口指针类型(除了参数),都使用CComPtr<IHello> 代替即可。即程序中除了参数之外,再也不要使用IHello*,全部以CComPtr<IHello>代替。
CComPtr的用法和普通COM指针几乎一样,另外使用中有以下几点需要注意。
1. CComPtr已经保证了AddRef和Release的正确调用,所以不需要,也不能够再调用AddRef和Release。
2. 如果要释放一个智能指针,直接给它赋NULL值即可。(这一点要牢记曾因为没有设置为null而出错)
3. CComPtr本身析构的时候会释放COM指针。
4. 当对CComPtr使用&运算符(取指针地址)的时候,要确保CComPtr为NUL。(因为通过CComPtr的地址对CComPtr赋值时,不会自动调用AddRef,若不为NULL,则前面的指针不能释放,CComPtr会使用assert报警)
以刚才的程序为例:
void SomeApp( IHello * pHello )
{
CComPtr<IHello> pCopy = pHello;
OtherApp();
pCopy->Hello();
}
由于pCopy是一个局部的对象,所以即使OtherApp()抛出异常,pCopy也会被析构,指针能够被释放。
如果不想在程序临近发布前,还因为COM指针的引用计数造成崩溃的话,就牢记这一点吧:程序中除了参数之外,不要直接使用COM指针类型,一定要全部以CComPtr<IXXX>代替

 

DWORD GetFileAttributes(LPCTSTR lpFilename); 为一个指定的文件或目录返回文件系统的属性

参数:

    hFileName: 输入参数,为需要获取属性的文件或目录

 

返回值:

    返回DWORD值,表示文件属性。如果返回INVALID_FILE_ATTRIBUTES,则表示失败,可使用GetLastError函数获取错误信息

 

使用说明:
    要判断文件属性,需要使用“&”与属性常量进行运算,如果运行结果为真,则表示具有这种属性

 

注意:INVALID_FILE_ATTRIBUTES 实为DWORD(-1). 因此在判断文件属性之前,首先要确保函数返回值不是INVALID_FILE_ATTRIBUTES,因为和INVALID_FILE_ATTRIBUTES 做 ‘&’ 操作,结果总是非零,从而带来误判。

在MATLAB中,判断两个矩形是否有重叠部分,通常需要比较它们的边界。你可以通过比较矩形的左边界、右边界、上边界和下边界来实现。以下是一种基本的方法: ```matlab function isOverlap = checkOverlap(rect1, rect2) % 矩形1和2的边界信息 [x1, y1, width1, height1] = getBBox(rect1); [x2, y2, width2, height2] = getBBox(rect2); % 计算每个矩形的左上角和右下角坐标 ul1 = [x1, y1]; lr1 = [x1 + width1, y1 + height1]; ul2 = [x2, y2]; lr2 = [x2 + width2, y2 + height2]; % 判断是否相交 isOverlap = ~((ul1(1) > lr2(1)) || (lr1(1) < ul2(1)) || ... % 左边界 (ul1(2) > lr2(2)) || (lr1(2) < ul2(2))); % 上边界 % 如果左上角和右下角都满足条件,则表示有重叠 if isOverlap && intersectRect(ul1, lr1, ul2, lr2) isOverlap = true; end end function [x, y, width, height] = getBBox(rect) % 提取矩形的顶点坐标 x = get(rect, 'XData'); y = get(rect, 'YData'); width = diff([x(1), x(end)]); height = diff([y(1), y(end)]); end function intersects = intersectRect(rect1, rect2) % 函数用于检查两个矩形是否完全重叠 intersects = (min(rect1(:,1)) <= max(rect2(:,1))) && ... (min(rect1(:,2)) <= max(rect2(:,2))) && ... (max(rect1(:,1)) >= min(rect2(:,1))) && ... (max(rect1(:,2)) >= min(rect2(:,2))); end ``` 上述代码定义了一个`checkOverlap`函数,它首先获取两个矩形的边界信息,然后检查它们的左上角和右下角是否交叉。如果交叉,再调用`intersectRect`函数进一步确认是否存在重叠。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值