图片 1图片 2

   
LabVIEW的TCP/IP函数库非常好用,但是不恰当地设置打开连接结点的参数将带来一些问题,麻烦。如下图的打开连接的参数设置:

 这是我在项目实战中的个人总结,写的仓促,有些东西也不一定准确,有些是自己推断的,还希望各位多多指教,多多评论。

 1 //SYSMETS.H -- System metrics display structure
 2 #include <Windows.h>
 3 
 4 #define NUMLINES ((int) (sizeof(sysmetrics) / sizeof(sysmetrics[0])))
 5 
 6 struct  
 7 {
 8     int iIndex;
 9     TCHAR *szLabel;
10     TCHAR *szDesc;
11 }
12 sysmetrics[] =
13 {
14     SM_CXSCREEN,            TEXT("SM_CXSCREEN"),            TEXT("Screen width in pixels"),
15     SM_CYSCREEN,            TEXT("SM_CYSCREEN"),            TEXT("Screen height in pixels"),
16     SM_CXVSCROLL,            TEXT("SM_CXVSCROLL"),            TEXT("Vertical scroll width"),
17     SM_CYHSCROLL,            TEXT("SM_CYHSCROLL"),            TEXT("Horizontal scroll height"),
18     SM_CYCAPTION,            TEXT("SM_CYCAPTION"),            TEXT("Caption bar height"),
19     SM_CXBORDER,            TEXT("SM_CXBORDER"),            TEXT("Window border width"),
20     SM_CYBORDER,            TEXT("SM_CYBORDER"),            TEXT("Window border height"),
21     SM_CXFIXEDFRAME,        TEXT("SM_CXFIXEDFRAME"),        TEXT("Dialog window frame width"),
22     SM_CYFIXEDFRAME,        TEXT("SM_CYFIXEDFRAME"),        TEXT("Dialog window frame height"),
23     SM_CYVTHUMB,            TEXT("SM_CYVTHUMB"),            TEXT("Vertical scroll thumb height"),
24     SM_CXHTHUMB,            TEXT("SM_CXHTHUMB"),            TEXT("Horizontal scroll thumb width"),
25     SM_CXICON,                TEXT("SM_CXICON"),                TEXT("Icon width"),
26     SM_CYICON,                TEXT("SM_CYICON"),                TEXT("Icon height"),
27     SM_CXCURSOR,            TEXT("SM_CXCURSOR"),            TEXT("Cursor width"),
28     SM_CYCURSOR,            TEXT("SM_CYCURSOR"),            TEXT("Cursor height"),
29     SM_CYMENU,                TEXT("SM_CYMENU"),                TEXT("Menu bar height"),
30     SM_CXFULLSCREEN,        TEXT("SM_CXFULLSCREEN"),        TEXT("Full screen client area width"),
31     SM_CYFULLSCREEN,        TEXT("SM_CYFULLSCREEN"),        TEXT("Full screen client area height"),
32     SM_CYKANJIWINDOW,        TEXT("SM_CYKANJIWINDOW"),        TEXT("Kanji window height"),
33     SM_MOUSEPRESENT,        TEXT("SM_MOUSEPRESENT"),        TEXT("Mouse present flag"),
34     SM_CYVSCROLL,            TEXT("SM_CYVSCROLL"),            TEXT("Vertical scroll arrow height"),
35     SM_CXHSCROLL,            TEXT("SM_CXHSCROLL"),            TEXT("Horizontal scroll arrow width"),
36     SM_DEBUG,                TEXT("SM_DEBUG"),                TEXT("Debug version flag"),
37     SM_SWAPBUTTON,            TEXT("SM_SWAPBUTTON"),            TEXT("Mouse buttons swapped flag"),
38     SM_CXMIN,                TEXT("SM_CXMIN"),                TEXT("Minimum window width"),
39     SM_CYMIN,                TEXT("SM_CYMIN"),                TEXT("Minimum window height"),
40     SM_CXSIZE,                TEXT("SM_CXSIZE"),                TEXT("Min/Max/Close button width"),
41     SM_CYSIZE,                TEXT("SM_CYSIZE"),                TEXT("Min/Max/Close button height"),
42     SM_CXSIZEFRAME,            TEXT("SM_CXSIZEFRAME"),            TEXT("Window sizing frame width"),
43     SM_CYSIZEFRAME,            TEXT("SM_CYSIZEFRAME"),            TEXT("Window sizing frame height"),
44     SM_CXMINTRACK,            TEXT("SM_CXMINTRACK"),            TEXT("Minimum window tracking width"),
45     SM_CYMINTRACK,            TEXT("SM_CYMINTRACK"),            TEXT("Minimum window tracking height"),
46     SM_CXDOUBLECLK,            TEXT("SM_CXDOUBLECLK"),            TEXT("Double click x tolerance"),
47     SM_CYDOUBLECLK,            TEXT("SM_CYDOUBLECLK"),            TEXT("Double click y tolerance"),
48     SM_CXICONSPACING,        TEXT("SM_CXICONSPACING"),        TEXT("Horizontal icon spacing"),
49     SM_CYICONSPACING,        TEXT("SM_CYICONSPACING"),        TEXT("Vertical icon spacing"),
50     SM_MENUDROPALIGNMENT,    TEXT("SM_MENUDROPALIGMENT"),    TEXT("Left or right menu drop"),
51     SM_PENWINDOWS,            TEXT("SM_PENWINDOWS"),            TEXT("Pen extensions installed"),
52     SM_DBCSENABLED,            TEXT("SM-DBCSENABLED"),            TEXT("Double-Byte Char Set enable"),
53     SM_CMOUSEBUTTONS,        TEXT("SM_CMOUSEBUTTONS"),        TEXT("Number of mouse buttons"),
54     SM_SECURE,                TEXT("SM_SECURE"),                TEXT("Security present flag"),
55     SM_CXEDGE,                TEXT("SM_CXEDGE"),                TEXT("3-D border width"),
56     SM_CYEDGE,                TEXT("SM_CYEDGE"),                TEXT("3-D border height"),
57     SM_CXMINSPACING,        TEXT("SM_CXMINSPACING"),        TEXT("Minimized window spacing width"),
58     SM_CYMINSPACING,        TEXT("SM-CYMINSPACING"),        TEXT("Minimized window spacing height"),
59     SM_CXSMICON,            TEXT("SM_CXSMICON"),            TEXT("Small icon width"),
60     SM_CYSMICON,            TEXT("SM_CYSMICON"),            TEXT("Small icon height"),
61     SM_CYSMCAPTION,            TEXT("SM_CYSMCAPTION"),            TEXT("Small caption height"),
62     SM_CXSMSIZE,            TEXT("SM_CXSMSIZE"),            TEXT("Small caption button width"),
63     SM_CYSMSIZE,            TEXT("SM_CYSMSIZE"),            TEXT("Small caption button height"),
64     SM_CXMENUSIZE,            TEXT("SM_CXMENUSIZE"),            TEXT("Menu bar button width"),
65     SM_CYMENUSIZE,            TEXT("SM_CYMENUSIZE"),            TEXT("Menu bar button height"),
66     SM_ARRANGE,                TEXT("SM_ARRANGE"),                TEXT("How minimized windows arranged"),
67     SM_CXMINIMIZED,            TEXT("SM_CXMINIMIZED"),            TEXT("Minimized window width"),
68     SM_CYMINIMIZED,            TEXT("SM_CYMINIMIZED"),            TEXT("Minimized window height"),
69     SM_CXMAXTRACK,            TEXT("SM_CXMAXTRACK"),            TEXT("Maximum draggable width"),
70     SM_CYMAXTRACK,            TEXT("SM_CYMAXTRACK"),            TEXT("Maximum draggable height"),
71     SM_CXMAXIMIZED,            TEXT("SM_CXMAXIMIZED"),            TEXT("Width of maximized window"),
72     SM_CYMAXIMIZED,            TEXT("SM_CYMAXIMIZED"),            TEXT("Height of maximized windwo"),
73     SM_NETWORK,                TEXT("SM_NETWORK"),                TEXT("Network present flag"),
74     SM_CLEANBOOT,            TEXT("SM_CLEANBOOT"),            TEXT("How system was booted"),
75     SM_CXDRAG,                TEXT("SM_CXDRAG"),                TEXT("Avoid drag x tolerance"),
76     SM_CYDRAG,                TEXT("SM_CYDRAG"),                TEXT("Avoid drag x tolerance"),
77     SM_SHOWSOUNDS,            TEXT("SM_SHOWSOUNDS"),            TEXT("Present sounds visually"),
78     SM_CXMENUCHECK,            TEXT("SM_CXMENUCHECK"),            TEXT("Menu check-mark width"),
79     SM_CYMENUCHECK,            TEXT("SM_CYMENUCHECK"),            TEXT("Menu check-mark height"),
80     SM_SLOWMACHINE,            TEXT("SM_SLOWMACHINE"),            TEXT("Slow processor flag"),
81     SM_MIDEASTENABLED,        TEXT("SM_MIDEASTENABLE"),        TEXT("Hebrew and Arabic enabled flag"),
82     SM_MOUSEWHEELPRESENT,    TEXT("SM_MOUSEWHEELPRESENT"),    TEXT("Mouse wheel present flag"),
83     SM_XVIRTUALSCREEN,        TEXT("SM_XVIRTUALSCREEN"),        TEXT("Virtual screen x origin"),
84     SM_YVIRTUALSCREEN,        TEXT("SM_YVIRTUALSCREEN"),        TEXT("Virtual screen y origin"),
85     SM_CXVIRTUALSCREEN,        TEXT("SM_CXVIRTUALSCREEN"),        TEXT("Virtual screen width"),
86     SM_CYVIRTUALSCREEN,        TEXT("SM_CYVIRTUALSCREEN"),        TEXT("Virtual screen height"),
87     SM_CMONITORS,            TEXT("SM_CMONITORS"),            TEXT("Number of monitors"),
88     SM_SAMEDISPLAYFORMAT,    TEXT("SM_SAMEDISPLAYFORMAT"),    TEXT("Same color format flag")
89 };

图片 3

 关于QCombox如果不需要自定义,其实写UI是很简单的。

SYSMETS.H

 

创建实例:QComboBox*  m_pMicrophoneCombox = new QComboBox;

图片 4图片 5

   
上图中指定了本地的端口,会发生这样的情况。当我们关闭应用程序之后,连接被关闭,按照TCP/IP的协议,连接会话资源会处于一种time_wait的状态,存活一段时间,一般都需要至少30秒以上才能被系统清理。有的系统中甚至需要1分钟左右。这给程序调试带来不便。

我是用的QSS去的写样式,这里用了一个命名函数:m_pMicrophoneCombox->setObjectName(“DeviceCombox”);

  1 /*-----------------------------------------------
  2 SYSMETS4.C -- System Metrics Display Program No.4
  3               (c) Charles Petzold, 1998
  4 -----------------------------------------------*/
  5 
  6 #include <Windows.h>
  7 #include "SYSMETS.h"
  8 
  9 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 10 
 11 int WINAPI WinMain( __in HINSTANCE hInstance
 12                     , __in_opt HINSTANCE hPrevInstance
 13                     , __in LPSTR lpCmdLine
 14                     , __in int nShowCmd )
 15 {
 16     static TCHAR szAppName[] = TEXT("SysMets4");
 17     HWND hwnd;
 18     MSG msg;
 19     WNDCLASS wndclass;
 20 
 21     wndclass.style = CS_HREDRAW | CS_VREDRAW;
 22     wndclass.lpfnWndProc = WndProc;
 23     wndclass.cbClsExtra = 0;
 24     wndclass.cbWndExtra= 0;
 25     wndclass.hInstance = hInstance;
 26     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 27     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
 28     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 29     wndclass.lpszMenuName = NULL;
 30     wndclass.lpszClassName = szAppName;
 31 
 32     if (!RegisterClass(&wndclass))
 33     {
 34         MessageBox(hwnd, TEXT("Program requires Windows NT !")
 35             , szAppName, MB_ICONERROR);
 36         return 0;
 37     }
 38 
 39     hwnd = CreateWindow(szAppName, TEXT("Get System Metrics No.4")
 40         , WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL
 41         , CW_USEDEFAULT, CW_USEDEFAULT
 42         , CW_USEDEFAULT, CW_USEDEFAULT
 43         , NULL, NULL, hInstance, NULL);
 44 
 45     ShowWindow(hwnd, nShowCmd);
 46     UpdateWindow(hwnd);
 47 
 48     while (GetMessage(&msg, NULL, 0, 0))
 49     {
 50         TranslateMessage(&msg);
 51         DispatchMessage(&msg);
 52     }
 53 
 54     return msg.wParam;
 55 }
 56 
 57 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 58 {
 59     static int cxChar, cxCaps, cyChar, cxClient, cyClient, iMaxWidth;
 60     HDC hdc;
 61     int i, x, y, iVertPos, iHorzPos, iPaintBeg, iPaintEnd;
 62     PAINTSTRUCT ps;
 63     SCROLLINFO si;
 64     TCHAR szBuffer[10];
 65     TEXTMETRIC tm;
 66 
 67     switch (message)
 68     {
 69     case WM_CREATE:
 70         hdc = GetDC(hwnd);
 71         GetTextMetrics(hdc, &tm);
 72         cxChar = tm.tmAveCharWidth;
 73         cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
 74         cyChar = tm.tmHeight + tm.tmExternalLeading;
 75         ReleaseDC(hwnd, hdc);
 76 
 77         //save the width of the three columns
 78         iMaxWidth = 40 * cxChar + 22 * cxCaps;
 79         return 0;
 80 
 81     case WM_SIZE:
 82         cxClient = LOWORD(lParam);
 83         cyClient = HIWORD(lParam);
 84 
 85         //set vertical scroll bar range and page size
 86         si.cbSize = sizeof(si);
 87         si.fMask = SIF_RANGE | SIF_PAGE;
 88         si.nMin = 0;
 89         si.nMax = NUMLINES - 1;
 90         si.nPage = cyClient / cyChar;
 91         SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
 92 
 93         //set horizontal scroll bar range and page size
 94         si.cbSize = sizeof(si);
 95         si.fMask = SIF_RANGE | SIF_PAGE;
 96         si.nMin = 0;
 97         si.nMax = 2 + iMaxWidth / cxChar;
 98         si.nPage = cxClient / cxChar;
 99         SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
100         return 0;
101 
102     case WM_VSCROLL:
103         //get all the vertical scroll bar information
104         si.cbSize= sizeof(si);
105         si.fMask = SIF_ALL;
106         GetScrollInfo(hwnd, SB_VERT, &si);
107 
108         //save the position for comparison later on
109         iVertPos = si.nPos;
110 
111         switch (LOWORD(wParam))
112         {
113         case SB_TOP:
114             si.nPos = si.nMin;
115             break;
116 
117         case SB_BOTTOM:
118             si.nPos = si.nMax;
119             break;
120 
121         case SB_LINEUP:
122             si.nPos -= 1;
123             break;
124 
125         case SB_LINEDOWN:
126             si.nPos += 1;
127             break;
128 
129         case SB_PAGEUP:
130             si.nPos -= si.nPage;
131             break;
132 
133         case SB_PAGEDOWN:
134             si.nPos += si.nPage;
135             break;
136 
137         case SB_THUMBTRACK:
138             si.nPos = si.nTrackPos;
139             break;
140 
141         default:
142             break;
143         }
144 
145         //Set the position and then retrieve it. Due to adjustments
146         //by windows it might not be the same as the value set.
147         si.fMask = SIF_POS;
148         SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
149         GetScrollInfo(hwnd, SB_VERT, &si);
150 
151         //if the position has changed, scroll the window and update it
152         if (si.nPos != iVertPos)
153         {
154             ScrollWindow(hwnd, 0, cyChar * (iVertPos - si.nPos), NULL, NULL);
155             UpdateWindow(hwnd);
156         }
157         return 0;
158 
159     case WM_HSCROLL:
160         //get all the horizontal scroll bar information
161         si.cbSize = sizeof(si);
162         si.fMask = SIF_ALL;
163 
164         //save the position for comparison later on
165         GetScrollInfo(hwnd, SB_HORZ, &si);
166         iHorzPos = si.nPos;
167 
168         switch (LOWORD(wParam))
169         {
170         case SB_LINELEFT:
171             si.nPos -= 1;
172             break;
173 
174         case SB_LINERIGHT:
175             si.nPos += 1;
176             break;
177 
178         case SB_PAGELEFT:
179             si.nPos -= si.nPage;
180             break;
181 
182         case SB_PAGERIGHT:
183             si.nPos += si.nPage;
184             break;
185 
186         case SB_THUMBPOSITION:
187             si.nPos = si.nTrackPos;
188             break;
189 
190         default:
191             break;
192         }
193 
194         //Set the position and then retrieve it. Due to adjustments
195         //by window it might not be the same as the value set.
196         si.fMask = SIF_POS;
197         SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
198         GetScrollInfo(hwnd, SB_HORZ, &si);
199 
200         //if the position has changed, scroll the window.
201         if (si.nPos != iHorzPos)
202         {
203             ScrollWindow(hwnd, cxChar * (iHorzPos - si.nPos), 0, NULL, NULL);
204             UpdateWindow(hwnd);
205         }
206         return 0;
207 
208     case WM_KEYDOWN:
209         switch (wParam)
210         {
211         case VK_HOME:
212             SendMessage(hwnd, WM_VSCROLL, SB_TOP, 0);
213             break;
214 
215         case VK_END:
216             SendMessage(hwnd, WM_VSCROLL, SB_BOTTOM, 0);
217             break;
218 
219         case VK_PRIOR:
220             SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, 0);
221             break;
222 
223         case VK_NEXT:
224             SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
225             break;
226 
227         case VK_UP:
228             SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
229             break;
230 
231         case VK_DOWN:
232             SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
233             break;
234 
235         case VK_LEFT:
236             SendMessage(hwnd, WM_HSCROLL, SB_PAGEUP, 0);
237             break;
238 
239         case VK_RIGHT:
240             SendMessage(hwnd, WM_HSCROLL, SB_PAGEDOWN, 0);
241             break;
242         }
243         return 0;
244 
245     case WM_PAINT:
246         hdc = BeginPaint(hwnd, &ps);
247 
248         //get vertical scroll bar position
249         si.cbSize = sizeof(si);
250         si.fMask = SIF_POS;
251         GetScrollInfo(hwnd, SB_VERT, &si);
252         iVertPos = si.nPos;
253 
254         //get horizontal scroll bar position
255         GetScrollInfo(hwnd, SB_HORZ, &si);
256         iHorzPos = si.nPos;
257 
258         //find paintint limits
259         iPaintBeg = max(0, iVertPos + ps.rcPaint.top / cyChar);
260         iPaintEnd = min(NUMLINES - 1, iVertPos + ps.rcPaint.bottom / cyChar);
261 
262         for (i = iPaintBeg; i <= iPaintEnd; ++i)
263         {
264             x = cxChar * (1 - iHorzPos);
265             y = cyChar * (i - iVertPos);
266 
267             TextOut(hdc, x, y, sysmetrics[i].szLabel, lstrlen(sysmetrics[i].szLabel));
268 
269             TextOut(hdc, x + 22 * cxCaps, y, sysmetrics[i].szDesc, lstrlen(sysmetrics[i].szDesc));
270 
271             SetTextAlign(hdc, TA_RIGHT | TA_TOP);
272 
273             TextOut(hdc, x + 22 * cxCaps + 40 * cxChar, y, szBuffer
274                 , wsprintf(szBuffer, TEXT("%5d"), GetSystemMetrics(sysmetrics[i].iIndex)));
275 
276             SetTextAlign(hdc, TA_LEFT | TA_TOP);
277         }
278 
279         EndPaint(hwnd, &ps);
280         return 0;
281 
282     case WM_DESTROY:
283         PostQuitMessage(0);
284         return 0;
285     }
286 
287     return DefWindowProc(hwnd, message, wParam, lParam);
288 }

   
翻阅了网络上的许多地方都找不到问题到底出现在哪。看了许多功能相似的示例代码,都没有出现像我这样的问题。放了一段时间,某天突然开窍,想到肯定是指定了某个唯一的参数,使得重新打开和上一次连接一样的会话导致错误。也就是那么几秒钟时间,把指定的本地端口号这个参数删除——搞定了。

如果是用的setstylesheet直接在代码里去写就好了。

SYSMETS4.C

   
小小总结一下就是假如建立与服务器的连接时,如果不指定端口号,系统会随机分配一个或者函数的底层会随机领一个(我猜)。但指定之后,一旦上一次的会话进入time_wait状态时,当然不能够立即再次利用一样的会话资源。这应该是很不安全的,不是吗?

下面说几个属性函数:

 

    问题居然是藏在这个特别不起眼的”坑“里有木有?

m_pMicrophoneCombox->setMaxVisibleItems(2);//最大可见区域为显示两条。

    希望遇到和我一样问题的童鞋能够有所帮助。

m_pMicrophoneCombox->setEditable(false); //设置QComboBox可编辑。

m_pMicrophoneCombox->setFocusPolicy(Qt::NoFocus);//设置QCombox没有焦点。

m_pMicrophoneCombox->setLineEdit(edit);//edit是一个lineedit,就是说QCombox的LineEdit可以自定义,而QT的源码中,顶端也是一个LineEdit。

m_pMicrophoneCombox->setCurrentIndex(0); // 设置当前选中的索引。

发表评论

电子邮件地址不会被公开。 必填项已用*标注