Advertisement
ASP_Volume2 Forms Validation Processing #39970

Validation

If you're looking for a very thorough validation routine, look no further.

AI

AI 摘要: 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.

源代码
original-source
Upload
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<META NAME="Generator" CONTENT="Internet Assistant for Word Version 3.0">
</HEAD>
<BODY>
<B><FONT SIZE=5><P>How to Read from and Write to Registry in WIN32</P>
</B></FONT><P>Suppose you want your application to be able to save the position and size of its main window when ending and restore it in startup. First, let me show how to write its position to the registry:</P>
<P>( Both for reading from and writing to the registry, you must include &lt;winreg.h&gt;)</P>
<P>Inside the WM_CLOSE message handler, you could put:</P>
<P>HKEY hParentKey; <FONT COLOR="#008000">/* handle to the parent key */</P>
</FONT><P>RECT rect = {0, 0, 0, 0};</P>
<P>SECURITY_ATTRIBUTES sa = {<FONT COLOR="#0000ff">sizeof</FONT>(sa), 0,1}; <FONT COLOR="#008000">/*since it will be ignored in some OSs, lets set it to the default setting */</P>
</FONT><P>DWORD state = 0; <FONT COLOR="#008000">/* this will hold an integer that will say if a new key was created or an existing key was opened */</P>
</FONT><P>GetWindowRect(hwnd, &rect); <FONT COLOR="#008000">/* get the position of the window */</P>
</FONT>
<P>RegCreateKeyEx ( <FONT COLOR="#008000">/* creates a new key, or opens an existing key */</P>
</FONT><P>HKEY_CURRENT_USER, <FONT COLOR="#008000">/* handle to the base key; could also be a handle to an opened key */</P>
</FONT><P>“Software\\MyCompany\\MyApp”, <FONT COLOR="#008000">/* name of the subkey to open or create */</P>
</FONT><P>0, <FONT COLOR="#008000">/* reserved; should be set to 0 */</P>
</FONT><P>“”, <FONT COLOR="#008000">/* name of the class or object type of the key. You can also specify an empty string */</P>
</FONT><P>KEY_ALL_ACCESS, <FONT COLOR="#008000">/* level of access desired; this allows any access*/</P>
</FONT><P>0, <FONT COLOR="#008000">/* if 0, the key will be saved to the registry; if 1, the key will disappear when window shut down */</P>
</FONT><P>&sa, <FONT COLOR="#008000">/* security attributes; will be ignored in win 9x */</P>
</FONT><P>&hParentKey, <FONT COLOR="#008000">/* will receive the handle to the key */</P>
</FONT><P>&state <FONT COLOR="#008000">/* will receive 1 if the key was created, or 2 if it was opened */</P>
</FONT><P>);</P>
<P>RegSetValueEx( <FONT COLOR="#008000">/* save the values of left, top, right and bottom */</P>
</FONT><P>hParentKey, <FONT COLOR="#008000">/* handle to the key under which lies the subkey */</P>
</FONT><P>“Left”,<FONT COLOR="#008000"> /* name of the subkey */</P>
</FONT><P>0, <FONT COLOR="#008000">/* reserved - must be 0 */</FONT> </P>
<P>REG_DWORD,<FONT COLOR="#008000"> /* we are reading a 32-bit integer */</P>
</FONT><P>(CONST BYTE*) &rect.left, <FONT COLOR="#008000">/* address of whatever we want to write */</P>
</FONT><FONT COLOR="#0000ff"><P>sizeof</FONT>(rect.left) <FONT COLOR="#008000">/* size of whatever was passed in above argument */</P>
</FONT><P>);</P>
<FONT COLOR="#008000"><P>/* lets do the same with the other 3 values */</P>
</FONT><P>RegSetValueEx(hParentKey, “Top”, 0, REG_DWORD, (CONST BYTE*) &rect.top, <FONT COLOR="#0000ff">sizeof</FONT>(rect.top) );</P>
<P>RegSetValueEx(hParentKey, “Bottom”, 0, REG_DWORD, (CONST BYTE*) &rect.bottom, <FONT COLOR="#0000ff">sizeof</FONT>(rect.bottom) );</P>
<P>RegSetValueEx(hParentKey, “Right”, 0, REG_DWORD, (CONST BYTE*) &rect.right, <FONT COLOR="#0000ff">sizeof</FONT>(rect.right) );</P>
<FONT COLOR="#008000"><P>/* now lets close the handle to the parent key */</P>
</FONT>
<P>RegCloseKey(hParentKey);</P>
<P>Good. Now we have learnt how to save the values. But, how to read them and move the window appropriately? That’s a way of doing that:</P>
<P>Inside the WM_CREATE message handler, put:</P>
<P>HKEY hParentKey;</P>
<P>RECT rect = {0, 0, 0, 0};</P>
<P>SECURITY_ATTRIBUTES sa = {<FONT COLOR="#0000ff">sizeof</FONT>(sa), 0, 1};</P>
<P>DWORD size = 0, state = 0, type = 0;</P>
<P>RegCreateKeyEx ( <FONT COLOR="#008000">/* creates a new key, or opens an existing key */</P>
</FONT><P>HKEY_CURRENT_USER, <FONT COLOR="#008000">/* handle to the base key; could also be a handle to an opened key */</P>
</FONT><P>“Software\\MyCompany\\MyApp”, <FONT COLOR="#008000">/* name of the subkey to open or create */</P>
</FONT><P>0, <FONT COLOR="#008000">/* reserved should be set to 0 */</P>
</FONT><P>“”, <FONT COLOR="#008000">/* name of the class or object type of the key. You can also specify an empty string */</P>
</FONT><P>KEY_ALL_ACCESS, <FONT COLOR="#008000">/* level of access desired; this allows any access*/</P>
</FONT><P>0, <FONT COLOR="#008000">/* if 0, the key will be saved to the registry; if 1, the key will disappear when window shut down */</P>
</FONT><P>&sa, <FONT COLOR="#008000">/* security attributes; will be ignored in win 9x */</P>
</FONT><P>&hParentKey, <FONT COLOR="#008000">/* will receive the handle to the key */</P>
</FONT><P>&state <FONT COLOR="#008000">/* will receive 1 if the key was created, or 2 if it was opened */</P>
</FONT><P>);</P>
<FONT COLOR="#0000ff"><P>if</FONT>( state == 2) <FONT COLOR="#008000">/* opened existing key */</P>
</FONT><P>{</P>
<P> size = <FONT COLOR="#0000ff">sizeof</FONT>(rect.left); </P>
<P> RegQueryValueEx( <FONT COLOR="#008000">/* read the value from registry */</P>
<P> </FONT>hParentKey, <FONT COLOR="#008000">/* handle to tha parent key */</P>
<P> </FONT>“Left”, <FONT COLOR="#008000">/* name of the value to read */</P>
<P> </FONT>0, <FONT COLOR="#008000"> /* reserved - must be 0 */</P>
<P> </FONT>&type, <FONT COLOR="#008000">/* this will receive the type of the value read; should receive REG_DWORD /*</P>
<P> </FONT>(BYTE*) &rect.left, <FONT COLOR="#008000">/* this will receive the data read */</P>
<P> </FONT>&size <FONT COLOR="#008000">/* set this to the size of what was passed above; will also receive the size of what was actually read */</P>
</FONT><P> );</P>
<P> <FONT COLOR="#008000">/* lets do the same with the other 3 values */</P>
</FONT><P> size = <FONT COLOR="#0000ff">sizeof</FONT>(rect.top); </P>
<P> RegQueryValueEx(hParentKey, “Top”, 0, &type, (BYTE*) &rect.top, &size);</P>
<P> size = <FONT COLOR="#0000ff">sizeof</FONT>(rect.bottom);</P>
<P>RegQueryValueEx(hParentKey, “Bottom”, 0, &type, (BYTE*) &rect.bottom, &size );</P>
<P>size = <FONT COLOR="#0000ff">sizeof</FONT>(rect.right);</P>
<P>RegQueryValueEx(hParentKey, “Right”, 0, &type, (BYTE*) &rect.right, &size)</P>
<P>}</P>
<FONT COLOR="#0000ff"><P>else </FONT><FONT COLOR="#008000">/* created a new one */</P>
</FONT><P>{</P>
<P> rect.top = rect.left = 0; <FONT COLOR="#008000">/* default position */</P>
</FONT><P> rect.right = rect.bottom = 200; <FONT COLOR="#008000">/* default height and width */</P>
</FONT><P> <FONT COLOR="#008000">/* save them to the registry */</P>
</FONT><P> RegSetValueEx(hParentKey, “Left”, 0, REG_DWORD, (CONST BYTE*) &rect.left, <FONT COLOR="#0000ff">sizeof</FONT>(rect.left)<FONT COLOR="#0000ff"> </FONT>);</P>
<P> </P>
<P> RegSetValueEx(hParentKey, “Top”, 0, REG_DWORD, (CONST BYTE*) &rect.top, <FONT COLOR="#0000ff">sizeof</FONT>(rect.top) );</P>
<P> </P>
<P> RegSetValueEx(hParentKey, “Bottom”, 0, REG_DWORD, (CONST BYTE*) &rect.bottom, <FONT COLOR="#0000ff">sizeof</FONT>(rect.bottom) );</P>
<P> </P>
<P> RegSetValueEx(hParentKey, “Right”, 0, REG_DWORD, (CONST BYTE*) &rect.right, <FONT COLOR="#0000ff">sizeof</FONT>(rect.right) );</P>
<P>}</P>
<FONT COLOR="#008000">
<P>/* now that we’ve read the values, move the window appropriately */</P>
</FONT>
<P>SetWindowPos( <FONT COLOR="#008000">/* move the window and set its size */</P>
</FONT><P>hwnd, <FONT COLOR="#008000">/* window to move */</P>
</FONT><P>0, <FONT COLOR="#008000">/* position in Z-order to insert the window */</P>
</FONT><P>rect.left, <FONT COLOR="#008000">/* left coordinate of the upper left corner*/</P>
</FONT><P>rect.top, <FONT COLOR="#008000">/* top coordinate of the upper left corner*/</P>
</FONT><P>rect.right, <FONT COLOR="#008000">/*left coordinate of the lower right corner*/</P>
</FONT><P>rect.bottom, <FONT COLOR="#008000">/* top coordinate of the lower right corner*/</P>
</FONT><P>SWP_NOACTIVATE | SWP_NOZORDER <FONT COLOR="#008000">/* do not activate the window unless it was already active; do not change its position in Z-order. */</P>
</FONT><P>);</P>
<P>That’s it. Hope I could help you.</P>
</BODY>
</HTML>
原始评论 (3)
从 Wayback Machine 恢复