---
title: "webservice系列教学（4）"
author: "primer_2004"
date: "2007-11-05T23:16:53Z"
tags: []
slug: "webservice-4"
draft: false
lastmod: "2007-11-05T23:16:53Z"
id: 138248
url: "https://primer-2004.iteye.com/blog/138248"
---

001. &lt;MClientDlg.h&gt;
002. // MClientDlg.h : header file
003. //
004. #if !defined(AFX\_MCLIENTDLG\_H\_\_C2D8DBC4\_30FE\_40DD\_848C\_78D5834CAD07\_\_INCLUDED\_)
005. #define AFX\_MCLIENTDLG\_H\_\_C2D8DBC4\_30FE\_40DD\_848C\_78D5834CAD07\_\_INCLUDED_
006. #if \_MSC\_VER &gt; 1000
007. #pragma once
008. #endif // \_MSC\_VER &gt; 1000
009. /////////////////////////////////////////////////////////////////////////////
010. // CMClientDlg dialog
011. class CMClientDlg : public CDialog
012. {
013. // Construction
014. public:
015. void EnableButtons();
016. void DisableButtons();
017. void AssignpRef(VARIANT \*var1, VARIANT \*var2);
018. void assignItem(LVITEM \*item, UINT mask, int iItem, int iSubItem, LPTSTR pszText, int cchTextMax);
019. int UpdateList();
020. bool DestroyTree();
021. int CheckforURL();
022. int ModifyDialog();
023. void Execute();
024. int nCountParameter();
025. HTREEITEM AddtoTree(HTREEITEM hParent, HTREEITEM hInsertAfter, LPTSTR pszData, UINT mask, IUnknown * theInterface);
026. CMClientDlg(CWnd* pParent = NULL);    // standard constructor
027. // Dialog Data
028. //{{AFX\_DATA(CMClientDlg)
029. enum { IDD = IDD\_MCLIENT\_DIALOG };
030. CTreeCtrl    m\_TreeCtrl;
031. CListCtrl    m\_Parameters;
032. CString    m\_strParameter;
033. CString    m\_strURL;
034. //}}AFX\_DATA
035. // ClassWizard generated virtual function overrides
036. //{{AFX\_VIRTUAL(CMClientDlg)
037. protected:
038. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
039. //}}AFX\_VIRTUAL
040. // Implementation
041. protected:
042. HICON m\_hIcon;
043. // Generated message map functions
044. //{{AFX\_MSG(CMClientDlg)
045. virtual BOOL OnInitDialog();
046. afx\_msg void OnPaint();
047. afx\_msg HCURSOR OnQueryDragIcon();
048. afx\_msg void OnBrowse();
049. afx\_msg void OnClose();
050. afx\_msg void OnEdit();
051. afx\_msg void OnExecute();
052. afx\_msg void OnDeleteitemListparam(NMHDR* pNMHDR, LRESULT* pResult);
053. afx\_msg void OnDeleteitemTree(NMHDR* pNMHDR, LRESULT* pResult);
054. afx\_msg void OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult);
055. afx\_msg void OnLoad();
056. //}}AFX\_MSG
057. DECLARE\_MESSAGE\_MAP()
058. };
059. //{{AFX\_INSERT\_LOCATION}}
060. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
061. #endif // !defined(AFX\_MCLIENTDLG\_H\_\_C2D8DBC4\_30FE\_40DD\_848C\_78D5834CAD07\_\_INCLUDED\_)
062. &lt; resource.h&gt;
063. //{{NO\_DEPENDENCIES}}
064. // Microsoft Developer Studio generated include file.
065. // Used by MClient.rc
066. //
067. #define IDD\_MCLIENT\_DIALOG              102
068. #define IDR\_MAINFRAME                   128
069. #define IDC\_URL                         1000
070. #define IDLOAD                          1001
071. #define IDBROWSE                        1002
072. #define IDC\_TREE                        1003
073. #define IDC\_LISTPARAM                   1004
074. #define IDC\_PARAMETER                   1005
075. #define IDC\_EDIT                        1006
076. #define IDC\_EXECUTE                     1007
077. #define IDC\_CLOSE                       1008
078. // Next default values for new objects
079. //
080. #ifdef APSTUDIO\_INVOKED
081. #ifndef APSTUDIO\_READONLY\_SYMBOLS
082. #define \_APS\_NEXT\_RESOURCE\_VALUE        129
083. #define \_APS\_NEXT\_COMMAND\_VALUE         32771
084. #define \_APS\_NEXT\_CONTROL\_VALUE         1009
085. #define \_APS\_NEXT\_SYMED\_VALUE           101
086. #endif
087. #endif
088. &lt;StdAfx.cpp&gt;
089. // stdafx.cpp : source file that includes just the standard includes
090. //    MClient.pch will be the pre-compiled header
091. //    stdafx.obj will contain the pre-compiled type information
092. #include "stdafx.h"
093. &lt; MClient.cpp&gt;
094. // MClient.cpp : Defines the class behaviors for the application.
095. //
096. #include "stdafx.h"
097. #include "MClient.h"
098. #include "MClientDlg.h"
099. #ifdef \_DEBUG
100. #define new DEBUG\_NEW
101. #undef THIS\_FILE
102. static char THIS\_FILE\[] = \_\_FILE\_\_;
103. #endif
104. /////////////////////////////////////////////////////////////////////////////
105. // CMClientApp
106. BEGIN\_MESSAGE\_MAP(CMClientApp, CWinApp)
107. //{{AFX\_MSG\_MAP(CMClientApp)
108. // NOTE - the ClassWizard will add and remove mapping macros here.
109. //    DO NOT EDIT what you see in these blocks of generated code!
110. //}}AFX\_MSG
111. ON\_COMMAND(ID\_HELP, CWinApp::OnHelp)
112. END\_MESSAGE\_MAP()
113. /////////////////////////////////////////////////////////////////////////////
114. // CMClientApp construction
115. CMClientApp::CMClientApp()
116. {
117. // TODO: add construction code here,
118. // Place all significant initialization in InitInstance
119. }
120. /////////////////////////////////////////////////////////////////////////////
121. // The one and only CMClientApp object
122. CMClientApp theApp;
123. /////////////////////////////////////////////////////////////////////////////
124. // CMClientApp initialization
125. BOOL CMClientApp::InitInstance()
126. {
127. if (FAILED(::CoInitialize(NULL)))
128. {
129. return FALSE;
130. }
131. AfxEnableControlContainer();
132. // Standard initialization
133. // If you are not using these features and wish to reduce the size
134. //  of your final executable, you should remove from the following
135. //  the specific initialization routines you do not need.
136. #ifdef \_AFXDLL
137. Enable3dControls();            // Call this when using MFC in a shared DLL
138. #else
139. Enable3dControlsStatic();    // Call this when linking to MFC statically
140. #endif
141. CMClientDlg dlg;
142. m\_pMainWnd = &dlg;
143. int nResponse = dlg.DoModal();
144. if (nResponse == IDOK)
145. {
146. // TODO: Place code here to handle when the dialog is
147. //  dismissed with OK
148. }
149. else if (nResponse == IDCANCEL)
150. {
151. // TODO: Place code here to handle when the dialog is
152. //  dismissed with Cancel
153. }
154. ::CoUninitialize();
155. // Since the dialog has been closed, return FALSE so that we exit the
156. //  application, rather than start the application's message pump.
157. return FALSE;
158. }

<!--THE END-->

001. &lt; MClientDlg.cpp&gt;
002. // MClientDlg.cpp : implementation file
003. //
004. #include "stdafx.h"
005. #include "MClient.h"
006. #include "MClientDlg.h"
007. #include "Atlbase.h"
008. #ifdef \_DEBUG
009. #define new DEBUG\_NEW
010. #undef THIS\_FILE
011. static char THIS\_FILE\[] = \_\_FILE\_\_;
012. #endif
013. /////////////////////////////////////////////////////////////////////////////
014. // CMClientDlg dialog
015. CMClientDlg::CMClientDlg(CWnd* pParent /\*=NULL\*/)
016. : CDialog(CMClientDlg::IDD, pParent)
017. {
018. //{{AFX\_DATA\_INIT(CMClientDlg)
019. m\_strParameter = \_T("");
020. m\_strURL = \_T("");
021. //}}AFX\_DATA\_INIT
022. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
023. m\_hIcon = AfxGetApp()-&gt;LoadIcon(IDR\_MAINFRAME);
024. }
025. void CMClientDlg::DoDataExchange(CDataExchange* pDX)
026. {
027. CDialog::DoDataExchange(pDX);
028. //{{AFX\_DATA\_MAP(CMClientDlg)
029. DDX\_Control(pDX, IDC\_TREE, m\_TreeCtrl);
030. DDX\_Control(pDX, IDC\_LISTPARAM, m\_Parameters);
031. DDX\_Text(pDX, IDC\_PARAMETER, m\_strParameter);
032. DDX\_Text(pDX, IDC\_URL, m\_strURL);
033. //}}AFX\_DATA\_MAP
034. }
035. BEGIN\_MESSAGE\_MAP(CMClientDlg, CDialog)
036. //{{AFX\_MSG\_MAP(CMClientDlg)
037. ON\_WM\_PAINT()
038. ON\_WM\_QUERYDRAGICON()
039. ON\_BN\_CLICKED(IDBROWSE, OnBrowse)
040. ON\_BN\_CLICKED(IDC\_CLOSE, OnClose)
041. ON\_BN\_CLICKED(IDC\_EDIT, OnEdit)
042. ON\_BN\_CLICKED(IDC\_EXECUTE, OnExecute)
043. ON\_NOTIFY(LVN\_DELETEITEM, IDC\_LISTPARAM, OnDeleteitemListparam)
044. ON\_NOTIFY(TVN\_DELETEITEM, IDC\_TREE, OnDeleteitemTree)
045. ON\_NOTIFY(TVN\_SELCHANGED, IDC\_TREE, OnSelchangedTree)
046. ON\_BN\_CLICKED(IDLOAD, OnLoad)
047. //}}AFX\_MSG\_MAP
048. END\_MESSAGE\_MAP()
049. /////////////////////////////////////////////////////////////////////////////
050. // CMClientDlg message handlers
051. BOOL CMClientDlg::OnInitDialog()
052. {
053. CDialog::OnInitDialog();
054. if (ModifyDialog() == -1)
055. return FALSE;
056. // Set the icon for this dialog.  The framework does this automatically
057. //  when the application's main window is not a dialog
058. SetIcon(m\_hIcon, TRUE);            // Set big icon
059. SetIcon(m\_hIcon, FALSE);        // Set small icon
060. // TODO: Add extra initialization here
061. return TRUE;  // return TRUE  unless you set the focus to a control
062. }
063. // If you add a minimize button to your dialog, you will need the code below
064. //  to draw the icon.  For MFC applications using the document/view model,
065. //  this is automatically done for you by the framework.
066. void CMClientDlg::OnPaint()
067. {
068. if (IsIconic())
069. {
070. CPaintDC dc(this); // device context for painting
071. SendMessage(WM\_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
072. // Center icon in client rectangle
073. int cxIcon = GetSystemMetrics(SM\_CXICON);
074. int cyIcon = GetSystemMetrics(SM\_CYICON);
075. CRect rect;
076. GetClientRect(&rect);
077. int x = (rect.Width() - cxIcon + 1) / 2;
078. int y = (rect.Height() - cyIcon + 1) / 2;
079. // Draw the icon
080. dc.DrawIcon(x, y, m\_hIcon);
081. }
082. else
083. {
084. CDialog::OnPaint();
085. }
086. }
087. // The system calls this to obtain the cursor to display while the user drags
088. //  the minimized window.
089. HCURSOR CMClientDlg::OnQueryDragIcon()
090. {
091. return (HCURSOR) m\_hIcon;
092. }
093. /////////////////////////////////////////////////////////////////////////////////////////////////////////
094. //  function: CMClientDlg::OnBrowse()
095. //
096. //  parameters: No Parameters
097. //
098. //  description:  selection of wsdl file
099. //
100. //  returns: void
101. //
102. /////////////////////////////////////////////////////////////////////////////////////////////////////////
103. void CMClientDlg::OnBrowse()
104. {
105. // browse dialog will open and get the selected file
106. CFileDialog browse(TRUE, \_T("wsdl"), NULL, 0, \_T("WSDL Files (\*.wsdl)|\*.wsdl|All files|\*.\*||"));
107. if (browse.DoModal() == IDOK)
108. {
109. m\_strURL = browse.GetPathName();
110. if (m\_strURL.IsEmpty())
111. return;
112. UpdateData(FALSE);
113. OnLoad();
114. }
115. return;
116. }
117. //////////////////////////////////////////////////////////////////////////////////////////////////
118. //  function: CMClientDlg::OnClose()
119. //
120. //  parameters: No Parameter
121. //
122. //  description: called when dialog is being closed, but before close the dialog, tree should be destroyed.
123. //  returns: void
124. //
125. //////////////////////////////////////////////////////////////////////////////////////////////////
126. void CMClientDlg::OnClose()
127. {
128. if (!DestroyTree())
129. return;
130. CMClientDlg::DestroyWindow();
131. }
132. //////////////////////////////////////////////////////////////////////////////////////////////////
133. //  function: CMClientDlg::OnEdit()
134. //
135. //  parameters: No Parameters
136. //
137. //  description: Edits parameters of the operation
138. //
139. //  returns: void
140. //
141. //////////////////////////////////////////////////////////////////////////////////////////////////
142. void CMClientDlg::OnEdit()
143. {
144. // get selected row, insert the user input in 3 column of that row
145. // assign -1 to the selection mark  , so next time user has to select a row to insert data
146. int                nSelectedRow ;
147. LVITEM            LVitem;
148. UpdateData();
149. if (m\_Parameters.GetItemCount() == 0)
150. return;
151. nSelectedRow = m\_Parameters.GetSelectionMark();
152. if (nSelectedRow == -1)
153. return;
154. assignItem (&LVitem, LVIF\_TEXT, nSelectedRow, 2, (LPSTR)(LPCTSTR)m\_strParameter, ::SysStringLen((LPWSTR)(LPCTSTR)m\_strParameter));
155. if (m\_Parameters.SetItem(&LVitem) == 0)
156. MSG("Data could not be inserted !");
157. cleanup :
158. m\_strParameter.Empty();
159. m\_Parameters.SetSelectionMark(-1);
160. UpdateData(false);
161. UpdateData();
162. return;
163. }
164. //////////////////////////////////////////////////////////////////////////////////////////////////
165. //  function: CMClientDlg::OnExecute()
166. //
167. //  parameters: No Parameters
168. //
169. //  description: pressing Execute button calls this function
170. //  returns: void
171. //
172. //////////////////////////////////////////////////////////////////////////////////////////////////
173. void CMClientDlg::OnExecute()
174. {
175. UpdateData();
176. UpdateData(false);
177. if (CheckforURL() == -1)
178. return;
179. Execute();
180. return;
181. }