<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Блог Stafox&#039;a &#187; c++</title>
	<atom:link href="/tag/cpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://stafox.ru</link>
	<description>О программировании и создании сайтов</description>
	<lastBuildDate>Sat, 18 May 2013 12:07:57 +0000</lastBuildDate>
	<language>ru-RU</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Visual C++ Win32: сокращаем код начальной программы</title>
		<link>http://stafox.ru/visual-c-win32-sokrashhaem-kod-nachalnoj-programmy/</link>
		<comments>http://stafox.ru/visual-c-win32-sokrashhaem-kod-nachalnoj-programmy/#comments</comments>
		<pubDate>Sat, 29 May 2010 15:42:35 +0000</pubDate>
		<dc:creator>Stafox</dc:creator>
				<category><![CDATA[WinAPI]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[win32]]></category>
		<category><![CDATA[winapi]]></category>

		<guid isPermaLink="false">http://stafox.ru/?p=23</guid>
		<description><![CDATA[Здравствуйте, дорогие читатели. Это моя первая статья, и я хочу посвятить её Visual C++, а именно Win32. В процессе написания своей курсовой работы на этом языке программирования, я столкнулся с некоторыми проблемами и неудобствами, которые касаются по большому счёту Microsoft Visual Studio 2008. В этой статье я расскажу как можно уменьшить код на 120 строк [...]]]></description>
				<content:encoded><![CDATA[<p><a href="visual-c-win32-sokrashhaem-kod-nachalnoj-programmy"><img src="/wp-content/uploads/2010/05/programm-code.jpg" alt="Оптимизируем код" width="580" height="200" class="aligncenter size-full wp-image-1748" /></a><br />
Здравствуйте, дорогие читатели. Это моя первая статья, и я хочу посвятить её Visual C++, а именно Win32. В процессе написания своей курсовой работы на этом языке программирования, я столкнулся с некоторыми проблемами и неудобствами, которые касаются по большому счёту Microsoft Visual Studio 2008. В этой статье я расскажу как можно уменьшить код на 120 строк без всякого труда. Начну, пожалуй, с создания проекта. <span id="more-23"></span><br />
<strong>File</strong> → <strong>New</strong> → <strong>Project</strong> → <strong>Visual C++</strong> → <strong>Win32</strong> → <strong>Win32 Project</strong></p>
<p>Обзовём наш проект <em>example</em>. Вот код стандартного проекта:</p>
<pre class="brush: cpp; title: ; notranslate">
// example.cpp : Defines the entry point for the application.
//

#include &quot;stdafx.h&quot;
#include &quot;example.h&quot;

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;	// current instance
TCHAR szTitle[MAX_LOADSTRING];	   // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];	// the main window class name

// Forward declarations of functions included in this code module:
ATOM	MyRegisterClass(HINSTANCE hInstance);
BOOL	InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPTSTR    lpCmdLine,
 int       nCmdShow)
{
 UNREFERENCED_PARAMETER(hPrevInstance);
 UNREFERENCED_PARAMETER(lpCmdLine);

 // TODO: Place code here.
 MSG msg;
 HACCEL hAccelTable;

 // Initialize global strings
 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
 LoadString(hInstance, IDC_EXAMPLE, szWindowClass, MAX_LOADSTRING);
 MyRegisterClass(hInstance);

 // Perform application initialization:
 if (!InitInstance (hInstance, nCmdShow))
 {
 return FALSE;
 }

 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_EXAMPLE));

 // Main message loop:
 while (GetMessage(&amp;msg, NULL, 0, 0))
 {
 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &amp;msg))
 {
 TranslateMessage(&amp;msg);
 DispatchMessage(&amp;msg);
 }
 }

 return (int) msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
 WNDCLASSEX wcex;

 wcex.cbSize = sizeof(WNDCLASSEX);

 wcex.style		= CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc	= WndProc;
 wcex.cbClsExtra	= 0;
 wcex.cbWndExtra	= 0;
 wcex.hInstance		= hInstance;
 wcex.hIcon		= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_EXAMPLE));
 wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_EXAMPLE);
 wcex.lpszClassName	= szWindowClass;
 wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

 return RegisterClassEx(&amp;wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//   In this function, we save the instance handle in a global variable and
//   create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
 HWND hWnd;

 hInst = hInstance; // Store instance handle in our global variable

 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

 if (!hWnd)
 {
 return FALSE;
 }

 ShowWindow(hWnd, nCmdShow);
 UpdateWindow(hWnd);

 return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 int wmId, wmEvent;
 PAINTSTRUCT ps;
 HDC hdc;

 switch (message)
 {
 case WM_COMMAND:
 wmId    = LOWORD(wParam);
 wmEvent = HIWORD(wParam);
 // Parse the menu selections:
 switch (wmId)
 {
 case IDM_ABOUT:
 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
 break;
 case IDM_EXIT:
 DestroyWindow(hWnd);
 break;
 default:
 return DefWindowProc(hWnd, message, wParam, lParam);
 }
 break;
 case WM_PAINT:
 hdc = BeginPaint(hWnd, &amp;ps);
 // TODO: Add any drawing code here...
 EndPaint(hWnd, &amp;ps);
 break;
 case WM_DESTROY:
 PostQuitMessage(0);
 break;
 default:
 return DefWindowProc(hWnd, message, wParam, lParam);
 }
 return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
 UNREFERENCED_PARAMETER(lParam);
 switch (message)
 {
 case WM_INITDIALOG:
 return (INT_PTR)TRUE;

 case WM_COMMAND:
 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
 {
 EndDialog(hDlg, LOWORD(wParam));
 return (INT_PTR)TRUE;
 }
 break;
 }
 return (INT_PTR)FALSE;
}
</pre>
<p>В итоге имеем 188 строк, многовато, не правда ли? При создании оконного приложения многое можно принять за лишнее. А теперь <em>мой</em> стандартный код:</p>
<pre class="brush: cpp; title: ; notranslate">

#include &quot;stdafx.h&quot;
#include &quot;example.h&quot;

HINSTANCE hInst;
MSG msg;
HWND hWnd;

LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPTSTR    lpCmdLine,
 int       nCmdShow)
{
 hInst=hInstance;
 DialogBox(hInst, MAKEINTRESOURCE(IDD_YOUR_DIALOG), NULL, (DLGPROC)WndProc);
 return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 int wmId, wmEvent;

 switch (message)
 {
 case WM_COMMAND:
 wmId    = LOWORD(wParam);
 wmEvent = HIWORD(wParam);

 switch (wmId)
 {
 case IDM_ABOUT:
 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
 break;
 case IDM_EXIT:
 DestroyWindow(hWnd);
 break;
 default:
 return DefWindowProc(hWnd, message, wParam, lParam);
 }
 break;
 case WM_DESTROY:
 PostQuitMessage(0);
 break;
 default:
 return DefWindowProc(hWnd, message, wParam, lParam);
 }
 return 0;
}
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
 UNREFERENCED_PARAMETER(lParam);
 switch (message)
 {
 case WM_INITDIALOG:
 return (INT_PTR)TRUE;

 case WM_COMMAND:
 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
 {
 EndDialog(hDlg, LOWORD(wParam));
 return (INT_PTR)TRUE;
 }
 break;
 }
 return (INT_PTR)FALSE;
}
</pre>
<p>Вуа-ля &#8212; 68 строк! В строке 17 вместо <strong>IDD_YOUR_DIALOG</strong> укажите ID вашего DialogBox&#8217;a, например, <strong>IDD_ABOUTBOX</strong>. Теперь, если скомпилировать программу (F7, Ctrl+F5), то на экране появится диалоговое окно <strong>IDD_ABOUTBOX</strong>.</p>
<h2>Постовой</h2>
<hr noshade="noshade" />
<p>Ищите идеи для сайта? Не знаете какой тематике посвятить свой сайт? Как нарастить тИЦ и монетизировать блог? <a href="http://pinzes.ru" target="_blank">Pinzes.ru</a> &#8212; ответы на все ваши вопросы!</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://stafox.ru/visual-c-win32-sokrashhaem-kod-nachalnoj-programmy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
