【SQL开源代码栏目提醒】:网学会员SQL开源代码为您提供vc 编程技术600个大型项目源码(十) - 电子设计参考,解决您在vc 编程技术600个大型项目源码(十) - 电子设计学习中工作中的难题,参考学习。
OpenGLMFC单文档
图形绘制具体函数实现 1.在5dkx.comVeiw::preCreateWindow函数加入以下
代码 //设置窗口类型 cs.styleWS_CLIPCHILDRENWS_CLIPSIBLINGS return CView::PreCreateWindowcs 2.在5dkx.comVeiw::OnDraw函数加入 RenderScene 3.在5dkx.comVeiw::OnCreate加入一下
代码 //初始化OpenGL和设置定时器 m_pDC new C
ClientDCthis SetTimer120NULL InitializeOpenGLm_pDC 4.在5dkx.comVeiw::OnDestroy
加入 //删除调色板和渲染上下文、定时器 ::wglMakeCurrent00 ::wglDeleteContextm_hRC ifm_hPalette DeleteObjectm_hPalette ifm_pDC Delete m_pDC KillTimer1 5.在5dkx.comVeiw::OnSize函数加入glViewport00cxcy 6.在5dkx.comVeiw::OnTimerUINT nIDEvent函数加入 //添加定时响应器和场景
更新函数 InvalidateFALSE CView::OnTimernIDEvent 7.添加 void 5dkx.comVeiw::SetLogiclPalettevoid //设置逻辑调色板 struct WORD Version WORD NumberOfEnteries PALETTEENTRY aEnteries256 logicalPalette0×3
00256 BYTE reds 03672109145182218255 BYTE greens03672109145182218255 BYTE blues085170255 forint colorNum0colorNumlt256coloeNum logicalPalette.aEntriescolorNum.peRed redscolorNumamp0x07 logicalPalette.aEntriescolorNum.peGreen greenscolorNumgtgt0x03amp0x07 logicalPalette.aEntriescolorNum.peBlue greenscolorNumgtgt0x06amp0x03 logicalPalette.aEntriescolorNum.peFlags0 m_hPalette CreatePaletteLOGPALETTEamplogicalPalette 7.添加 BOOL 5dkx.comVeiw::InitializeOpenGLCDCpDC m_pDC pDC setupPixelFormat //生成绘制表 m_hRC ::wglCreateContextm_pDC-gtGetSafeHdc //设置
当前绘制描述表 ::wglMakeCruutem_pDC-gtGetSafeHdcm_hRC return TRUE 8.添加 BOOL 5dkx.comVeiw::SetupPixelFormat static PIXELFORMATDESCRIPTOR pfd sizeofPIXELFORMATDESCRIPTOR // pdf结构大小 1 // 版本号 PFD_DRAW_TO_WINDOW // 支持的窗口绘图 PFD_SUPPORT_OPENGL // 支持OpenGL PFD_DOUBL
EBUFFER // double buffered PFD_TYPE_RGBA // RGBA type 24 // 24-bit color depth 0 0 0 0 0 0 // 忽略颜色位 0 // 没有非透明度缓存 0 // 忽略移位位 0 // 无累加缓存 0 0 0 0 // 忽略累加位 16 // 16-bit 深度缓存 0 // 无模板缓存 0 //无
辅助缓存 PFD_MAIN_PLANE // 主层 0 // 保留 0 0 0 // 忽略层可见性和损毁掩模 //选择像素格式 int m_nPixelFormat ::ChoosePixelFormatm_pDC-gtGetSafeHdc amppfd //设置像素
格式 if m_nPixelFormat 0 return FALSE if ::SetPixelFormatm_pDC-gtGetSafeHdc m_nPixelFormat amppfd FALSE return FALSE ifpdf.dwFlags amp PDF_NEED_PALETTE
SetLogicalPaltte//设置
逻辑调色板 return TRUE 8.添加 BOOL 5dkx.comVeiw::RenderScene ::SwapBuffersm_pDC-gtGetSafeHdc return TRUE 利用MFC编程在工具条中增加组合框控件 Windows UI
技术一直是程序
设计人员尤其是用户
接口程序设计
人员最关心的技术。
显然新颖美观的界面会给用户留下深刻的印象。
Windows操作系统的
流行也在于其
用户界面的不断创新与
改进。
Office 97、Visual Studio 97、Outlook、IE等每一个新的软件的推出都能够给人新的感觉。
尤其是这些软
件工具条十分丰富美观如3D形式的工具条、飞行Tips这些小
技巧既富于吸引力又方便了使用者。
我们知道Visual C所提供的标准工具条中
基本上只有一些简单的诸如文档打开、存储
以及一些编辑功能按钮而Visual C自身的工具条功能十分丰富尤其是工具条中内嵌的
组合框控件十分方便。
实际上利用MFC
编程在工具条中增加一些有趣的控件
还是比较容易的下面我们就谈谈如何利用MFC编程在
工具条中增加
组合框控件。
1. 创建AppWizard工程文件设为toolbar采用
系统缺省设置。
2. 打开资源
编辑器选择工具条在工具条中增加一个按钮设置按钮 I D 为 I D_MY_COMBO_BOX 。
移动该按钮以使按钮处于适当的位置。
3. 从 C ToolBar 派 生出一个新类 C MyToolBar 在 C MyToolBar 类 中增加一个成员变量 m_wndMyComboBox
代码如下 class CMyToolBar : public CToolBar public: CComboBox m_wndMyComboBox 注意要将
代码放
在工程文件mainframe.h中“endif // _MSC_VER gt 1000”语句之后类CMainFrame
定义之前。
4. 将工程文件mainframe.h中的CToolBar变量m_wndToolBar用CMyToolBar变量m_wndToolBar代替。
同时增加函数CreateMyComboToolBar声明。
代码如下 protected: // control bar embedded members CStatusBar m_wndStatusBar CMyToolBar m_wndToolBar int CreateMyComboToolBar 5. 在工程文件mainframe.cpp
中的OnCreate
函数返回语句之前调用函数CreateMyToolBar
代码如下所示 ifCreateMyComboToolBar MessageBoxquotCreate My Combo ToolBar Failurequot 6. 以文本方式打开资源头文件resource.h增加一个控制ID如下
注意该控制的ID值不要与其它控制或资源的ID值重复 define IDC_MY_COMBO 1111 7. 在工程
文件mainframe.cpp中增加函数CreateMyToolBar
代码如下 int MainFrame::CreateMyToolBar define COMBO_BOX_WIDTH 80 //the width of the combo box //set up the ComboBox control as a select box //First get the index of the buttons position in the toolbar int index 0 CRecr rect whilem_wndToolBar.GetItemIDindex ID_MY_CO
MBO_BOX index //next convert that button to a seperator and get its position m_wndToolBar.SetButtonInfoindex ID_MY_
COMBO_BOX TBBS_SEPARATOR COMBO_BOX_WIDTH m_wndToolBar.GetItemRectindex amprect //expand the rectangle to allow the combo box room to drop down rect.top2 rect.bottom 200 // then .Create the combo box and show it if m_wndToolBar.m_ wndMyComboBox.Create WS_CHILDWS_VISIBLE CBS_AUTOHSCROLL CBS_DROPDOWNLIST CBS_HASSTRINGS rect ampm_wndToolBar IDC_MY_COMBO TRACE0quotFailed to create combo-box quot return FALSE m_wndToolBar.m_wndMyComboBox.ShowWindowSW_SHOW //adding string to the combo box m_wndToolBar.m_wndMyComboBox.AddStringquotFisrt Selectquot m_wndToolBar.m_wndMyComboBox.AddStringquotSecond Selectquot m_wndToolBar.m_wndMyComboBox.AddStringquotThird Selectquot m_wndToolBar.m_wndMyComboBox.AddStringquotFourth Selectquot m_wndToolBar.m_wndMyComboBox.AddStringquotFifth Selectquot m_wndToolBar.m_wndMyComboBox.SetCurSel0
编译并
运行该应用就会
发现工具条中多了一个组合框控件。
从上面的例程可以看出在工具条中增加新的控件
关键在于重写主
框架的OnCreate函数。
掌握了这
一点就可以在工具条中增加各种各样的控件了如增加无限输入Radio控件、
检查框Check控件等。
有兴趣的读者可以
自己去尝试一下加深
理解。
MFC通过ADO访问
SQL数据库VS2008中首先新建个基于Dialog的MFC应用
程序 然后经过wizard
生成的stdafx.h文件的末尾添加上 import quotC:Program FilesCommon FilesSystemadomsado15.dllquot no_namespace renamequotEOFquotquotrsEOFquot 双击resouce view中dialog目录
下的Dialog中的任意一个按钮 然后再ltProjectNamegtDlg.CPP按钮
事件中添加如下
代码 CoInitializeNULL //AfxOleInit _ConnectionPtr pConn__uuidofConnection _RecordsetPtr pRst__uuidofRecordset _CommandPtr pCmd__uuidofCommand//实例化
一个Command对象pCmd if FAILEDpConn.CreateInstancequotADODB.Connectionquot AfxMessageBoxLquotCreate Instance failedquot return pConn-gtOpenquotProviderSQLOLEDB.1
Integrated SecuritySSPI Initial Cata
logComponentData SourceVISIONSKYsqlexpressquotquotquotquotquotadModeUnknown AfxMessageBoxLquot连接
成功quot catch_com_error e AfxMessageBoxLquot
连接失败quot e.Description 这里使用的方法是windows验证你也可以用混合验证 ProviderSQLO
LEDB.1
Integrated SecuritySSPI Initial CatalogComponentData SourceVISIONSKYsqlexpress 替换成 ProviderSQLOLEDB.1Persist Security InfoFalseUser IDsaPWDsaInitial CatalogComponentData SourceVISIONSKY
sqlexpress 我的数据库用的是
sql2005express如果你用的是2
005而不是express版本的那么你需要将sqlexpress去掉即可 MFC中
关于控件和变量对应方法 在MFC中
我们常常要把一些变量于控件对应起来 1. 控件与value变量对应。
通过UpdateDate来实调用DoDataExchangeCDataExchange pDX实现变量信息交换 如 DDX_TextpDX IDC_STATICCOUNT m_sCount --gt value 把
控件ID为IDC_STATICCOUNT的控件与m_sCount变量对应起 用UpdateDateTRUE来把IDC_STATICCOUNT控件数据写到m_sCount 用UpdateDateFALSE把m_sCount数据读写到IDC_STATICCOUNT控件 2. 控件与控制变量对应。
DDX_ControlpDX IDC_
BSTARTTIME m_cStartTime--gtcontrol 把控件ID为IDC_BSTARTTIME的控件与m_sCount变量对应起如BUTTON。
此时我们可以通过m_cStartTime控件变量改变对应控件状态 m_cStartTime.Enable
WindowFALSE 可以
改变控件
是否可用如果
没有control对应 那么我们可以使用等价语句GetDlgItemIDC_BSTARTTIME-gtEnableWindowTRUE 对话框数据交换MFC //Jiangsheng的牢骚对话框数据交换 由于越来越多的人问如
何在对话框中获得
文档/视图指针以执行数据
访问和交换的
问题我大多数情况下都不得不
建议比较好的方式是备份数据而不是直接修改。
本文的
内容以现状提供并且不提供任何
担保Jiangsheng不对使用本文造成的可能的损失负责 //End Jiangsheng的牢骚对话框数据
交换 通常简单的
对话框不使用结构来
存储成员数据。
但是大量的
简单类型的成员交换会使得
代码繁琐。
这时候
可以使用结构来封装简单
类型的数据声明一个赋值操作符和修改DDX调用来简化数据交换
代码。
例如在
文档或者视图的命令处理函数中 void CMyView::OnSomeButton CMyDoc pDocGetDocument CMyDialog mydlg mydlg.m_datapDoc-gtdata1 ifmydlg.DoModalIDOKreturn//Allow cancel pDoc-gtdata1mydlg.m_data void CMyDialog:oDataExchangeCDataExchange pDX CDialog:oDataExchangepDX // call base class //AFX_DATA_MAPCMyDialog //AFX_DATA_MAP DDX_CheckpDX IDC_SEX m_data.m_bFemale DDX_TextpDX IDC_EDIT1 m_data.m_strName 这里pDoc-gtdata1和mydlg.m_data不必是同一类型的类只需要有操作符来复制数据就行了。
我要把文档的数据复制一个备份的
原因是如果在模态对话框中把控件
直接绑定到文档的成员那么如果在对话框数据
验证过程中一个控件验证失败那么验证
过程中这个控件之前的数据交换已经执行即使选择取消之后也无法恢复弹出对话框之前的
状态。
这可能不是用户期望的行为。
另外基于MFC的句柄映射机制跨线程调用CWnd成员函数例如UpdateData会有不可预料的
后果。
有
兴趣的可以参考微软
知识库文章Q147578 CWnd Derived MFC Objects and Multi-threaded
Applications。
修改工程的CLW文件以扩展默认的DDX/DDV
机制的
方法对我不很
实用。
我的VC经常丢失部
分类的向导信息在存盘的时候我的杀毒
软件就隔离了我的
代码文件
开始扫描病毒然后
VC报告文件不能保存classview的
相关类信息就丢失了我不得不每隔一段
时间删除并且重建CLW文件。
顺便说一下TN026里面似乎有好多BUG被解释了两次根据顺序来看第一个
解释应该是
正确的。
第二个似乎是旧的解释的版本忘记删除了。
另外有一段话“ single identifier for the DDV_ procedure. The C function name must start
with “DDV_” but dont include “DDX_” in the identifier.”被重复了两次。
看起来看MFC技术文章的人次是不多... 编程删除IE历史 在你调用IUrlHistoryStg:eleteUrl 之后 这个URL项目仍旧会
出现在IE历史纪录目录中。
你可以使用IContextMenu::InvokeCommand模拟一个手动删除
命令但是这样做的缺点是你
不能够跳过删除确认对话框 下面的
代码删除一个IE历史纪录
目录中顶层目录的第一项。
这通常是最久的
历史纪录但是你可以修改部分
代码来删除
需要的
项目。
// Error checking minimized for clarity. void DeleteUrlFromHistoryShell HRESULT hr // Call this if needed. CoInitialize NULL IShellFolder pDesktopFolder NULL IMalloc pMalloc NULL hr ::SHGetMallocamppMalloc // Get desktop folder. hr ::SHGetDesktopFolderamppDesktopFolder // Get the history folder. ITEMIDLIST pidlHistoryFolder NULL hr ::SHGetSpecialFolderLocationNULL
CSIDL_HISTORY amppidlHistoryFolder // Get the IShellFolder of the history folder. IShellFolder pHistoryFolder NULL hr pDesktopFolder-gtBindToObjectpidlHistoryFolder NULL IID_IShellFolder voidamppHistoryFolder // Enumerate the history items. IEnumIDList pHistoryEnum NULL hr pHistoryFolder-gtEnumObjectsNULL SHCONTF_FOLDERS SHCONTF_NONFOLDERS amppHistoryEnum ITEMI
DLIST pidl NULL ULONG fetched 0 hr pHistoryEnum-gtNext1 amppidl ampfetched if SUCCEEDEDhr const ITEMIDLIST pidl2 pidl // Get the IContextMenu interface. IContextMenu pContextMenu NULL hr pHistoryFolder-gtGetUIObjectOfNULL 1 amppidl2 IID_IContextMenu NULL voidamppContextMenu if SUCCEEDEDhr CMINVOKECOMMANDINFO pCommandInfo 0 pCommandInfo.cbSize sizeofCMINVOKECOMMANDINFO pCommandInfo.lpVerb _Tquotdeletequot pCommandInfo.fMask CMIC_MASK_FLAG_NO_UI // has no effect hr pContextMenu-gtInvokeCommandamppCommandInfo pContextMenu-gtRelease pHistoryEnum-gtRelease pHistoryFolder-gtRelease pMalloc-gtRelease pDesktopFolder-gtRelease IUrlHistoryStg2::
ClearHistory从IE历史纪录目录清除一已经被删除的URL项目。
IUrlHistoryStg:eleteUrl 并非设计来
用于删除历史纪录中的URL项目因为很多
内部函数依赖于它所以他的
行为不能被改变。
限制应用程序
的实例数目 ///////////////////////////////////////////////////////////////////////////// // CTheApp::FirstInstance // FirstInstance checks for an existing instance of the application. // If one is found it is activated. // // This function uses a technique similar to that
described in KB // article Q109175 to locate the previous instance of the application. // However instead of searching for a matching class name it searches // for a matching caption. This allows us to use the normal dialog // class for our main
window. It assumes that the AFX_IDS_APP_TITLE // string resource matches the caption specified in the dialog template. BOOL C
TheApp::FirstInstance CString strCaption strCaption.LoadStringAFX_IDS_APP_TITLE CWnd pwndFirst CWnd::FindWindowLPCTSTRDWORD_PTRWC_DIALOG strCaption if pwndFirst // another instance is already running - activate it CWnd pwndPopup pwndFirst-gtGetLastActivePopup pwndFirst-gtSetForegroundWindow if pwndFirst-gtIsIconic pwndFirst-gtShowWindowSW_SHOWNORMAL if pwndFirst pwndPopup pwndPopup-gtSetForegroundWindow return FALSE else // this is the first instance return TRUE ///////////////////////////////////////////////////////////////////////////// // CThe
App::InitInstance // InitInstance performs per-instance initialization of the DLGCBAR // application. If an instance of the application is already running // it activates that instance.
Otherwise it creates the mo
deless // dialog which serves as the applications interface. BOOL CTheApp::InitInstance if FirstInstance return FALSE 这里使用了窗口标题进行查找匹配。
可以使用内核同步
对象来
进行准确的匹配和防止同时启动。
让msxml4导出
的文本
xml缩进和换行 问起过好多次
的问题了手头正好在做XML的生成就写了一下 其实缩进和换行就是
文本在需要的位置
创建文本节点就可以了。
另外一个方法就是用SAX来写 ltMessage Datequot1/11/2004quot Timequot4:23:07 PMquot DateTimequot1/11/
2004 4:23:07 PMquot SessionIDquot1quotgt ltFromgt ltUser LogonNamequotuser1somesite.comquot FriendlyNamequotuser1quot/gt lt/Fromgt ltTogt ltUser LogonNamequotuser2somesite.comquot FriendlyNamequotuser2quot/gt lt/Togt ltText Stylequotfont-size:9ptquotgtlt/Textgt lt/Messagegt 可以这么写出来 voidCChatEditDlg::SendContentBSTR bstrContent CHARFORMAT2amp rcfm_wndSend.GetCharFormatSelection CComBSTR bstrXML USES_CONVERSION try IXMLDOMDocument2Ptr pDoc IXMLDOMProcessingInstructionPtrpProcessingInstruction IXMLDOMElementPtrpLogpMessage IXMLDOMElementPtrpFrompTo IXMLDOMElementPtrpUser IXMLDOMElementPtrpText HRESULThr pDoc.CreateInstance__uuidofMSXML2:OMDocument40 ifpDoc pDoc-gtasync false pDoc-gtresolveExternals false pDoc-gtvalidateOnParse true //pProcessingInstructionpDoc-gtcreateProcessingInstruction_Tquotxmlquot //_Tquotversion1.0 encodingUTF-16quot //pDoc-gtappendChildpProcessingInstruction ///Log pLogpDoc-gtcreateElement_TquotLogquot hrpDoc-gtappendChildpLog AppendTextNodepDocpLog_Tquotrntquot ///Log/Message pMessagepDoc-gtcreateElement_TquotMessagequot hrpLog-gtappendChildpMessage AppendTextNodepDocpMessage_Tquotrntquot COleDateTime tCOleDateTime::GetCurrentTime COleDateTime tdt td.m_dtinttd.m_dt COleDateTime ttt-td ///Log/Message/Date pMessage-gtsetAttributeLquotDatequotCOleVarianttd ///Log/Message/Time pMessage-gtsetAttributeLquotTimequotCOleVarianttt ///Log/Message/DateTime pMessage-gtsetAttributeLquotDateTimequotCOleVariantt ///Log/Message/SessionID pMessage.