打算写一个C++程序 其实我是想用MFC静态库实现一个通过TXT配置文件在程序中动态创建界面的程序。
主要思路是:
1:提供一个基类CSearchBaseDialog,在基类中,程序从配置文件中读取界面控件的信息,然后根据配置文件中的信息在界面上创建相应的控件。
配置文件的格式:
IDC_BTN_MAIN_MENU=10;80;10;40;102;button;菜单
解释:
IDC_BTN_MAIN_MENU=left;right;top;bottom;nID;controlType;TEXT
即:
IDC_BTN_MAIN_MENU=x坐标;y坐标;top坐标;bottom坐标;控件句柄数;控件类型;控件文本
现在对代码进行了重构:
1:stdafx.h的代码:
#define STDAFX_H_
#include < afxwin.h >
#include < afxext.h >
#include < afxdisp.h >
#include < afxdtctl.h >
#include < afxcmn.h >
#include < windows.h >
#define MFC_SHOWWND 0x04EF
#endif
2:读取配置文件的类:
class MFCINi
{
public :
MFCINi();
virtual ~ MFCINi();
public :
/* *------------------------------------------------------------------------------------
* 名 称: <Open>
* 说 明: <打开配置文件>
* 参 数: <MFCINi &ini>:<配置信息对象>
* <const CString &strPath>:<配置文件路径>
* 返 回: <true:打开成功;false:打开失败>
*--------------------------------------------------------------------------------------- */
bool Open(MFCINi & ini, const CString & strPath);
/* *------------------------------------------------------------------------------------
* 名 称: <Close>
* 说 明: <关闭文件,释放占用的内存>
* 参 数:
* 返 回:
*--------------------------------------------------------------------------------------- */
void Close();
/* *------------------------------------------------------------------------------------
* 名 称: <Read>
* 说 明: <根据配置键,取得配置项>
* 参 数: <char* item>:<配置键>
* 返 回: <读出的配置信息>
*--------------------------------------------------------------------------------------- */
char * Read( char * item);
/* *------------------------------------------------------------------------------------
* 名 称: <GetAppPath>
* 说 明: <取得应用程序的路径(bug路径)>
* 参 数: <CString &strAppPath>:<存储取得的路径的字符串>
* 返 回: <配置项的值>
*--------------------------------------------------------------------------------------- */
static void GetAppPath(CString & strAppPath);
char ** m_chItems; // INi文件配置项
char ** m_chItemValues; // 项目值
int m_nItemCount; // INI文件总配置项数
private :
FILE * m_pf; // INI文件指针
};
该类的实现:
#include " MFCINi.h "
/ /
// Construction/Destruction
/ /
// 取得应用程序的路径
void MFCINi::GetAppPath(CString & strAppPath)
{
GetModuleFileName(NULL, strAppPath.GetBuffer(MAX_PATH), MAX_PATH);
strAppPath.ReleaseBuffer();
int nLength = 0 ;
char ch = NULL;
ch = ' / ' ;
nLength = strAppPath.GetLength();
strAppPath = strAppPath.Left(nLength - 1 );
nLength = strAppPath.ReverseFind(ch);
strAppPath = strAppPath.Left(nLength + 1 );
}
MFCINi::MFCINi()
{
m_nItemCount = 0 ;
m_chItems = NULL;
m_chItemValues = NULL;
m_pf = NULL;
}
MFCINi:: ~ MFCINi()
{
Close();
}
// 打开文件
bool MFCINi::Open(MFCINi & ini, const CString & strPath)
{
CString strIniPath;
CString strAppPath;
MFCINi::GetAppPath(strAppPath);
strIniPath.Format( " %s%s " , strAppPath, strPath);
m_pf = fopen(strIniPath, " a+ " );
if (m_pf == NULL)
{
return false ;
}
m_chItemValues = ( char ** )malloc(ITEM_COUNT * sizeof ( char ** ));
m_chItems = ( char ** )malloc(ITEM_COUNT * sizeof ( char ** ));
for ( int i = 0 ; i < ITEM_COUNT; i ++ )
{
m_chItemValues[i] = NULL;
m_chItems[i] = NULL;
}
char chReading[ 500 ];
char * pchSeparator = NULL;
while (m_nItemCount < ITEM_COUNT)
{
int nEOF = fscanf(m_pf, " %s " , chReading);
if (nEOF == EOF)
{
// 读取成功
return true ;
}
pchSeparator = strstr(chReading, " = " );
int nCharCountName = pchSeparator - chReading;
if (nCharCountName < 0 )
{
continue ;
}
int nCharCountValue = strlen(chReading) - nCharCountName - 1 ;
chReading[nCharCountName] = 0 ;
// 复制数据
m_chItems[m_nItemCount] = ( char * )malloc(nCharCountName + 1 );
strcpy(m_chItems[m_nItemCount], chReading);
m_chItemValues[m_nItemCount] = ( char * )malloc(nCharCountValue + 1 );
strcpy(m_chItemValues[m_nItemCount], chReading + nCharCountName + 1 );
m_nItemCount ++ ;
}
return true ;
}
// 读取配置项
char * MFCINi::Read( char * nItem)
{
if (m_nItemCount == 0 )
{
return NULL;
}
int i = 0 ;
while (strcmp(m_chItems[i], nItem) != 0 )
{
i ++ ;
if (i == m_nItemCount)
{
return NULL;
}
}
return m_chItemValues[i];
}
void MFCINi::Close()
{
if (m_pf != NULL)
{
fclose(m_pf);
m_pf = NULL;
}
for ( int i = 0 ; i < m_nItemCount; i ++ )
{
// 释放第i个配置项内存
if (m_chItems[i] != NULL)
{
free(m_chItems[i]);
m_chItems[i] = NULL;
}
// 释放第i个值内存
if (m_chItemValues[i] != NULL)
{
free(m_chItemValues[i]);
m_chItemValues[i] = NULL;
}
}
// 释放配置项内存
if (m_chItems != NULL)
{
free(m_chItems);
m_chItems = NULL;
}
// 释放值内存
if (m_chItemValues != NULL)
{
free(m_chItemValues);
m_chItemValues = NULL;
}
m_nItemCount = 0 ;
}
2:存储配置文件信息的结构体ConfigInfo:
{
int nLeft; // 左
int nRight; // 右
int nTop; // 上
int nBottom; // 下
int nID; // 控件ID
char * chType; // 控件类型
char * chText; // 控件文本
char * chKey; // 控件主键
};
3:存储创建窗体样式的结构体:
{
int nWndType; // 窗体类型
DWORD dwstyle; // 窗体样式
DWORD dwExStyle; // 扩展样式
LPCTSTR lpszClass; // 对应类
LPCTSTR lpszName; // 名称
DWORD style; // 样式
HWND hwndParent;
HMENU hMenu;
HINSTANCE hInstance;
LPVOID lpCreateParams;
};
4:创建公共函数类ToolKit:
声明:
{
public :
static char * TrimForCharArray( const char * chRes);
static void ShowMessageBox( char * chText);
ToolKit();
virtual ~ ToolKit();
};
实现:
#include " ToolKit.h "
/ /
// Construction/Destruction
/ /
ToolKit::ToolKit()
{
}
ToolKit:: ~ ToolKit()
{
}
void ToolKit::ShowMessageBox( char * chText)
{
AfxMessageBox(chText);
}
char * ToolKit::TrimForCharArray( const char * chResource)
{
char * chRet = NULL;
char * pchSeparator = NULL;
pchSeparator = strstr(chResource, "" );
int nCharCountName = pchSeparator - chResource;
if (nCharCountName < 0 )
{
return chRet;
}
int nCharCountValue = strlen(chResource) - nCharCountName - 1 ;
// 复制数据
chRet = ( char * )malloc(nCharCountName + 1 );
strcpy(chRet, chResource);
return chRet;
}
5:创建主要的基类CSearchBaseDialog:
声明:
#include " MFCINi.h "
class CSearchBaseDialog
{
private :
public :
CSearchBaseDialog();
virtual ~ CSearchBaseDialog();
ConfigInfo GetConfigStruct( char * chValue);
public :
/* *-----------------------------------------------------------------------------
* 名 称: <DrawItem>
* 说 明: <绘制事件>
* 参 数:
* 返 回:
*----------------------------------------------------------------------------------- */
virtual void DrawItem(WPARAM wParam,LPARAM lParam);
virtual void MoveWnd();
public :
/* *------------------------------------------------------------------------------------
* 名 称: <CreateControls>
* 说 明: <根据配置信息,创建界面的所有控件>
* 参 数:
* 返 回:
*--------------------------------------------------------------------------------------- */
void CreateControls();
/* *------------------------------------------------------------------------------------
* 名 称: <CreateControls>
* 说 明: <根据信息,创建界面的控件>
* 参 数: <char *chKey>:<控件的配置主键>
* <char *chValue>:<控件的配置信息>
* 返 回: <创建控件的句柄>
*--------------------------------------------------------------------------------------- */
// HWND CreateControl(char *chKey, char *chValue);
HWND CreateControl( const ConfigInfo & info);
/* *------------------------------------------------------------------------------------
* 名 称: <GetCreateWndStyle>
* 说 明: <取得创建控件的样式>
* 参 数: <CreateWndStruct &createWndStruct>:<控件样式>
* <char *chKey>:<控件的配置主键>
* <char *chValue>:<控件的配置信息>
* 返 回:
*--------------------------------------------------------------------------------------- */
// void GetCreateWndStyle(CreateWndStruct &createWndStruct, char* chKey, char* chValue);
void GetCreateWndStyle(CreateWndStruct & createWndStruct, const ConfigInfo & info);
void SetTitleText(HWND hWnd, TCHAR * chLabel, HFONT hFont, COLORREF rgb);
void SetTextByID( int nID, char * chText);
public :
virtual void OnDestroyWindow();
CString m_strFile; // 配置文件路径
HWND * m_hwndControls; // 窗体中的控件
HWND * m_hWnd; // 当前句柄
RECT m_rcMainWnd;
MFCINi m_MFCIni;
protected :
unsigned long GetIDByKey( char * chKey);
};
实现:
// SearchBaseDialog.cpp: implementation of the CSearchBaseDialog class.
//
//
#include "stdafx.h"
#include "SearchBaseDialog.h"
#include "MFCFrmMain.h"
#include "ToolKit.h"
//
// Construction/Destruction
//
CSearchBaseDialog::CSearchBaseDialog()
{
m_hWnd = NULL;
m_strFile = "../NaviIniHR.ini";
m_rcMainWnd.left = 200;
m_rcMainWnd.right = 700;
m_rcMainWnd.top = 200;
m_rcMainWnd.bottom = 500;
}
CSearchBaseDialog::~CSearchBaseDialog()
{
if (m_hwndControls != NULL)
{
delete[] m_hwndControls;
}
}
void CSearchBaseDialog::CreateControls()
{
//MFCINi ini;
m_MFCIni.Open(m_MFCIni, m_strFile);
m_hwndControls = new HWND[m_MFCIni.m_nItemCount];
for (int i = 0; i < m_MFCIni.m_nItemCount; i++)
{
ConfigInfo info = GetConfigStruct(m_MFCIni.m_chItemValues[i]);
m_hwndControls[i] = CreateControl(info);
}
}
HWND CSearchBaseDialog::CreateControl(const ConfigInfo &info)
{
CreateWndStruct createWndStruct;
GetCreateWndStyle(createWndStruct, info);
HWND wnd = NULL;
wnd = CreateWindowEx(
createWndStruct.dwExStyle,
createWndStruct.lpszClass,
createWndStruct.lpszName,
createWndStruct.style,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
createWndStruct.hwndParent,
createWndStruct.hMenu,
createWndStruct.hInstance,
createWndStruct.lpCreateParams);
if (strcmp(info.chType, "edit") != 0)
{
SetWindowText(wnd, TEXT(info.chText));
}
return wnd;
}
void CSearchBaseDialog::GetCreateWndStyle(CreateWndStruct &createWndStruct, const ConfigInfo &info)
{
createWndStruct.dwExStyle = 0;
createWndStruct.lpszClass = TEXT(info.chType);
createWndStruct.lpszName = TEXT("");
createWndStruct.style = WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_OWNERDRAW;
createWndStruct.hwndParent = *m_hWnd;
createWndStruct.hInstance = g_hInst;
createWndStruct.hMenu = (HMENU)info.nID;
createWndStruct.lpCreateParams = NULL;
if (strcmp(info.chType, "button") == 0)
{
createWndStruct.nWndType = 1;
createWndStruct.dwstyle = WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_OWNERDRAW;
}
else if (strcmp(info.chType, "static") == 0)
{
createWndStruct.nWndType = 3;
createWndStruct.dwstyle = WS_CHILD|WS_VISIBLE|WS_EX_CLIENTEDGE|WS_BORDER;
}
else if (strcmp(info.chType, "edit") == 0)
{
createWndStruct.nWndType = 2;
createWndStruct.dwstyle = WS_CHILD|WS_VISIBLE|ES_LEFT;
}
else
{
createWndStruct.nWndType = 1;
createWndStruct.dwstyle = WS_CHILD|WS_VISIBLE|WS_EX_CLIENTEDGE|WS_BORDER;
}
}
/*
HWND CSearchBaseDialog::CreateControl(char *chKey, char *chValue)
{
CreateWndStruct createWndStruct;
GetCreateWndStyle(createWndStruct, chKey, chValue);
HWND wnd = NULL;
wnd = CreateWindowEx(
createWndStruct.dwExStyle,
createWndStruct.lpszClass,
createWndStruct.lpszName,
createWndStruct.style,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
createWndStruct.hwndParent,
createWndStruct.hMenu,
createWndStruct.hInstance,
createWndStruct.lpCreateParams);
return wnd;
}
void CSearchBaseDialog::GetCreateWndStyle(CreateWndStruct &createWndStruct, char *chKey, char *chValue)
{
ConfigInfo info = GetConfigStruct(chValue);
createWndStruct.dwExStyle = 0;
createWndStruct.lpszClass = TEXT(info.chType);
createWndStruct.lpszName = TEXT("");
createWndStruct.style = WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_OWNERDRAW;
createWndStruct.hwndParent = *m_hWnd;
createWndStruct.hInstance = g_hInst;
createWndStruct.hMenu = (HMENU)info.nID;
createWndStruct.lpCreateParams = NULL;
if (strcmp(info.chType, "button") == 0)
{
createWndStruct.nWndType = 1;
createWndStruct.dwstyle = WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_OWNERDRAW;
}
else if (strcmp(info.chType, "static") == 0)
{
createWndStruct.nWndType = 3;
createWndStruct.dwstyle = WS_CHILD|WS_VISIBLE|WS_EX_CLIENTEDGE|WS_BORDER;
}
else if (strcmp(info.chType, "edit") == 0)
{
createWndStruct.nWndType = 2;
createWndStruct.dwstyle = WS_CHILD|WS_VISIBLE|ES_LEFT;
}
else
{
createWndStruct.nWndType = 1;
createWndStruct.dwstyle = WS_CHILD|WS_VISIBLE|WS_EX_CLIENTEDGE|WS_BORDER;
}
}*/
//绘制
void CSearchBaseDialog::DrawItem(WPARAM wParam,LPARAM lParam)
{
LPDRAWITEMSTRUCT pdis ;
pdis = (LPDRAWITEMSTRUCT) lParam ;
TCHAR szTemp[MAX_PATH];
if (GetWindowText(pdis->hwndItem, szTemp, MAX_PATH) > 0)
{
SetTitleText( pdis->hwndItem, szTemp, NULL, RGB(39, 102, 203));
}
}
void CSearchBaseDialog::SetTitleText(HWND hWnd, TCHAR* chLabel,
HFONT hFont, COLORREF rgb)
{
HFONT hOldFont;
COLORREF hOldColor;
RECT rt,rect;
HDC dc = ::GetDC(hWnd);
//显示文字的区域
::GetClientRect(hWnd, &rt);
SetRect(&rect, rt.left, rt.top, rt.right, rt.bottom);
if(hFont != NULL)//使用配置的字体,否则使用默认的字体
{
hOldFont = (HFONT)SelectObject(dc, hFont);
}
//背景透明效果
SetBkMode(dc,TRANSPARENT);
//文字的颜色
hOldColor = SetTextColor(dc, rgb);
DrawText( dc,chLabel,-1,&rect,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
//释放对象
SetTextColor(dc, hOldColor);
SelectObject(dc, hOldFont);
::ReleaseDC(hWnd,dc);
}
void CSearchBaseDialog::SetTextByID(int nID, char* chText)
{
SetWindowText(GetDlgItem(*m_hWnd, nID), TEXT(chText));
}
void CSearchBaseDialog::MoveWnd()
{
::MoveWindow(*m_hWnd, m_rcMainWnd.left, m_rcMainWnd.top,
m_rcMainWnd.right, m_rcMainWnd.bottom, true);
for (int i = 0; i < m_MFCIni.m_nItemCount; i++)
{
ConfigInfo info = GetConfigStruct(m_MFCIni.m_chItemValues[i]);
RECT rc;
rc.left = info.nLeft;
rc.right = info.nRight;
rc.top = info.nTop;
rc.bottom = info.nBottom;
::MoveWindow(m_hwndControls[i], rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, true);
//SetWindowText(m_hwndControls[i], TEXT(info.chText));
}
}
ConfigInfo CSearchBaseDialog::GetConfigStruct(char *chValue)
{
char chType[20];
char chText[20];
ConfigInfo info = {10, 20, 30, 40, 1, "", "", ""};
/*
{
for (int i = 0; i < 20; i++)
{
chType[i] = '*';
chText[i] = '*';
}
}*/
sscanf(chValue, ("%d;%d;%d;%d;%d;%[^;];%[^;]"),
&info.nLeft, &info.nRight, &info.nTop, &info.nBottom,
&info.nID, chType, chText);
info.chType = ToolKit::TrimForCharArray(chType);
info.chText = ToolKit::TrimForCharArray(chText);
return info;
}
unsigned long CSearchBaseDialog::GetIDByKey(char *chKey)
{
char* chValue = m_MFCIni.Read(chKey);
ConfigInfo info = GetConfigStruct(chValue);
return info.nID;
}
void CSearchBaseDialog::OnDestroyWindow()
{
DestroyWindow(*m_hWnd);
}
6:实现一个界面MainFrm:
声明:
提供一个句柄,一个该类型的全局变量:
class MainFrm : public CSearchBaseDialog
{
public :
MainFrm();
virtual ~ MainFrm();
void Command(WPARAM wParam, LPARAM lParam);
void OnDestroyWindow();
};
extern HWND g_hMainFrm;
extern MainFrm g_MainFrm;
实现:
//
/ /
#include " stdafx.h "
#include " MainFrm.h "
/ /
// Construction/Destruction
/ /
HWND g_hMainFrm;
MainFrm g_MainFrm;
MainFrm::MainFrm()
{
m_hWnd = & g_hMainFrm;
m_strFile = " ../NaviIniHR.ini " ;
}
MainFrm:: ~ MainFrm()
{
}
void MainFrm::Command(WPARAM wParam, LPARAM lParam)
{
DWORD dCommand = LOWORD(wParam);
if (dCommand == GetIDByKey( " IDC_BTN_MAIN_MENU " ))
{
ToolKit::ShowMessageBox( " 菜单 " );
}
else if (dCommand == GetIDByKey( " IDC_BTN_MAIN_ZOOMIN " ))
{
ToolKit::ShowMessageBox( " 放大 " );
}
else if (dCommand == GetIDByKey( " IDC_BTN_MAIN_EXIT " ))
{
OnDestroyWindow();
PostQuitMessage( 0 );
}
else
{
// ToolKit::ShowMessageBox("其他");
}
}
void MainFrm::OnDestroyWindow()
{
OnDestroyWindow();
PostQuitMessage( 0 );
}
7:配置文件内容:
IDC_BTN_MAIN_MENU = 10 ; 80 ; 10 ; 40 ; 102 ;button;菜单
// 放大按钮
IDC_BTN_MAIN_ZOOMIN = 10 ; 80 ; 70 ; 100 ; 103 ;button;放大
// 缩小按钮
IDC_BTN_MAIN_ZOOMOUT = 10 ; 80 ; 130 ; 160 ; 104 ;button;退出
// 全副显示按钮
IDC_BTN_MAIN_VIEWENTIRE = 10 ; 80 ; 190 ; 220 ; 105 ;button;全副显示
// 路径分析按钮
ID_BTN_NAVI_ANALY = 10 ; 80 ; 250 ; 280 ; 106 ;button;路径分析
// 导航按钮
ID_BTN_NAVI_GUIDE = 10 ; 80 ; 310 ; 340 ; 107 ;edit;导航
// 退出按钮
IDC_BTN_MAIN_EXIT = 10 ; 80 ; 370 ; 400 ; 108 ;button;退出
// 拼音查询按钮
IDD_DLG_POIQUERY = 10 ; 80 ; 430 ; 460 ; 109 ;button;按钮9
// 分类查询按钮
IDD_DLG_POIQUEFL = 10 ; 80 ; 490 ; 520 ; 110 ;button;按钮10
// 周边查询按钮
IDD_DLG_POIQUEZB = 10 ; 80 ; 550 ; 580 ; 111 ;button;按钮11
// 全文查询按钮
IDD_DLG_POIQUEQW = 10 ; 80 ; 610 ; 640 ; 112 ;button;按钮12
8:主程序:
#include " MFCINi.h "
#include " ToolKit.h "
#include " MainFrm.h "
extern HINSTANCE g_hInst;
HINSTANCE g_hInst;
// 窗体初始化
HWND InitInstance(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK MainProc(HWND, UINT, WPARAM, LPARAM);
void OnShowWnd(HWND hWnd);
void UpDataWnd(HWND hWnd);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWndMain;
MSG msg;
// 窗体初始化
hWndMain = InitInstance(hInstance, nCmdShow);
if (hWndMain == 0 )
{
// 初始化失败,显示错误信息
ToolKit::ShowMessageBox( " 窗体初始化失败 " );
return 0x10 ;
}
while (GetMessage( & msg, NULL, 0 , 0 ))
{
TranslateMessage( & msg);
DispatchMessage( & msg);
}
return msg.wParam;
}
HWND InitInstance(HINSTANCE hInstance, int nCmdShow)
{
WNDCLASS wc;
HWND hWnd;
g_hInst = hInstance;
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.cbClsExtra = 0 ;
wc.cbWndExtra = 0 ;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (RGB( 0 , 0 , 0 ));
wc.lpszMenuName = NULL;
wc.lpfnWndProc = MainProc;
wc.lpszClassName = TEXT( " MainFrm " ); // SNMAINCLASSNAME;
if (RegisterClass( & wc) == 0 )
{
return 0 ;
}
// 用户可以在这里注册其他自定义窗体
DWORD dwStyle = WS_POPUP | WS_CAPTION | WS_BORDER | WS_VISIBLE | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
CreateWndStruct createWndStruct;
createWndStruct.dwExStyle = 0 ;
createWndStruct.lpszClass = TEXT( " MainFrm " );
createWndStruct.lpszName = " 六子的代码 " ;
createWndStruct.style = dwStyle;
createWndStruct.hwndParent = NULL;
createWndStruct.hInstance = g_hInst;
createWndStruct.hMenu = NULL;
createWndStruct.lpCreateParams = NULL;
hWnd = CreateWindow(createWndStruct.lpszClass,
createWndStruct.lpszName,
createWndStruct.style,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
createWndStruct.hwndParent,
createWndStruct.hMenu,
createWndStruct.hInstance,
createWndStruct.lpCreateParams);
if ( ! IsWindow(hWnd))
{
return 0 ;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return hWnd;
}
LRESULT CALLBACK MainProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
g_hMainFrm = hWnd;
// 创建,定位主窗口上按钮
g_MainFrm.CreateControls();
g_MainFrm.MoveWnd();
SendMessage(g_hMainFrm, MFC_SHOWWND, 0 , 1 );
}
break ;
case WM_PAINT:
{
PAINTSTRUCT pt;
HDC hdc;
hdc = BeginPaint(hWnd, & pt);
EndPaint(hWnd, & pt);
}
break ;
case MFC_SHOWWND:
{
OnShowWnd(hWnd);
g_MainFrm.MoveWnd();
}
break ;
case WM_COMMAND:
{
// 相应主窗口上按钮操作
g_MainFrm.Command(wParam, lParam);
}
break ;
case WM_DRAWITEM:
{
g_MainFrm.DrawItem(wParam,lParam);
}
break ;
case WM_DESTROY:
{
/*
SNConfig snConfig;
snConfig.WriteConfig();
s_Main.OnDestroyWindow();
*/
g_MainFrm.OnDestroyWindow();
}
break ;
// 单击右键
case WM_RBUTTONDOWN:
{
// s_Main.PopupMenu(hWnd);
}
break ;
// 响应时钟
case WM_TIMER:
{
}
break ;
default :
break ;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
void OnShowWnd(HWND hWnd)
{
ShowWindow(hWnd,SW_SHOW);
UpDataWnd(hWnd);
}
void UpDataWnd(HWND hWnd)
{
// 如果没有刷新的界面则不用绘制背景
InvalidateRect(hWnd, NULL, false );
}
程序代码: