MFC学习笔记

来源:互联网 发布:网络用语996什么意思 编辑:程序博客网 时间:2024/05/29 19:48


  • 通过theApp获得主窗口句柄

在任何文件中如何通过theApp获得主窗口的控件,并响应其消息函数

例:

extern CxxxApp theApp
theApp.m_pMainWnd->SendDlgItemMessage(IDC_INPUT_OK,WM_LBUTTONDOWN,NULL,NULL);
theApp.m_pMainWnd->SendDlgItemMessage(IDC_INPUT_OK,WM_LBUTTONUP,NULL,NULL);

以上是对主窗口的IDC_INPUT_OK控件发送单击命令。

  • MFC中RadioButton组回调函数使用

在面板中Ctrl+D设置RadioButton分组,将每组第一个Group属性设为True,设置一个变量关联每个组(控件变量选变量值),对变量赋值UpData(TRUE)传输到面板...

Group Radio Button 响应函数:给第一个添加响应后,将ON_BN_CLICKED(IDC_RADIO1,OnRadio)改为ON_CONTROL_RANGE(BN_CLICKED,IDC_RADIO1,IDC_RADIO10,OnRadio) 此处有10个Radio为一组

  • CComBSTR和char*相互转换
char *IP;
CComBSTR IoResource="L102";
IP=_com_util::ConvertBSTRToString(IoResource);
IoResource=_com_util::ConvertStringToBSTR(IP);
  • MessageBox使用
try{
    ...
} catch (_com_error& e)
 {
        ::MessageBox(NULL, e.Description(), e.ErrorMessage(), MB_ICONERROR);
}
MessageBox(L"生成会话工厂实例发生错误!",L"错误",MB_ICONERROR);
 
  • 创建线程
HANDLE pt=CreateThread(NULL,0,Thread_Proc,(LPVOID)&Lxi102channel[n],0,NULL);
CloseHandle(pt);
 
  • 自定义延时函数
void MyDelay(int n)
{
    int dwStart=0;
    int dwEnd=0;
    do 
    {
        MSG msg;
        GetMessage(&msg,NULL,0,0);
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        dwEnd=GetTickCount()-dwStart;
    } while(dwEnd<n);
}
 
  • 常用字符串操作函数
strcpy(char *_Dest,const char * _Source);
strcpy_s(char *_Dest,size_t _SizeInBytes,constchar* _Source);
wcscpy_s(WCHAR *_Dest,size_t _SizeInWords,const WCHAR*_Source);

swscanf_s(inputDate,L"%d:%d:%d",&hh,&mm,&ss);
sscanf_s(rawDate,"%d/%d/%d %d:%d:%d",&year,&month,&day,&hour,&minute,&second);

wsprintf(wstr1,L"正在进行通道%d等待源设置...",number);
swprintf_s(inputDate,L"%d:%d:%d",hh,mm,ss);
sprintf_s(scpi_send,SCPI_SEND_LENGTH,"MEASure:%s(@%d,@%d)\n",measureSwitchStr[measureSwitch],channel1,channel2);
 
  • CDC画图
int m_left;
int m_top ;
int m_right; 
int m_bottom;
CDC *pDC;
CPen pPenRed;
CPen pPenWhite;
CRect drawrect;
CWnd* pWnd;

pWnd = GetDlgItem(IDC_DRAW);//IDC_DRAW为图形控件(也可以是STATIC控件?)
pWnd->GetClientRect(&drawrect);//获得画图矩形区
pDC = pWnd->GetDC();    
pWnd->Invalidate();
pWnd->UpdateWindow();
pDC->Rectangle(&drawrect);

m_left = drawrect.left+25;
m_top = drawrect.top+5;
m_right = drawrect.right-10;
m_bottom = drawrect.bottom-25;

// 红色画笔
pPenRed.CreatePen(PS_DASHDOTDOT,1,RGB(255,0,0));
//白色画笔
pPenWhite.CreatePen(PS_SOLID,1,RGB(255,255,255));

//画一条对角线
pDC->FillSolidRect(&drawrect, RGB(0,0,0));//背景黑色
pDC->SelectObject(&pPenRed);//使用红笔
pDC->SetTextColor(RGB(0,255,0));//绿色字体
pDC->MoveTo(m_left,m_bottom);//线条起点
pDC->LineTo(m_right,m_top);//线条终点
pDC->TextOut(m_left+15,m_bottom+3,L"Hello");//指定坐标写字
 
  • CDC画图移动窗口图形消失解决办法
重载窗口OnPaint()函数
如:
void TSER::OnPaint()
{
    CPaintDC dc(this); // device context for painting
    // TODO: Add your message handler code here
    // Do not call CDialog::OnPaint() for painting messages
    DrawBackGround();
    DrawWave();
}
  • VARIANT数据存储与读取
void getValue(VARIANT *getVal)
{
    //返回数组格式为//时间 通道1至16数据
    /////////////////[t0, y1_0 ~ y16_0,
    //////////////////t1, y1_1 ~ y16_1,
    //////////////////t2, y1_2 ~ y16_2,
    //////////////////t3, y1_3 ~ y16_3,
    //////////////////................]
    ///////////// 返回数据初始化,如果读取数据正常,则返回读取数据,否则返回下列数据
    SAFEARRAY FAR* psa;
    SAFEARRAYBOUND rgsabound[1];
    rgsabound[0].lLbound=0;
    rgsabound[0].cElements=20*17;
    long idx=0;
    psa=SafeArrayCreate(VT_I4,1,rgsabound);
    for (unsignedint n=rgsabound[0].lLbound;n<rgsabound[0].cElements;n++)
    {
        readData=((n%17)==0)?(n*10):(0);
        idx=(long)n;
        SafeArrayPutElement(psa,&idx,&readData);
    }
    V_VT(getVal) = VT_ARRAY | VT_I4;
    V_ARRAY(getVal)=psa;
    /////////////////////
}

void FormateVariantToDatas()
{
    unsigned int xdata[1024];
    unsigned int ydata[16][1024];

    VARIANT V_getValue;
    getValue(&V_getValue);

    long dim=SafeArrayGetDim(V_getValue.parray);
    long ubound;
    long lbound;

    SafeArrayGetUBound(V_getValue.parray,dim,&ubound);
    SafeArrayGetLBound(V_getValue.parray,dim,&lbound);

    long index=0;
    unsigned int tempn=0;
    unsigned int tempnmode=0;
    long    readData;

    for (long index=0;index<=ubound;index++)
    {
        tempn=(unsignedint)index/17;
        tempnmode=(unsignedint)(index%17);
        SafeArrayGetElement(V_getValue.parray,&index,&readData);
        if (tempnmode==0)
        {
            xdata[tempn]=readData;
        }
        else
        {
            ydata[tempnmode-1][tempn]=readData;
        }
    }
}

原创粉丝点击