⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winprog.html

📁 黑客培训教程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
this message. WM_QUIT will cause the loop to return 0 and to exit the loop and also
close our application.</p>
<font size="4"><b><a name="1_4">1.4 Text</a></b></font><br>
<font size="2"><a href="http://blacksun.box.sk/aztek/winprog/examples/section_1_4.zip">Source</a> - <a href="http://blacksun.box.sk/aztek/winprog/images/section_1_4.gif">Screen Shot</a><br><br></font>
<p>
Now most windows programmers (including myself) never memorize much of that. We
usually take a simple window application like that and use it as a skelaton for
all of our new applications, adding what we need and changning things. This is what
I am going to do throughout the entire tutorial. I will take this code form the
last section and I will reuse it in all outher applications that have a window.
Now next up how do we write text to our window? This to is not as simple as it may
seem.<br><br>
<table border="0" cellpadding="5" bgcolor="#26343e" align="center">
<tr>
   <td nowrap>
   <tt>
   #include &lt;windows.h><br>
   <br>
   LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);<br>
   <br>
   static char gszClassName[] &nbsp;= &quot;MyWindowClass&quot;;<br>
   static HINSTANCE ghInstance = NULL;<br>
   <br>
   int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WNDCLASSEX WndClass;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HWND hwnd;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MSG Msg;<br>
   <br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ghInstance = hInstance;<br>
   <br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WndClass.cbSize &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= sizeof(WNDCLASSEX);<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WndClass.style &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= NULL;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WndClass.lpfnWndProc &nbsp;&nbsp;= WndProc;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WndClass.cbClsExtra &nbsp;&nbsp;&nbsp;= 0;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WndClass.cbWndExtra &nbsp;&nbsp;&nbsp;= 0;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WndClass.hInstance &nbsp;&nbsp;&nbsp;&nbsp;= ghInstance;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WndClass.hIcon &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= LoadIcon(NULL, IDI_APPLICATION);<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WndClass.hCursor &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= LoadCursor(NULL, IDC_ARROW);<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WndClass.lpszMenuName &nbsp;= NULL;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WndClass.lpszClassName = gszClassName;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WndClass.hIconSm &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= LoadIcon(NULL, IDI_APPLICATION);<br>
   <br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!RegisterClassEx(&amp;WndClass)) {<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MessageBox(0, &quot;Window Registration Failed!&quot;, &quot;Error!&quot;, MB_ICONSTOP | MB_OK);<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
   <br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hwnd = CreateWindowEx(<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WS_EX_STATICEDGE,<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gszClassName,<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Windows Title&quot;,<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WS_OVERLAPPEDWINDOW,<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CW_USEDEFAULT, CW_USEDEFAULT,<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;320, 240,<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL, NULL,<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ghInstance,<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL);<br>
   <br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(hwnd == NULL) {<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MessageBox(0, &quot;Window Creation Failed!&quot;, &quot;Error!&quot;, MB_ICONSTOP | MB_OK);<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
   <br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShowWindow(hwnd, nCmdShow);<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UpdateWindow(hwnd);<br>
   <br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(GetMessage(&amp;Msg, NULL, 0, 0)) {<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TranslateMessage(&amp;Msg);<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DispatchMessage(&amp;Msg);<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return Msg.wParam;<br>
   }<br>
   <br>
   LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HDC hdc;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PAINTSTRUCT ps;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LPSTR szMessage = &quot;Hello World&quot;;<br>
   <br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch(Message) {<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case WM_PAINT:<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case WM_DESTROY:<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PostQuitMessage(0);<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return DefWindowProc(hwnd, Message, wParam, lParam);<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br>
   }
   </tt>
   </td>
</tr>
</table><br><br>
This prints Hello World out on the screen, not very interesting I know, but we are
learning. First to exapling what I added. At the top of the WinProc I added a few
variables. hdc Identifies the display DC to be used for painting. ps is our paint
structure the PAINTSTRUCT structure contains information for an application. This
information can be used to paint the client area of a window owned by that application.
message is just a simple character array that hold our message.<br>
<br>
After that we can see i also added another case. Remember when I said above in Section
1.3 how the UpdateWidnow() send the message WM_PAINT, well here we can use it.
First we tell hdc to recive painting information from BeginPaint(). BeginPaint()
takes the following parameters:<br>
<br>
<dl>
  <dt>hwnd</dt>
  <dd>Identifies the window to be painted.</dd>
  <dt>pPaint</dt>
  <dd>Pointer to the PAINTSTRUCT structure that will receive painting information.</dd>
</dl>
Then we use TextOut() to print text to the screen. TextOut() takes the following
parameters:<br>
<br>
<dl>
  <dt>hdc</dt>
  <dd>Identifies the device context.</dd>
  <dt>nXStart</dt>
  <dd>Specifies the logical x-coordinate of the reference point that Windows uses to align the string.</dd>
  <dt>nYStart</dt>
  <dd>Specifies the logical y-coordinate of the reference point that Windows uses to align the string.</dd>
  <dt>lpString</dt>
  <dd>Points to the string to be printe dout (The string does not have to be \0 terminated, because cbString is the length)</dd>
  <dt>cbString</dt>
  <dd>Specifies the number of characters in the string.</dd>
</dl>
Then EndPaint() tells the program to stop painting the screen. EndPaint() takes
the following parameters:<br>
<br>
<dl>
  <dt>hwnd</dt>
  <dd>Identifies the window that was painted.</dd>
  <dt>lpPaint</dt>
  <dd>Points to a PAINTSTRUCT structure that contains the painting information retrieved by BeginPaint.</dd>
</dl>
</p>
<font size="4"><b><a name="1_5">1.5 Menus</a></b></font><br><br>
<p>When programming there are usually a few ways to do things, making a windows
menu is no different. I am going to go over the two main ways you can make a menu.
I will have each one make the same menu but it will show you different ways of doing
things.</p>
<font size="4"><b><a name="1_5_1">1.5.1 Resources</a></b></font><br>
<font size="2"><a href="http://blacksun.box.sk/aztek/winprog/examples/section_1_5_1.zip">Source</a> - <a href="http://blacksun.box.sk/aztek/winprog/images/section_1_5.gif">Screen Shot</a><br><br></font>
<p>Resources is probably the simplest way of making a menu. The menu is predefined
in a resource file (always something.rc). The resource files are compiled by the
resource compiler and linked to the program when the linker runs. This is the first
program where I am also going to throw other files besides the source files in.
There will be a resource file (*.rc) and a header file (*.h) in the zip along with
a make file. Type make when you are in the directory of the source and Borlands
should compile and link it all in the right order.<br><br>
<table border="0" cellpadding="5" bgcolor="#26343e" align="center">
<tr>
   <td nowrap>
   <tt>
   #define ID_FILE_NEW &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1000<br>
   #define ID_FILE_OPEN &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1001<br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -