开发目标
有一些数据采集卡功能比较多,有AD采集,DA输出,开关量输入,开关量输入,计数器采集等等功能,如果能够放到一个对话框中,同时显示输入量,设置输出量是最理想的情况了,但是在通道数比较多的情况下,一个对话框往往盛不下那么多的功能,此时就需要将不同的功能分配到不同的对话框中,如果想要同时显示多个对话框,在不同分辨率的显示器上就会有不同的排列方式,此时如果能够让程序记住对话框的位置,每一次打开程序的时候就可以按照相同的方式来显示,这样程序在不同的地方运行时,只要摆放一次就可以了。
获取对话框位置
使用对话框的GetWindowRect函数可以得到窗体相对于桌面的位置,使用对话框的SetWindowPos函数可以设置窗体位置,使用IsWindowVisible函数可以得到对话框是否显示的信息。得到这些信息后将它存在应用程序目录下。
获取应用程序目录
string GetAppPath()
{
char appPath[1024];
DWORD nameSize = GetModuleFileName(NULL, appPath, sizeof(appPath));
if(!nameSize)
return "";
string strFilePath(appPath);
strFilePath = strFilePath.substr(0, strFilePath.find_last_of('\\'));
return strFilePath;
}
读取、保存位置
使用了CIniWriter和CIniReader来读取和保存位置,这两个类不记得是从哪里找到的了,后面会给出源码,下面是读取位置代码:
string cfgName = GetAppPath() + "config.txt";
CIniWriter wr(cfgName.c_str());
CRect rc;
_infoDlg.GetWindowRect(&rc);
wr.WriteInteger("infoDlg", "top", rc.top);
wr.WriteInteger("infoDlg", "left", rc.left);
下面是保存位置代码:
_infoDlg.Create(IDD_INFO, this);
string cfgFileName = GetAppPath() + "config.txt";
CIniReader rd(cfgFileName.c_str());
int x = rd.ReadInteger("infoDlg", "left", 0);
int y = rd.ReadInteger("infoDlg", "top", 0);
_infoDlg.SetWindowPos(&wndBottom, x, y, 0, 0, SWP_NOSIZE);
下面是CIniWriter和CIniReader源程序:
头文件:
#ifndef INIREADER_H
#define INIREADER_H
#include <windows.h>
class CIniReader
{
public:
CIniReader(LPCTSTR szFileName);
int ReadInteger(LPCTSTR szSection, LPCTSTR szKey, int iDefaultValue);
float ReadFloat(LPCTSTR szSection, LPCTSTR szKey, float fltDefaultValue);
bool ReadBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolDefaultValue);
LPTSTR ReadString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szDefaultValue);
private:
TCHAR m_szFileName[255];
TCHAR _szReadString[255];
};
class CIniWriter
{
public:
CIniWriter(LPCTSTR szFileName);
void WriteInteger(LPCTSTR szSection, LPCTSTR szKey, int iValue);
void WriteFloat(LPCTSTR szSection, LPCTSTR szKey, float fltValue);
void WriteBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolValue);
void WriteString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szValue);
private:
TCHAR m_szFileName[255];
};
#endif //INIREADER_H
代码文件:
#include "IniRWer.h"
#include <iostream>
#include <memory.h>
#include <tchar.h>
//#include <windows.h>
CIniReader::CIniReader(LPCTSTR szFileName)
{
memset(m_szFileName, 0x00, sizeof(m_szFileName));
memcpy(m_szFileName, szFileName, _tcslen(szFileName)*sizeof(TCHAR));
}
int CIniReader::ReadInteger(LPCTSTR szSection, LPCTSTR szKey, int iDefaultValue)
{
int iResult = GetPrivateProfileInt(szSection, szKey, iDefaultValue, m_szFileName);
return iResult;
}
float CIniReader::ReadFloat(LPCTSTR szSection, LPCTSTR szKey, float fltDefaultValue)
{
TCHAR szResult[255];
TCHAR szDefault[255];
float fltResult;
_stprintf_s(szDefault, 255, TEXT("%f"),fltDefaultValue);
GetPrivateProfileString(szSection, szKey, szDefault, szResult, 255, m_szFileName);
fltResult = (float)_tstof(szResult);
return fltResult;
}
bool CIniReader::ReadBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolDefaultValue)
{
TCHAR szResult[255];
TCHAR szDefault[255];
bool bolResult;
_stprintf_s(szDefault, 255, TEXT("%s"), bolDefaultValue? TEXT("True") : TEXT("False"));
GetPrivateProfileString(szSection, szKey, szDefault, szResult, 255, m_szFileName);
bolResult = (_tcscmp(szResult, TEXT("True")) == 0 ||
_tcscmp(szResult, TEXT("true")) == 0) ? true : false;
return bolResult;
}
LPTSTR CIniReader::ReadString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szDefaultValue)
{
memset(_szReadString, 0x00, sizeof(_szReadString));
GetPrivateProfileString(szSection, szKey, szDefaultValue, _szReadString, 255, m_szFileName);
return _szReadString;
}
CIniWriter::CIniWriter(LPCTSTR szFileName)
{
memset(m_szFileName, 0x00, sizeof(m_szFileName));
memcpy(m_szFileName, szFileName, _tcslen(szFileName)*sizeof(TCHAR));
}
void CIniWriter::WriteInteger(LPCTSTR szSection, LPCTSTR szKey, int iValue)
{
TCHAR szValue[255];
_stprintf_s(szValue, 255, TEXT("%d"), iValue);
DWORD a = WritePrivateProfileString(szSection, szKey, szValue, m_szFileName);
if( !a )
{
a = GetLastError();
}
}
void CIniWriter::WriteFloat(LPCTSTR szSection, LPCTSTR szKey, float fltValue)
{
TCHAR szValue[255];
_stprintf_s(szValue, 255, TEXT("%f"), fltValue);
WritePrivateProfileString(szSection, szKey, szValue, m_szFileName);
}
void CIniWriter::WriteBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolValue)
{
TCHAR szValue[255];
_stprintf_s(szValue, 255, TEXT("%s"), bolValue ? TEXT("True") : TEXT("False"));
WritePrivateProfileString(szSection, szKey, szValue, m_szFileName);
}
void CIniWriter::WriteString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szValue)
{
WritePrivateProfileString(szSection, szKey, szValue, m_szFileName);
}
非模式窗体在任务栏是否显示图标
行为下面的Application Window属性设置为true,则在任务栏显示,设置为false,则不在任务栏显示