VC ++ Study Notes-September

Source: Internet
Author: User
Tags drawtext textout

Chapter 3 Application Framework

Handle
Hwnd window handle
Hbitmap bitmap handle
Hicon icon handle
Hmenu handle
Hfile file handle
Hinstance current instance handle
Hglobal global memory object handle
HDC device environment handle
Hcursor cursor handle
Hfont font handle
Hpen paint handle
Hbrush paint brush handle
Hlocal local memory object handle

Side note:
Char is the first address of char [].
Similarly, main is the first address of main ().

Message
Message ID: indicates the pre-defined message id.
Wparam: additional message information
Lparam: additional message information of the long byte Parameter
Message loop:
Common:
While (getmessage (& MSG, null, 0 ,))
{
Translatmessage (& MSG); // converts the virtual key of a message to a character. For example

Wm_keyup/wm_keydown is converted to the wm_char message, which does not eliminate the old message but generates

New Messages

Dispatchmessage (& MSG); // transmits a message to a specified window, that is, the message is transmitted to

Function Name pointed to by lpfnwndproc
}

Window Function winproc ()
Defines the program's response to different received messages, including the location where various messages may be received

Process.
Each case processes a message

Messages that you are not interested in cannot be ignored. In general message callback functions, default will return

Defwindowproc (...) is handled by the system; otherwise, the window is not displayed.
The callback function callback is used to distinguish between _ stdcall (Language Conventions for Delphi and other APIs) and

_ Cdle (C Language Conventions) for different functions.

Initialization
1. Define window class
2 Registration window registerclass ();
3. Create window createwindow ();
4. showwindow () is displayed in the window ();

MFC class library

Including cwinapp cdoucument cview cframewnd cdoctemplate

Command-related class ccmdtarget is the base class of the message ing attribute. Message ing specifies that when an object is connected

Which function is called to process a message command.

Thread-based cwinthread can be used directly

Application cwinapp

Window Type cwnd

Framework window class cframewnd

Document window class cdoctemplate

In winmain, The lcmdline parameter indicates the system parameter.

Redraw cs_vredraw in the window .. 0x00001 for a single bit.
If you want to remove it, you can use Style &~ Cs_vredraw

Window wndclass

Side note: HDC
HDC hdc1 creates an HDC variable.

It can be displayed to HDC)
Hdc1.getdc (specify the handle of the window to be displayed, such as hwnd );
You can use textout (hdc1,...) to display text.
Release releasedc (hwnd, hdc1) after use)

Class:

Constructor
Its name is the name of the class, which is generally in public and called when the class is created.

A function that automatically assigns an initial value to a parameter. It is required for class creation.

Example:
Class Point
{
Public:
Int X;
Int y;
/* Void Init () // This is used to manually assign the Initial Value

Method, such as point S1; s1.init (); // Initialization
{
X = 0;
Y = 0;
}*/
Point () // This is the constructor
{
X = 0;
Y = 0;
}
---------------
Destructor:

Its name is ~ Class Name, which will be called when the class is used up. It is used to release the null occupied by the class.

Usually appears in the public

Example:

Class Point
{
Public:
Int X;
Int y;

 
~ Point () // This is the destructor
{
}

---------
Function Overloading

Multiple Functions with the same name and number of parameters or different types

For example:
Point ()
{
X = 0;
Y = 0;
}
Point (int A, int B)
{
X =;
Y = B;
}
That will be used when the parameter is met .. Adaptive.

----------------------
This pointer is used to point to an object,

For example:
Class Point
{
...
Void output (int x, int y)
{
This-> X = X;
This-> Y = y;
}
}
...
Point ist;

In this case, this in the class refers to ist.
----------------
Inheritance and derivation of functions

For example, Class;
When defining B, you can use
Class B: public
{

}
B inherits Class A, and B is derived from Class.
==== From csdn
The difference between inheritance and derivation is that they have different primary-object relationships.
Inheritance is passive for the parent class and can be described as: parent class XXX quilt class XXX inheritance

The derivation is active for the parent class. Description: XXX, a subclass of the parent class XXX.

Differences between a derived class and a subclass: it should be the conceptual difference between the hierarchy of the inheritance relationship.
A derived class can be an extra-level inheritance relationship,
Subclass is a level-1 link.
At the same time, subclass belongs to a derived class.

Example: Base Class,
Class B directly inherits from Class
Class C directly inherits from B;

The rules mentioned above are as follows:

Class A refers to Class B, Class A is inherited by Class B, Class B is derived from Class C, and Class B is inherited by Class C.
Class B is a subclass of Class A, and class C is a subclass of Class B;
Both class B and class C are derived classes of Class.
------------------------
Polymorphism

That is, if a function with the same name of the parent class is set to a virtual function, the type of the parent virtual function will be discarded, mainly in the subclass. Child

Class.
----------------------
Virtual Functions

See the polymorphism of the above functions,
Example:
Class animal
{
Public:
....
..
Virtual viod breathe () // This is a virtual function,
{
}

}

If it is virtual viod breathe () = 0, the pure virtual function is used by the parent class for "white ".

That is, the subclass must define its own function.
--------------
Reference

Is an alias.
Example:
Int A = 6;
Int & B =;
Then B is the reference of A, and B transformation, a will also change,
Note that the reference can only be defined once during definition and will not be repeatedly defined later.
Usage
If B = 5, a will also become 5

General Usage definition function
For example
Change (Int & A, Int & B0)
{...}
When used, change (x, y) even if the parameters X and Y are exchanged...

======================================
Class usage
Divides files into CPP and H files.
The H file only declares class functions without specific definitions.
For example, Animal. h only needs to be written.
Class animal
{
Public:
Animal (INT heght, int weght );
Viod eat ();
Viod sleep ();
Virtual viod breathe ();
}
Write in animal. cpp

Animal: Animal () // note that you do not need to write the function return value here, because the constructor does not return the value.
{
// Constructor
}
Void animal: Eat ()
{
}
.....

Inherits the fish build function defined by the fish class of the animail class.
Fish: fish (): ananimal (300,400)
{
}
--------- Note that header files are not compiled
--------------
: Shownwindow () indicates to call global or platform functions.
The afx class is a framework-class function. The system associates many classes with global functions.
Afxgetapp () can obtain a cwinapp class pointer that is, this
Cwinapp is an application class

Build cwinapp first build global variables
Then construct winmain
Afxwinmain is a function called by winmain to complete the function of the winmain function.
Afxregisterclass registers window class functions to use Win32-style Windows.
Initapplication () is used to initialize management functions.
Initinstance () calls the initialization function of the subclass.

This indicates that xxxapp in the cxxxapp xxxapp definition is an application class.

The precreatwindows () function first registers the window afxdeferregisterclass.

() // Afxenddeferregisterclass determines whether a parameter is registered. If not, register the parameter.
You can use the createstruct & CS parameter to change the window type, such as whether the window is maximized or not.
The creation of the main window is implemented in processshellcomand (cmdinfo ).
: Createmediwex (...) Creation window. The returned value is a handle.
M_pmainwnd is the pointer to cmainframe
Cwinthread: Run () completes the message loop

Note that wndcls. lpfnwndproc = defwindowproc does not simply send messages to the message processing function.

Defxxxx is used for message ing. Therefore, this processing function does not have a corresponding function. -- Leon Annotation

The sub-window of a single document program is a cxxxxview class.
Long String

Cxxxdoc document class
In C ++, struct is also a special class.

Differences and links between the methods for creating windows between MFC and Win32

Wndclass wndcls;
Wndcls. cbclsextra = 0;
Wndcls. cbwndextra = 0;
......
Registerclass (& wndcls );

Cwnd WND; // WND is not a window. When the window is destroyed, the destroyed handle is empty, but the Member

The function can still be used, but if WND is destroyed, the window is destroyed.
WND. createex (...); // there is no leakage in the internal transfer of hwnd
WND. showwindow (sw_shownormal );
WND. updatewindow ();
| Win32 ----------------------------------------------------------------

-- MFC
V hwnd;
Hwnd = createdomainwex ();
: Showwindow (hwnd, sw_shownormal );
: Updatewindow (hwnd );

......

Cbutton: Create (text, style, size and position of the lpststr button, parent window, ID)
Example: cbutton BTN; // Add it to private in class
Call
BTN. Create ("test", ws_child | bs_pushbutton, crect (0, 0, 1, 1), this,

Id123 );
BTN. showwindow...
Small lock private, key protect, nothing is public
Obtain the parent window pointer hwnd getparent ();

----------------------------------------------------------------------

--------------------
----------------------------------------------------------------------

--------------------
Text editing
Create an insert Operator
Createsolidcaret (20 width, 100 height );
Showcaret ();
// Ascent ascending height, that is, the H-to-head height
// Descent descending height, that is, the height from the beginning to the end of G
Font height
Ccilentdc DC (this );
Textmetric TM; // note that the TM here is randomly named.
DC. gettextmetrics (& TM );
TM. tmhight is his height
This is useful!

Bitmap insert Operator

Cbitmap bitmap; // if necessary, it is best to put it in the class private to avoid being killed during structure analysis.
Bitmap. loadbitmap (idc_xxx); // The cstring Method for reading resources is similar.

Loadstring // you can add it to the string table of the resource.
Createcaret (& Bitmap );
Showcaret ();

View class, the ondraw window repainting experience in

* PDC defined by the CDC class
Cstring STR ("Hello world! ");
Text output:
PDC-> textout (50, 50, STR); // 50, 50 is the coordinate of the upper left corner of the text

The member x y in csize is the length and height respectively.

Returns the string height.
Csize SZ = PDC-> gettextextent (STR );

Draw a rectangle PDC-> rectangle (x0, y0, X1, Y1 );
Cstring. getlength () to get the length
//////////////////////////////
In the cxxxview class, right-click the added message and choose add windowsmessage hander.

Intercepts the response of the wm_char message
Void ctextview: onchar (uint nchar, uint nrepcnt, uint nflags)
{
Cclientdc DC (this );
Cfont font;
Font. createpointfont (300, "文 ", null );
Cfont * poldfont = Dc. SelectObject (& font );
Textmetric TM;
DC. gettextmetrics (& TM );
If (0x0d = nchar) // press ENTER
{
M_strline.empty ();
M_ptorigin.y + = TM. tmheight;
}
Else if (0x08 = nchar) // return
{
Colorref CLR = Dc. settextcolor (DC. getbkcolor ());
DC. textout (m_ptorigin.x, m_ptorigin.y, m_strline );
M_strline = m_strline.left (m_strline.getlength ()-1 );
DC. settextcolor (CLR );
}
Else
{
M_strline + = nchar;
}
Csize SZ = Dc. gettextextent (m_strline );

Cpoint pt;
PT. x = m_ptorigin.x + Sz. CX;
PT. Y = m_ptorigin.y;

Setcaretpos (PT );
DC. textout (m_ptorigin.x, m_ptorigin.y, m_strline );

DC. SelectObject (poldfont );

Cview: onchar (nchar, nrepcnt, nflags );
}

Ctextview: ontimer (uint nidevent) // The timer component code that follows the color-changing text
{
// Todo: add your message handler code here and/or call default
M_nwidth + = 5;

Cclientdc DC (this );
Textmetric TM;
DC. gettextmetrics (& TM );
Crect rect;
Rect. Left = 0;
Rect. Top = 200;
Rect. Right = m_nwidth;
Rect. Bottom = rect. Top + TM. tmheight;

DC. settextcolor (RGB (255, 0, 0 ));
Cstring STR;
Str. loadstring (ids_weixin );
DC. drawtext (STR, rect, dt_left );

Rect. Top = 150;
Rect. Bottom = rect. Top + TM. tmheight;
DC. drawtext (STR, rect, dt_right );

Csize SZ = Dc. gettextextent (STR );
If (m_nwidth> Sz. CX)
{
M_nwidth = 0;
DC. settextcolor (RGB (0,255, 0 ));
DC. textout (0,200, STR );
}
//////////////////////////////////////// ///////////////////////////////

/
Cview: ontimer (nidevent );
}

///////////////////////////////////////
========================================================== ====================================

====
Three points about the message:

1. Import and release macros (// {afx_virtual (XXX) in the header file of the class )... //} Afx_virtual)
2. Source File
Begin_message_map (cxxxview, cview)
// {Afx_msg_map (cxxxview)
This location
//} Afx_msg_map
3. viod cxxxx: onxxx () custom processing function section of the source file

Message ing of MFC (non-Win32 message loop processing)

In Win32 programming, message processing makes all messages into virtual functions. As long as the sub-classes are set

Line
This will cause a waste of memory resources.

There is a ing table between the handle and the C ++ class object pointer in the background. When a message occurs, the background will

Find the pointer to the C ++ object through the handle of the window, and then pass the pointer to the base class

The base class calls windowproc through a message loop, while windowproc itself is a virtual function.

The onwinmsg (message routing part) must be called when the class is defined. onwinmsg searches for 1 and 2.

To determine whether there is a life of the message response, and finally find 3 for processing. -- Mr. Sun Xin's original article

I understand that the message ing process of MFC is as follows:

Message generation-enable the ing table in the background-find the handle in the response window-windowproc-

Onwinmsg-to see if there are definitions in part 1 and 2. If there is any, just-> 3;, and no.

Processed by the base class ):)

In view, you do not need to use a function to obtain the handle of the current window. Simply use m_hwnd to obtain the handle of the current window.

Draw line functions and usage:

Win32 usage

HDC;
HDC =: getdc (m_hwnd); // the function of the Platform SDK must be added ::
Movetoex (HDC, m_old.x, m_old.y, null); // first move the mouse to the original point
Lineto (HDC, point. X, point. Y); // draw a function
: Releasedc (m_hwnd, HDC); // release the DC after use

MFC usage

CDC * PDC = getdc ();
PDC-> moveTo (m_old );
PDC-> lineto (point );
Releasedc (PDC );
Same as above

Faster MFC (automatically getdc and releasedc)

// Cclientdc DC (getparent (); // if you use this method, you can draw it on the toolbar, that is, the target is the whole

Framework, but does not include non-customer areas, that is, the title bar and menu.
Cclientdc DC (this); // note that this function requires a cwnd pointer instead of hwnd and hwnd.

Only one cwnd variable can be called with cwnd-> hwnd
DC. moveTo (m_old );
DC. lineto (ponit );

Method 4: cwindowdc (getdc and releasedc are also automatically executed)

Cwindowdc DC (this); // If getparent is used, the entire window can be painted.
DC. moveTo (m_old );
DC. lineto (ponit );

The desktop itself is a window!
Get the handle of the desktop window: getdesktopwindow () can get a cwnd;

NOTE: If cwnd: is used, it is used to obtain the pointer of the cwnd class.
If you use the SDK: The class is used to get the handle of this window!

Cpen paint class
Cpen pen (pen, width, RGB color );
You need to select the device description table !!
SelectObject should be used to select ppen, pbrush, pfont, pbitmap (need to add & such

& Pen, the return value is also a pointer, which must be connected with * pen)
The returned value is a device in the previous State. For example, if a new paint brush is defined, the returned value is the old paint brush.

 

Cbrush

Color Paint Brush
Cbrush brush (RGB (255, 0, 0 ));
Cclientdc DC (this );
DC. fillrect (crect (m_old, point), & brush); // fill
// Draw a red rectangle

Bitmap painter

Cbitmap bitmap;
Bitmap. loadbitmap (idb_xxxx );
Cbrush brush (& Bitmap );
Cclientdc DC (this );
DC. fillrect (crect (m_old, point), & brush); // fill

The fastest way to draw a rectangle:
DC. rectangle (crect (m_old, point ));

Transparent painting method:

Cbrush: * pb = cbrush: fromhandle (hbursh) getstockobject (null_brush ));
DC. SelectObject (PB );
DC. Rectangle ....
---------------------
Class P
{
Public:
Void ()
{}
Static void B ()
'// Static use of member functions can be written anywhere
For example, the static environment cannot call non-static (without static definition) member variables,

Non-static parameters can be referenced

Static Initialization is required !! That is to say, we should do it well before definition!

P point;
P: B ();

{}
}
========================================================== ======
SS menu:

Four files for adding menu command response functions
 
Mainframe
Third Priority
App
Fourth priority
Doc
Second priority
View
Highest priority

If there is a function, other classes are no longer allowed.

-----------

Message category

Standard Message:
It is a class derived from cwnd and can receive the message in all its derived classes.
All messages starting with WM except wm_command

Command Message
Menu, shortcut key, toolbar button message
All are presented in wm_command. You need to distinguish the ID number of the required menu item.
Classes derived from cshorttarget (parent class of cwnd) can accept such messages.

Announcement message
There is a message generated by the Control. click the button to identify the ID...
It is also presented using wm_command.
Classes derived from cshorttarget (parent class of cwnd) can accept such messages.

The cwnd class can accept all messages, but the cve-target class can only accept the latter two messages.

Command message routing:
Afxwndproc-> afxcallwandproc-> windowproc-> onwndmsg->. 1 onnotify (Notice cleared

Messages)-> on1_msg
. 2 oncommand (command

Messages)

Tag menu-the one with check boxes
Implementation Method:
Add oncreate to corresponding function
Getmenu ()-> getsubmenu (0)-> checkmenuitem (ID _ key,

Mf_bycommand | mf_checked); // mf_unchecked
0 indicates the number of sub-menus
Getmenu is a member function of cwnd. It returns a pointer to the entire menu bar.
Getsubmenu is just a pointer to the returned sub-menu.
Checkmenuitem is to place or cancel a tag in the menu

Note that if you use the index method, you must be able to calculate the split column!

Set the default menu (font to bold)
Getmenu ()-> getsubmenu (0)-> setdefaultitem (ID );
A sub-menu can only have one default menu, that is, the last name.

Set Image menu
Setmenuitembitmaps
You can select a bitmap without a bitmap.
Cbitmap cc;
Cc. loadbitmap (image ID );
Setmenuitembitmaps (id_file_open, mf_bycommand, & m_white, & m_white );

Function for dimmed buttons: getmenu ()-> getsubmenu (0)-> enablemenuitem (ID,

Mf_bycommand/mf_disable | mf_grayed); you need to add

M_bautomenuenable = 0;

Getsystemmetrics (); // obtain system parameters. For details, refer to msdn to find a lot of useful data.
Remove menu: setmenu (null );
Reply menu:
Cmenu menu;
Menu. loadmenu (menu ID );
Setmenu (& menu );
1.10
Cmenu class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.