Who is this article for?
This article is addressed to the same as I am new to C ++ programming who by chance or by choice decided to learn WinAPI.
I want to immediately warn:
I do not claim to be a guru in C ++ or WinAPI.
I'm just learning and I want to give here some examples and tips that make it easier for me to learn the functions and mechanisms of WinAPI.
In this article, I assume that you have already become familiar with C ++ enough to be able to create classes and overload various operators for them, and that you have already “hidden” some of your mechanisms in the class.
')
Creating and using the console
To debug a Win32 application or just to see how it is there everything happens inside, I always use the console.
Since you are creating a GUI application, not a console, the console does not connect. In order to call it in the depths of the Internet, this code was found
if (AllocConsole())
{
int hCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), 4);
*stdout = *(::_fdopen(hCrt, "w"));
::setvbuf(stdout, NULL, _IONBF, 0);
*stderr = *(::_fdopen(hCrt, "w"));
::setvbuf(stderr, NULL, _IONBF, 0);
std::ios::sync_with_stdio();
}
. :
void CreateConsole()
{
if (AllocConsole())
{
int hCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), 4);
*stdout = *(::_fdopen(hCrt, "w"));
::setvbuf(stdout, NULL, _IONBF, 0);
*stderr = *(::_fdopen(hCrt, "w"));
::setvbuf(stderr, NULL, _IONBF, 0);
std::ios::sync_with_stdio();
}
The called console works only in the output mode and it works just like in console applications. Display information as usual - cout / wcout.
For this code to work, you must include the following files in the project:
#include <fcntl.h>
#include #include <io.h>
and include the std namespace in the global namespace:
using namespace std;
Of course, if you do not want to do this, then simply add std :: to all entities that are in it.
Inheritance of objects for output and arithm. operations
When creating and studying the windows themselves, I always needed to display some value in the console.
For example:
You get the size of the client area of the window using the GetClientRect function where the address of the object of the RECT structure is passed as a parameter to fill this object with data. If you need to know the size of the received client area, you can simply bring it to an already connected console.
cout<<rect.right<<endl<<rect.bottom<<endl;
But doing it every time (especially if you often have to do something like this) is very inconvenient.
Here inheritance comes to the rescue.
Create a class that is openly inherited from the RECT structure and reload the output statement << as you like.
For example:
class newrect:public RECT
{
public:
friend ostream& operator<<(ostream &strm,newrect &rect)
{
strm<<"Prtint RECT object:\n";
strm<<rect.right<<endl<<rect.bottom<<endl;
return strm;
}
};
Now just output the object using cout / wcout:
cout<<nrect;
And you will be displayed in a convenient way as you need.
You can also do it with any operators you need.
For example, if you need to compare or assign structures (say the same RECT or POINT) - overload operator == () and operator = (), respectively.
If you want to implement an operator less than <to quickly compare window sizes, etc. reload operator <().
So you can do, I suppose, with almost any structures and most importantly, all functions that work with a regular object of a RECT structure will work just as well with his successor.
And I also recommend putting all this beauty into a separate include file and using it if necessary.
Own class
I do not know about the others, but since I’m completely green, I decided to create a new project for each function studied or for each chapter / chapter of the book, so that everything would be on the shelves and I could return at any time and refresh the necessary moments .
Since in WinAPI, even to create a simple window, you need to fill the class structure, register it and write a trivial window procedure, after the third or fourth project I remembered that I am still writing in C ++.
In the end, I hid everything in a simple class. The window handle, its name, class name, window procedure address, window class (WNDCLASS) are all hidden in the private section of the class.
To obtain them, it suffices to describe simple methods-get'ery, for example:
HWND GetHWND ()
LPCTSTR GetClsName (), etc.
Filling and registering the window class, creating the window itself and displaying it is done in the constructor.
For convenience, you can overload the constructor, and hide the filling and registration of the window class in a separate private class function and call each constructor. The convenience of overloading is that I sometimes need to create a very simple window and I invoke a constructor with two parameters - the name of the window and the hinstance of the application.
Another time I need to create a window with special dimensions, not with the default window procedure and with some other specific style - I call the designer with the accompanying parameters.
This class is defined in a separately included file, which lies in the include folder of the IDE.
Template for this class:
class BaseWindow
{
WNDCLASSEX _wcex;
TCHAR _className[30];
TCHAR _windowName[40];
HWND _hwnd;
bool _WindowCreation();
public:
BaseWindow(LPCTSTR windowName,HINSTANCE hInstance,DWORD style,UINT x,UINT y,UINT height,UINT width);
BaseWIndow(LPCTSTR windowName,HINSTANCE hInstance);
const HWND GetHWND()const{return HWND;}
LPCTSTR GetWndName()const{return _windowName;}
};
Having once thought through and having written such a class, you will make your life easier and you will spend more time learning and honing your skills than writing the same thing every time. Moreover, I consider it very useful - to make such a class yourself and supplement it as necessary.
PS
Everything described above is valid for:
Platform - Windows 7 32 bit
IDE - Visual Studio 2010
Maybe someone these tips will cause laughter and irony, but still we all once were in some ways newcomers / trainees / junior'ami.
I ask you to treat the post with understand. Constructive criticism, of course, is welcome.