strdupa in Windows

my_alloca.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef _MY_ALLOCA_H_
#define _MY_ALLOCA_H_
 
#include <malloc.h>
 
__declspec(thread) extern size_t strdupa_len;
__declspec(thread) extern char* strdupa_p;
#define strdupa(_str) \
( \
	strdupa_len = strlen(_str), \
	strdupa_p = (char*)_alloca(strdupa_len + 1), \
	memcpy(strdupa_p, _str, strdupa_len + 1), \
	strdupa_p \
)
 
#endif

my_alloca.c

1
2
3
#include "my_alloca.h"
__declspec(thread) size_t strdupa_len;
__declspec(thread) char* strdupa_p;

example.c

1
2
3
4
5
6
#include "my_alloca.h"
 
int main()
{
	char* p = strdupa("hi");
}

获取HANDLE对应的文件路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <Windows.h>
#include <psapi.h>
#include <wchar.h>
 
WCHAR *getFilePathByHandle(HANDLE hFile, WCHAR* filePath, int filePathLen)
{
	DWORD dwFileSizeHi = 0;
	DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);
	HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, dwFileSizeLo, NULL);
 
	filePath[0] = 0;
	if (NULL != hFileMap)
	{
		void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);
		if (NULL != pMem)
		{
			if (0 == GetMappedFileNameW(GetCurrentProcess(), pMem, filePath, filePathLen))
			{
				// GetLastError
			}
			UnmapViewOfFile(pMem);
		}
		CloseHandle(hFileMap);
	}
	return filePath;
}
 
wchar_t toLower(wchar_t ch)
{
	if ('A' <= ch && ch <= 'Z')
		ch = ch - 'A' + 'a';
	return ch;
}
 
int isNoCaseBeginWith(wchar_t* l, wchar_t* r)
{
	if (*l == 0 && *r == 0)
		return 1;
	if (*l == 0 || *r == 0)
		return 0;
	for (; 0 != *l && 0 != *r; l++, r++)
	{
		if (toLower(*l) != toLower(*r))
			return 0;
	}
	return 1;
}
 
WCHAR* getDosFilePath(WCHAR *dosPath, int len, WCHAR* kernelPath)
{
	wchar_t path[3] = L"C:";
	wchar_t szBuf[MAX_PATH] = { 0 };
	for (wchar_t ch = L'A'; ch <= L'Z'; ch++)
	{
		path[0] = ch;
		QueryDosDevice(path, szBuf, MAX_PATH);
		if (isNoCaseBeginWith(kernelPath, szBuf))
		{
			swprintf_s(dosPath, len, L"%s%s", path, kernelPath + wcslen(szBuf));
			return dosPath;
		}
	}
	dosPath[0] = 0;
	return dosPath;
}
 
int main()
{
	HANDLE hFile = CreateFileW(L"d:\\test.txt",
		GENERIC_READ,
		FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
		NULL,
		OPEN_EXISTING,
		0x00,
		NULL);
 
	if (INVALID_HANDLE_VALUE == hFile)
	{
		return 0;
	}
 
	WCHAR path[1024];
	getFilePathByHandle(hFile, path, 1024);
 
	WCHAR dosPath[1024];
	getDosFilePath(dosPath, 1024, path);
 
	CloseHandle(hFile);
 
	return 0;
}

win32渐变颜色矩形函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#define FILL_STYLE_VERTICAL	0
#define FILL_STYLE_HORIZONTAL	1
 
void fillGradientRect(HDC hdc, RECT *rc, COLORREF fromRGB, COLORREF toRGB, int fillStyle)
{
	BITMAPINFO bmpInfo;
	memset(&bmpInfo, 0, sizeof(bmpInfo));
	bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmpInfo.bmiHeader.biWidth = rc->right - rc->left;
	bmpInfo.bmiHeader.biHeight = rc->bottom - rc->top;
	bmpInfo.bmiHeader.biPlanes = 1;
	bmpInfo.bmiHeader.biBitCount = 32;
	bmpInfo.bmiHeader.biCompression = BI_RGB;
 
	BYTE *rgbBuffer = (BYTE *)malloc(bmpInfo.bmiHeader.biWidth * bmpInfo.bmiHeader.biHeight * 4);
 
	int width = bmpInfo.bmiHeader.biWidth;
	int height = bmpInfo.bmiHeader.biHeight;
 
	int rStart = fromRGB & 0xFF;
	int rEnd = toRGB & 0xFF;
	int gStart = (fromRGB & 0xFF00) >> 8;
	int gEnd = (toRGB & 0xFF00) >> 8;
	int bStart = (fromRGB & 0xFF0000) >> 16;
	int bEnd = (toRGB & 0xFF0000) >> 16;
	if (FILL_STYLE_VERTICAL == fillStyle)
	{
		double rStep = (rEnd - rStart) * 1.0 / height;
		double gStep = (gEnd - gStart) * 1.0 / height;
		double bStep = (bEnd - bStart) * 1.0 / height;
		for (int y = 0; y < height; y++)
		{
			BYTE r = (BYTE)(rStart + rStep * y);
			BYTE g = (BYTE)(gStart + gStep * y);
			BYTE b = (BYTE)(bStart + bStep * y);
			for (int x = 0; x < width; x++)
			{
				BYTE *p = rgbBuffer + ((height - y) * width - (width - x)) * 4;
				*p++ = b;
				*p++ = g;
				*p++ = r;
			}
		}
	}
	else
	{
		double rStep = (rEnd - rStart) * 1.0 / width;
		double gStep = (gEnd - gStart) * 1.0 / width;
		double bStep = (bEnd - bStart) * 1.0 / width;
		for (int x = 0; x < width; x++)
		{
			BYTE r = (BYTE)(rStart + rStep * x);
			BYTE g = (BYTE)(gStart + gStep * x);
			BYTE b = (BYTE)(bStart + bStep * x);
			for (int y = 0; y < height; y++)
			{
				BYTE *p = rgbBuffer + ((height - y) * width - (width - x)) * 4;
				*p++ = b;
				*p++ = g;
				*p++ = r;
			}
		}
	}
 
	StretchDIBits(hdc, rc->left, rc->top, width, height, 0, 0, width, height, rgbBuffer, &bmpInfo, DIB_RGB_COLORS, SRCCOPY);
 
	free(rgbBuffer);
}
 
void CMFCApplicationDlg::OnPaint()
{
	CPaintDC dc(this);
	CRect rc(50, 50, 200, 200);
	fillGradientRect(dc.m_hDC, rc, RGB(255, 0, 255), RGB(0, 255, 0), FILL_STYLE_HORIZONTAL);
 
	CDialogEx::OnPaint();
}

win32发消息触发窗口拖动

1
2
3
	::ReleaseCapture();
	::SendMessage(m_hWnd, WM_NCLBUTTONDOWN, HTCAPTION, NULL);
	::SendMessage(m_hWnd, WM_LBUTTONUP, NULL, NULL);

win32限制改变窗口大小

响应WM_SIZING消息,消息函数中做如下处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void CDemoDlg::OnSizing(UINT fwSide, LPRECT pRect)
{
	if (pRect->right - pRect->left < minWidth_)
	{
		if (fwSide == WMSZ_LEFT
			|| fwSide == WMSZ_TOPLEFT
			|| fwSide == WMSZ_BOTTOMLEFT)
		{
			pRect->left = pRect->right - minWidth_;
		}
		else
			pRect->right = pRect->left + minWidth_;
	}
	if (pRect->bottom - pRect->top < memHdcHeight_)
	{
		if (fwSide == WMSZ_TOP
			|| fwSide == WMSZ_TOPLEFT
			|| fwSide == WMSZ_TOPRIGHT)
		{
			pRect->top = pRect->bottom - minHeight_;
		}
		else
			pRect->bottom = pRect->top + minHeight_;
	}
 
	CDialogEx::OnSizing(fwSide, pRect);
}

读取jpg文件到HBITMAP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
HBITMAP loadPictureFromFile(LPCTSTR szFile)
{
	HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
	if (INVALID_HANDLE_VALUE == hFile)
		return NULL;
 
	DWORD dwFileSize = GetFileSize(hFile, NULL);
 
	LPVOID pvData = NULL;
	HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
 
	pvData = GlobalLock(hGlobal);
 
	DWORD dwBytesRead = 0;
	BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
	GlobalUnlock(hGlobal);
	CloseHandle(hFile);
 
	LPSTREAM pStm = NULL;
	HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
 
	LPPICTURE gpPicture;
 
	hr = ::OleLoadPicture(pStm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
 
	pStm->Release();
 
	OLE_HANDLE picHandle;
	gpPicture->get_Handle(&picHandle);
	HDC hdc = ::CreateCompatibleDC(NULL);
	::SelectObject(hdc, (HGDIOBJ)picHandle);
 
	HBITMAP hRet = (HBITMAP)::GetCurrentObject(hdc, OBJ_BITMAP);
	DeleteDC(hdc);
 
//	gpPicture->Release(); // 这里不能释放gpPicture,否则返回的hRet是野指针数据
 
	return hRet;
}