记忆力太差,经常忘记以前写国的函数使用。 以后用的时候,又不得不查msdn,查资料,很费时,所以把一些常用的代码写下来。
1:8位bmp信息头:(8位以上需要更改几个参数bmphead.bfOffBits, m_bitmapinfo->bmiHeader.biBitCount)
BITMAPFILEHEADER bmphead;
LPBITMAPINFO m_bitmapinfo;/*bmp 头*/
m_bitmapinfo= (LPBITMAPINFO )new BYTE[1064];
m_bitmapinfo->bmiHeader.biBitCount=8;
m_bitmapinfo->bmiHeader.biClrImportant=0;
m_bitmapinfo->bmiHeader.biClrUsed=0;
m_bitmapinfo->bmiHeader.biCompression=0L;
m_bitmapinfo->bmiHeader.biPlanes=1;
m_bitmapinfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
m_bitmapinfo->bmiHeader.biSizeImage=0;
m_bitmapinfo->bmiHeader.biXPelsPerMeter=0;
m_bitmapinfo->bmiHeader.biYPelsPerMeter=0;
m_bitmapinfo->bmiHeader.biHeight=IHEIGHT;
m_bitmapinfo->bmiHeader.biWidth=IWIDTH;
for ( i=0;i<256;i++)
{
m_bitmapinfo->bmiColors[i].rgbBlue=(BYTE)i;
m_bitmapinfo->bmiColors[i].rgbGreen=(BYTE)i;
m_bitmapinfo->bmiColors[i].rgbRed=(BYTE)i;
m_bitmapinfo->bmiColors[i].rgbReserved=(BYTE)0;
}
ZeroMemory(&bmphead,sizeof(BITMAPFILEHEADER));
bmphead.bfType =0x4D42;
bmphead.bfOffBits =sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+ sizeof(RGBQUAD)*256;
bmphead.bfSize =bmphead.bfOffBits +IWIDTH*IHEIGHT;
2:临界区:
初始话和删除:
CRITICAL_SECTION g_s;
InitializeCriticalSection(&g_s);
DeleteCriticalSection(&g_s);
进入和离开临界区:
EnterCriticalSection(&g_s);
LeaveCriticalSection(&g_s);
3:exe的绝对路径(就算是快捷方式 也没问题)
GetModuleFileName(NULL,g_app_path1.GetBuffer(MAX_PATH), MAX_PATH);
g_app_path1.ReleaseBuffer();
int len =g_app_path1.GetLength();
//滤去文件名称,只保留路径
for(int i=0; i<=len-1; i++)
{
if(g_app_path1.Right(1)!="//")
g_app_path1.Delete(len-1-i,1);
else
{
g_app_path1.Delete(len-1-i,1);
break;
}
}
4:选择路径对话框
char szPath[MAX_PATH];
BROWSEINFO br;
ITEMIDLIST *pItem;
br.hwndOwner = ::GetDlgItem(m_hWnd, IDC_SAVE_DIR);
br.iImage = 0;
br.pszDisplayName = 0;
br.lParam = 0;
br.lpfn = 0;
br.lpszTitle = "请选择路径:";
br.pidlRoot = 0;
br.ulFlags = BIF_RETURNONLYFSDIRS;
pItem = SHBrowseForFolder(&br);
if (SHGetPathFromIDList(pItem,szPath)==TRUE)
{
m_save_path=CString(szPath);
UpdateData(FALSE);
return TRUE;
}
else
return FALSE;
5:更改鼠标指针
HCURSOR m_hcurSELECT= AfxGetApp()->LoadCursor( IDC_CURSOR1 );
SetCursor( m_hcurSELECT );
6:更新绘图区:Invalidate();
7:透明画刷: 一般如果直接调用 pDC->Ellipse(m_LRect); 那么中间的区域会变改变。
如果想不填充中间区域,那么 就这样子调用://设置 背景 透明画刷
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
pDC->SelectObject(pBrush);
pDC->Ellipse(m_LRect);
8:自定义 CButton 形状:
BOOL CMyButton::DrawButton(CDC *pDC)
{
m_LRect.SetRect( m_ButRect.left, m_ButRect.top,
m_ButRect.right-21, m_ButRect.bottom ); //按钮主体区尺寸
m_RRect.SetRect( m_ButRect.right-20, m_ButRect.top,
m_ButRect.right, m_ButRect.bottom ); //按钮选择区尺寸
CPen Pen;
Pen.CreatePen(PS_SOLID, 1, RGB(192,192,192) );
pDC->SelectObject( &Pen );
//设置 背景 透明画刷
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
pDC->SelectObject(pBrush);
pDC->Ellipse(m_LRect);
pDC->SelectStockObject( BLACK_BRUSH ); //定义画刷(黑色)
CRgn rgn;
//rgn.CreatePolygonRgn( m_pt, 3, ALTERNATE );
pDC->PaintRgn( &rgn ); //画选择区箭头
pDC->SetBkMode( TRANSPARENT );
return TRUE;
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
//定义按钮区域并初始化
CRect rect(lpDrawItemStruct->rcItem);
m_ButRect=rect;
DrawButton(pDC);
}
如果使button的其他区域无效,可以重载 mousemove 函数。。
9:单文档非d/v 动态创建控件,在view 视图中 重载Creat()
BOOL CChildView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
p.Create("OK", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(10,10,150,50), this,IDC_BUTTON1);
CMyDlg dlg;
if(dlg.DoModal()==IDOK)
{
PostQuitMessage(0);
// OnClose();
}
return TRUE;
}