首页 > MFC > MFC & C++ 笔记

MFC & C++ 笔记

2012年8月2日 发表评论 阅读评论

获取控件及控件使能:

CButton *pButton=(CButton*)GetDlgItem(IDC_STOP);

pButton->EnableWindow(FALSE);

=================================================

文件拖拽功能:

→→→→→→→点这里

=================================================

文件读取:

CFile mFile(_T(FileName),CFile::modeRead);

mFile.Read(Buffer,BufferLength);

CFile::SeekToBegin();//文件指针回到头

mFile.Close();

CFile mFile(_T(FileName),CFile::modeWrite|CFile::modeCreate); 

mFile.Write(Buffer,BufferLength); 

mFile.Flush(); 

如果要实现每次读取文件的一行,要用CStdioFile,这个类继承于CFile

CStdioFile::ReadString(CString)

=================================================

MFC中使用OPENCV显示图片API:

void CFBGDlg::DrawPicToHDC(IplImage *image, UINT ID)
{
	CDC *pDC = GetDlgItem(ID)->GetDC();
	HDC hDC=  pDC->GetSafeHdc();
	CRect rect;
	GetDlgItem(ID)->GetClientRect(&rect);
	CvvImage cimg;       //只有CvvImage类的图像才有下面的DrawToHDC函数
	cimg.CopyOf(image,3);
	cimg.DrawToHDC(hDC,&rect);
	ReleaseDC(pDC);
}

=================================================

添加控件变量时弹出对话框“未能返回新代码元素。可能是语法错误。xxx”解决方法:

关闭整个VS,然后把工程目录里面的所有文件的只读属性去掉即可。

=================================================

打开文件的对话框:

CFileDialog::CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL );  

bOpenFileDialog 为TRUE则显示打开对话框,为FALSE则显示保存对话文件对话框。

lpszFilter 指定文件拓展名。

范例如下:

CFileDialog pCFileDialog(TRUE,NULL,NULL,0,”文本文件(*.CSV)|*.CSV||”);

pCFileDialog.m_ofn.lpstrTitle = “打开目标曲线文件 “;

if(pCFileDialog.DoModal() != IDOK)   return;

 之后可用下列函数获取文件相关信息:

CString CFileDialog::GetPathName( ) 得到完整的文件名,包括目录名和扩展名 

CString CFileDialog::GetFileName( ) 得到完整的文件名,包括扩展名 

CString CFileDialog::GetExtName( ) 得到完整的文件扩展名 

CString CFileDialog::GetFileTitle ( ) 得到完整的文件名,不包括目录名和扩展名 

POSITION CFileDialog::GetStartPosition( ) 对于选择了多个文件的情况得到第一个文件位置。 

CString CFileDialog::GetNextPathName( POSITION& pos ) 对于选择了多个文件的情况得到下一个文件位置,并同时返回当前文件名。但必须已经调用过POSITION CFileDialog::GetStartPosition( )来得到最初的POSITION变量。

=================================================

double转CString

double atof(CString);

=================================================

改变控件文字:

GetDlgItem(id)->SetWindowText()

=================================================

HWND→CWnd*:

CWnd* = CWnd::FromHandle (HWND);

=================================================

获取屏幕分辨率:

int GetSystemMetrics(SM_CXSCREEN)

int GetSystemMetrics(SM_CYSCREEN)

=================================================

移动鼠标:

如果使用mouse_event(MOUSEEVENTF_MOVE,dx,dy,0,0)的话,不知为什么移动会被非线性放大,如果要精确移动像素值dx,dy,要使用:

mouse_event(MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE ,dx*65536/1280,dy*65536/800,0,0);

1280×800是屏幕分辨率

最简单还是SetCursorPos(500,500);这个是绝对坐标

PS:GetCursorPos(&CurPoint);

=================================================

获取指定窗口句柄:

::FindWindow( NULL,OBJECT_WINDOW_NAME)

=================================================

获取最前端窗口句柄:

GetForegroundWindow()

PS:SetForegroundWindow(HWND)设置为最前端窗口

=================================================

鼠标操作:

VOID mouse_event(

DWORD dwFlags, // motion and click options

DWORD dx, // horizontal position or change

DWORD dy, // vertical position or change

DWORD dwData, // wheel movement

ULONG_PTR dwExtraInfo // application-defined information);

MOUSEEVENTF_MOVE:表明发生移动。

MOUSEEVENTF_LEFTDOWN:表明接按下鼠标左键。

MOUSEEVENTF_LEFTUP:表明松开鼠标左键。

MOUSEEVENTF_RIGHTDOWN:表明按下鼠标右键。

MOUSEEVENTF_RIGHTUP:表明松开鼠标右键。

MOUSEEVENTF_MIDDLEDOWN:表明按下鼠标中键。

MOUSEEVENTF_MIDDLEUP:表明松开鼠标中键。

MOUSEEVENTF_WHEEL:表明鼠标轮被移动。移动的数量由dwData给出。

=================================================

获取窗口位置:

GetWindowRect(::FindWindow( NULL,OBJECT_WINDOW_NAME),&rect);

GetClientRect(::FindWindow( NULL,OBJECT_WINDOW_NAME),&rect);不包括菜单等

=================================================

获取控件的位置:

GetDlgItem(nID)->GetWindowRect(&rect); 

GetDlgItem(nID)->GetParent()->ScreenToClient(rect);

=================================================

显示JPG格式的图片:

戳这里

=================================================

改变控件的大小:

GetDlgItem(IDC_XXX)->MoveWindow(rect);

=================================================

获取DC方法:

CDC* pDC = GetDC();

CPaintDC dc(this);

=================================================

多个控件响应同一个消息响应函数:

  1. 定义消息响应函数:afx_msg void OnStnClickedTablePic(UINT uID);
  2. 添加消息映射:ON_CONTROL_RANGE(STN_CLICKED,IDC_MIN,IDC_MAX,OnStnClickedTablePic)
  3. 添加函数定义:void CXxxxxxDlg::OnStnClickedTablePic(UINT uID){// TODO: 在此添加控件通知处理程序代码}
  4. 对于Static类控件,响应按钮前要先使能Notify:GetDlgItem(ID)->ModifyStyle(0,SS_NOTIFY);

=================================================

CString转char:

CString::GetBuffer()

=================================================

CPen更换:

CPen pen_line(PS_SOLID,1,RGB(255,255,0));//画线颜色

CPen* oldpen = memDC.SelectObject(&pen_line);

/**/

memDC.SelectObject(oldpen);

pen_line.DeleteObject();//必须要

pen_line.CreatePen(PS_SOLID,1,RGB(0,0,0));

oldpen = memDC.SelectObject(&pen_line);

================================================= 

写字背景透明:

memDC.SetBkMode(TRANSPARENT);

memDC.TextOut(0,0,str);

=================================================

CString指定小数点位数格式化:

str.Format(“%.2f dbm”,ReferenceLevel); 

================================================= 

CFile向文本输出换行:

/r/n

================================================= 

在使用 VS2008 SP1 与 Matlab2009a 混合编程时遇到如下编译错误:
1>—— Build started: Project: testsincplot_dll, Configuration: Debug Win32 ——
1>Compiling…
1>testsincplot_dll.cpp
1>d:/program files/matlab/r2009a/extern/include/mclmcr.h(1722) : error C2011: ‘_INTERFACE_INFO’ : ‘struct’ type redefinition
1> c:/program files/microsoft sdks/windows/v6.0a/include/ws2ipdef.h(84) : see declaration of ‘_INTERFACE_INFO’
原因:
VS2008默认设置系统平台的最低要求是 Vista 系统,如果是 XP 系统,就会出现上述错误。
解决方法:
打开项目文件夹中的 targetver.h 头文件,把其中的 0X0600 修改为 0X0501,即:
#define WINVER 0x0600 ===>> #define WINVER 0x0501
#define _WIN32_WINNT 0x0600 ===>> #define _WIN32_WINNT 0x0501

=================================================

菜单使能:

法一:添加UPADTE_COMMAND_UI事件,然后pCmdUI->Enable(TRUE)或者pCmdUI->Enable(FALSE);

法二:

CMenu* mmenu = GetMenu();
CMenu* submenu = mmenu->GetSubMenu(0);
submenu->EnableMenuItem(ID_FILE_NEW, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

但是要先在CMainFrm::CMainFrm中添加m_bAutoMenuEnable = FALSE;不然无效。不推荐此方法,因为当你工具栏某个按钮和菜单栏使用同一个ID的话,这么做只对菜单栏有变化,而工具栏上的按钮该怎么样还是怎么样。

=================================================

绘制实心扇形API:

//************************************
    // Method:    DrawPie
    // FullName:  CEle_Power_DecomposeView::DrawPie
    // Access:    public 
    // Returns:   void
    // Qualifier:
    // Parameter: CDC * pDC
    // Parameter: CPoint Center
    // Parameter: int Radius
    // Parameter: COLORREF color   :RGB(0,0,0)
    // Parameter: double angle_begin:角度
    // Parameter: double angle_end:角度
//************************************
void DrawPie( CDC* pDC,CPoint Center,int Radius,COLORREF color,double angle_begin,double angle_end )
{

    double PI = 3.14159265358979;

    CRect rect(Center.x-Radius, Center.y-Radius, Center.x+Radius, Center.y+Radius);

    CPen newpen;
    newpen.CreatePen(PS_SOLID ,1,color);
    pDC->SelectObject(newpen);

    double t = angle_begin/180*PI;
    double x = sin(t);
    double y = -1*cos(t);
    CPoint Start(Radius*x+Center.x,Radius*y+Center.y);

    t = angle_end/180*PI;
    x = sin(t);
    y = -1*cos(t);
    CPoint End(Radius*x+Center.x,Radius*y+Center.y);

    pDC->MoveTo(Center);
    pDC->LineTo(Start);
    pDC->MoveTo(Center);
    pDC->LineTo(End);

      //迫不得已,为了防止断线连接不上
    t = (angle_begin-1)/180*PI;
    x = sin(t);
    y = -1*cos(t);
    Start = CPoint(Radius*x+Center.x,Radius*y+Center.y);

    t = (angle_end+1)/180*PI;
    x = sin(t);
    y = -1*cos(t);
    End = CPoint(Radius*x+Center.x,Radius*y+Center.y);

    pDC->Arc(rect,End,Start);

    CBrush NewBrush;
    NewBrush.CreateSolidBrush(color);
    pDC->SelectObject(NewBrush);

    t = (angle_end+angle_begin)/180*PI/2;
    x = sin(t);
    y = -1*cos(t);

    CPoint FillCenterPoint(Center.x + 0.5*Radius*x,Center.y + 0.5*Radius*y);
    pDC->ExtFloodFill(FillCenterPoint.x,FillCenterPoint.y,color,FLOODFILLBORDER);
}

=================================================

获取当前工作路径:(利用打开文件对话框打开文件后当前工作路径是会变化的)

char pFileName[MAX_PATH];
int nPos = GetCurrentDirectory( MAX_PATH, pFileName);

=================================================  
透明画刷:
dc.SelectStockObject(NULL_BRUSH);
dc.Rectangle(anchor.x,anchor.y,drawto.x,drawto.y);
=================================================  
在编译时出现:
1>正在编译…
1>PreviewDlg.cpp
1>d:zacprojectvs2008projectsdipalgorithmdipalgorithmview.h(46) : error C2143: 语法错误 : 缺少“;”(在“*”的前面)
1>d:zacprojectvs2008projectsdipalgorithmdipalgorithmview.h(46) : error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
1>d:zacprojectvs2008projectsdipalgorithmdipalgorithmview.h(46) : error C4430: 缺少类型说明符 – 假定为 int。注意: C++ 不支持默认 int
1>d:zacprojectvs2008projectsdipalgorithmdipalgorithmview.h(46) : warning C4183: “GetDocument”: 缺少返回类型;假定为返回“int”的成员函数

从上面可以看出,出错是在编译PreviewDlg.cpp时,错误原因是我在PreviewDlg.cpp中使用了VIEW类,但只包含了XXXView.h而没有包含XXXDoc.h.应该这样:
#include “XXXDoc.h”
#include “XXXView.h”
注意两个的顺序不能调换,调换了也会报错
=================================================  
屏蔽指定Warning:
#pragma warning(disable : 4995)
=================================================  
控件大小随对话框大小变化而变化:

在OnPaint或者OnSize中加入以下类似代码,自行研究:

CRect m_rect;
GetParent()->GetClientRect(&m_rect);

CWnd *pWnd,;
pWnd = GetDlgItem(IDC_LIST);

if(pWnd)
{
    CRect rect;
    pWnd->GetWindowRect(&rect);
    ScreenToClient(&rect);

    rect.right=    rect.left+m_rect.Width()*0.9;
    rect.bottom = m_rect.Height()-m_rect.Height()*0.4-10;
    pWnd->MoveWindow(rect);
}

=================================================

某块内存被占用时产生中断:

_CrtSetBreakAlloc(615);

用处在于内存泄露是可以检测到是什么代码产生的,但是如果每次泄露的内存不同位置,就不好使了。。
================================================= 

基于单文档的程序启动最大化:

App类中:

m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
m_pMainWnd->UpdateWindow();
=================================================  

设置保存路径对话框:

char szPath[MAX_PATH]; 
CString str;
ZeroMemory(szPath, sizeof(szPath));  
BROWSEINFO bi;   
bi.hwndOwner = m_hWnd;   
bi.pidlRoot = NULL;   
bi.pszDisplayName = szPath;   
bi.lpszTitle = "请选择输出目录:";   
bi.ulFlags = 0;   
bi.lpfn = NULL;   
bi.lParam = 0;   
bi.iImage = 0;   
LPITEMIDLIST lp = SHBrowseForFolder(&bi);  
if(lp && SHGetPathFromIDList(lp, szPath))   
{
	str.Format("选择的目录为 %s", szPath);
	AfxMessageBox(str);
}

=================================================
读取整个文本文件:

ifstream myfile("a.TXT");
stringstream buffer;
buffer<<myfile.rdbuf();
cout<<buffer.str();

=================================================
读取中文路径需要先设置:
setlocale(LC_ALL,”Chinese-simplified”);


【完】

本文内容遵从CC版权协议,转载请注明出自http://www.kylen314.com

分类: MFC 标签: ,
  1. 本文目前尚无任何评论.
验证码:4 + 5 = ?

友情提示:留言可以使用大部分html标签和属性;

添加代码示例:[code lang="cpp"]your code...[/code]

添加公式请用Latex代码,前后分别添加两个$$