a Better way of testing if connected to the NET
Ok i had previously posted the Improper way of doin a test for fun :) I just ran across this code on MVPS.ORG This code uses the wininet.dll VIA API calls, and it will tell you if you are connected and wether you are connected via LAN, modem, or proxy ! I had to post this up since it seems many ppl are lookin for somethin like this.
AI
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Source Code
'This code was copied from http://www.mvps.org/vbnet/ ! Go checkem out
'START .BAS MODULE CODE
Option Explicit
Public Declare Function InternetGetConnectedState _
Lib "wininet.dll" (ByRef lpdwFlags As Long, _
ByVal dwReserved As Long) As Long
'Local system uses a modem to connect to the Internet.
Public Const INTERNET_CONNECTION_MODEM As Long = &H1
'Local system uses a LAN to connect to the Internet.
Public Const INTERNET_CONNECTION_LAN As Long = &H2
'Local system uses a proxy server to connect to the Internet.
Public Const INTERNET_CONNECTION_PROXY As Long = &H4
'No longer used.
Public Const INTERNET_CONNECTION_MODEM_BUSY As Long = &H8
Public Const INTERNET_RAS_INSTALLED As Long = &H10
Public Const INTERNET_CONNECTION_OFFLINE As Long = &H20
Public Const INTERNET_CONNECTION_CONFIGURED As Long = &H40
'InternetGetConnectedState wrapper functions
Public Function IsNetConnectViaLAN() As Boolean
Dim dwflags As Long
'pass an empty varialbe into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)
'return True if the flags indicate a LAN connection
IsNetConnectViaLAN = dwflags And INTERNET_CONNECTION_LAN
End Function
Public Function IsNetConnectViaModem() As Boolean
Dim dwflags As Long
'pass an empty varialbe into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)
'return True if the flags indicate a modem connection
IsNetConnectViaModem = dwflags And INTERNET_CONNECTION_MODEM
End Function
Public Function IsNetConnectViaProxy() As Boolean
Dim dwflags As Long
'pass an empty varialbe into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)
'return True if the flags indicate a proxy connection
IsNetConnectViaProxy = dwflags And INTERNET_CONNECTION_PROXY
End Function
Public Function IsNetConnectOnline() As Boolean
'no flags needed here - the API returns True
'if there is a connection of any type
IsNetConnectOnline = InternetGetConnectedState(0&, 0&)
End Function
Public Function IsNetRASInstalled() As Boolean
Dim dwflags As Long
'pass an empty varialbe into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)
'return True if the falgs include RAS installed
IsNetRASInstalled = dwflags And INTERNET_RAS_INSTALLED
End Function
Public Function GetNetConnectString() As String
Dim dwflags As Long
Dim msg As String
'build a string for display
If InternetGetConnectedState(dwflags, 0&) Then
If dwflags And INTERNET_CONNECTION_CONFIGURED Then
msg = msg & "You have a network connection configured." & vbCrLf
End If
If dwflags And INTERNET_CONNECTION_LAN Then
msg = msg & "The local system connects to the Internet via a LAN"
End If
If dwflags And INTERNET_CONNECTION_PROXY Then
msg = msg & ", and uses a proxy server. "
Else: msg = msg & "."
End If
If dwflags And INTERNET_CONNECTION_MODEM Then
msg = msg & "The local system uses a modem to connect to the Internet. "
End If
If dwflags And INTERNET_CONNECTION_OFFLINE Then
msg = msg & "The connection is currently offline. "
End If
If dwflags And INTERNET_CONNECTION_MODEM_BUSY Then
msg = msg & "The local system's modem is busy with a non-Internet connection. "
End If
If dwflags And INTERNET_RAS_INSTALLED Then
msg = msg & "Remote Access Services are installed on this system."
End If
Else
msg = "Not connected to the internet now."
End If
GetNetConnectString = msg
End Function
' END MODULE CODE
'##############################
'START FORM CODE
Option Explicit
' Put 6 textboxes and 1 Command button and fire it up !
Private Sub Command1_Click()
Text1 = IsNetConnectViaLAN()
Text2 = IsNetConnectViaModem()
Text3 = IsNetConnectViaProxy()
Text4 = IsNetConnectOnline()
Text5 = IsNetRASInstalled()
Text6 = GetNetConnectString()
End Sub
<html>
<head>
<meta name="keywords" content="windows, programming, GUI, interface, design, region, window, button, buttons, VC, cpp, MFC, c++, developer, studio, education, bitmap, application, dialog, paint, project">
<title>MFC, Round Windows and Highlight Buttons</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p> </p>
<h2><font color="#0000A0">MFC, Round Windows and Highlight Buttons</font></h2>
<h3><font color="#0000A0">by Alex Rest</font> </h3>
<p><u>Environment</u> : VC6, VC7, Windows 9x/NT/2000/XP</p>
<p>  If you tired to create programs based on standard
gray-rectangles Windows interface this article for you. It describes creating
windows with original shapes, backgrounds and buttons. The VC++ v6.0 demo project
is attached.</p>
<p align="center"> <img src="RoundWindow.gif" width="221" height="190" border="1"></p>
<h3><font color="#0000A0">Starting</font></h3>
<p>  The first you must to create bitmap with images what you need. You could use
any painter program as Adobe Photoshop, Corel Paint, etc. You must design background
image of your window and all buttons images in different conditions. Standard
Windows button conditions are normal, pressed, disabled, default. To use highlight
condition you need create some additional code. Also you need include your background
bmp file into project resources. </p>
<h3><font color="#0000A0">Project creating</font></h3>
<p>  In most cases non-standard design is used for Dialog Windows. This project
example is based on a Dialog MFC Project. I created the Dialog Project, opened
recourse editor, removed title and borders of dialog (using properties menu).
Working with such window is easier. If you have title you must calculate its
size because the title size is depended from Windows Settings. If you have not
title and borders all window rectangle is client aria. Using WM_PAINT message
we can draw all over window rectangle.<br>
This demo dialog includes only one button "OK". To repaint button
from your code you need switch off parameter "Default Button" and
switch on parameter "Owner Draw". You could do it using resource editor.</p>
<h3><font color="#0000A0">Window region</font></h3>
<p>  Visible edge of a window could be any shape. The shape is determined by region.
CRgn class supports rectangles, round rectangles, elliptical and polygon regions.
More than you could combine regions. For example, when I was creating Color
Chains game (http://www.brigsoft.com/colorch) I created a window that looks
like three round rectangles. In the BSAlarm project (http://www.brigsoft.com/bsalarm)
I used combination of round rectangle region and elliptical region.<br>
 I do not change window region during program execution. So I attach region to
window before it is shown. The best place to do it is OnCreate() message handler
for normal window or OnInitDialog() for dialog window. Use wizard and WM_CREATE
(WM_INITDIALOG) messages to insert these functions to your project. <br>
Here is OnInitDialog() from the demo project:</p>
<p><font size="-1">BOOL CRoundWindowDlg::OnInitDialog()<br>
{<br>
 CDialog::OnInitDialog();<br>
<br>
 VERIFY( SetWindowPos( NULL, 0, 0, m_nW, m_nH, SWP_NOMOVE
| SWP_NOOWNERZORDER ) );<br>
  VERIFY( m_WinRgn.CreateEllipticRgn( 0, 0, m_nW,
m_nH ) );<br>
  VERIFY( SetWindowRgn(m_WinRgn , TRUE ) );</font></p>
<p><font size="-1">   m_ExitBtn.Move();<br>
<br>
  return TRUE; <br>
}</font></p>
<p>  I use SetWindowPos to set size of window according background image. It is
much easier than make dialog window size using resource editor.<br>
Pay attention to m_WinRgn. The region is a member of CRoundWindowDlg class.
So it creates before window creating and removes after window destroying. It
is important. Using of local variable for window region could cause an error.
</p>
<h3><font color="#0000A0">Drawing of background</font></h3>
<p>  You can draw any image into a dialog window as into a plain window using WM_PAINT
message or OnPaint() MCF handler. Draw background before other images. The drawing
is trivial. Here is a function from the demo project:</p>
<p><font size="-1">void CRoundWindowDlg::OnPaint() <br>
{<br>
 CPaintDC dc(this); // device context for painting</font></p>
<p> <font size="-1">  CBitmap BkBmp, *pOldBmp;<br>
  BkBmp.LoadBitmap(IDB_BKBITMAP);<br>
  CDC BmpDc;<br>
  VERIFY( BmpDc.CreateCompatibleDC(&dc) );<br>
  pOldBmp = (CBitmap *)BmpDc.SelectObject(&BkBmp);<br>
  dc.BitBlt(0,0,m_nW,m_nH,&BmpDc,0,0,SRCCOPY);<br>
  BmpDc.SelectObject(pOldBmp);<br>
}</font></p>
<p>It is draw background only.</p>
<h3><font color="#0000A0">Cool buttons</font></h3>
<p>  The highlight button status is not standard for Windows. So we must catch WM_MOUSEMOVE
message and do highlight in our code. To do such functionality I created CColorBtn
class that based on CButton. Using Wizard I created button member in demo project
and than change CBurtton to CColorBtn in the dialog window class declaration.<br>
Class CColorBtn uses same bitmap recourse as the background window. Button conditions
images are placed below dialog window background image. The drawing process
is trivial for "Owner Draw" buttons: </p>
<p><font size="-1">void CColorBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
<br>
{</font></p>
<blockquote>
<p><font size="-1"> CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);<br>
CDC BmpDC;</font></p>
<p><font size="-1"> VERIFY( BmpDC.CreateCompatibleDC(pDC) );<br>
CBitmap tmpBmp;<br>
tmpBmp.LoadBitmap(IDB_BKBITMAP);<br>
CBitmap *pOldBmp = (CBitmap *)BmpDC.SelectObject(&tmpBmp);</font></p>
<p><font size="-1"> switch(m_nStatus){<br>
case Normal:<br>
</font>
<blockquote><font size="-1"> pDC->BitBlt(lpDrawItemStruct->rcItem.left,<br>
lpDrawItemStruct->rcItem.top, <br>
lpDrawItemStruct->rcItem.right - lpDrawItemStruct->rcItem.left, <br>
lpDrawItemStruct->rcItem.bottom -lpDrawItemStruct->rcItem.top, <br>
&BmpDC, m_nBmpX, m_nBmpY, SRCCOPY);<br>
break;<br>
</font></blockquote>
<font size="-1">case Light:<br>
</font>
<blockquote><font size="-1"> pDC->BitBlt(lpDrawItemStruct->rcItem.left,<br>
lpDrawItemStruct->rcItem.top, <br>
lpDrawItemStruct->rcItem.right - lpDrawItemStruct->rcItem.left, <br>
lpDrawItemStruct->rcItem.bottom -lpDrawItemStruct->rcItem.top, <br>
&BmpDC, m_nBmpX+m_nW, m_nBmpY, SRCCOPY);<br>
break;<br>
</font></blockquote>
<font size="-1">case Pressed:<br>
</font>
<blockquote><font size="-1"> pDC->BitBlt(lpDrawItemStruct->rcItem.left,<br>
lpDrawItemStruct->rcItem.top, <br>
lpDrawItemStruct->rcItem.right - lpDrawItemStruct->rcItem.left, <br>
lpDrawItemStruct->rcItem.bottom -lpDrawItemStruct->rcItem.top, <br>
&BmpDC, m_nBmpX+m_nW*2, m_nBmpY, SRCCOPY);<br>
break;<br>
</font></blockquote>
<font size="-1">}<br>
<br>
BmpDC.SelectObject(pOldBmp);<br>
</font></blockquote>
<font size="-1">} </font>
<p><br>
Value of m_nStatus member is assigned in OnLButtonDown(), OnLButtonUp() and
OnMouseMove() functions. OnLButtonDown(), OnLButtonUp() are clear. OnMouseMove()
I had implemented so:</p>
<p><font size="-1">void CColorBtn::OnMouseMove(UINT nFlags, CPoint point) <br>
{<br>
</font>
<blockquote>
<p><font size="-1"> if(GetCapture()!=this){<br>
</font><font size="-1">  SetCapture(); <br>
} </font> </p>
<p><font size="-1"> CRect WinRect(0,0,m_nW, m_nH);</font></p>
<p><font size="-1"> if(WinRect.PtInRect(point)){<br>
  m_nStatus = Light;  <br>
} <br>
else{</font><br>
<blockquote><font size="-1"> m_nStatus = Normal;<br>
if(GetCapture()==this){<br>
</font>
<blockquote><font size="-1"> bool bRet = ReleaseCapture();<br>
if(!bRet){</font> <font size="-1"><br>
  ASSERT(0);</font> <font size="-1"><br>
}<br>
</font></blockquote>
<font size="-1">}
</font></blockquote>
<font size="-1">} </font>
<p><font size="-1"> CButton::OnMouseMove(nFlags, point);<br>
Invalidate(FALSE);<br>
UpdateWindow();</font></p>
</blockquote>
<font size="-1">} </font>
<p>  I hade captured mouse to determine the moment when mouse pointer left the button
shape. Other ways is using TRACKMOUSEEVENT or checking mouse pointer from OnTime(),
OnIdle() functions.<br>
 Button coordinates are assigned in the class constructor. Move() function places
button window to its coordinates.</p>
<h3><font color="#0000A0">Radio Buttons and Check Boxes</font></h3>
<p>  Radio Buttons and Check Boxes have not "Owner Draw" parameter. In
this demo project I have not used this buttons. But I used them before. <br>
As any windows this buttons have window procedure. If we catch this procedure
we can change any window functionality. We need catch drawing. To do so:<br>
 1) Using GetDlgItem function we could receive the handle of button window(SDK)
or pointer to CWnd (MFC); <br>
 2) Using GetWindowLong function we can change window proc function to our code.<br>
 3) Then we draw what we need while process WM_ENABLE, BM_SETSTATE, BM_SETCHECK,
WM_PAINT messages. Do not ask me why it so. All questions send to Microsoft;-)
I was tracing all button messages to define when drawing took place.</p>
<h3><font color="#0000A0">Links</font></h3>
<p>The technique was described in this article I used in the projects:<br>
BSAlarm, <a href="http://www.brigsoft.com/bsalarm">http://www.brigsoft.com/bsalarm</a><br>
Color Chains game, <a href="http://www.brigsoft.com/colorch">http://www.brigsoft.com/colorch</a></p>
<p align="right">(C) Alex Rest, 2002<br>
</p>
</body>
</html>
Original Comments (3)
Recovered from Wayback Machine