Use VC ++ to create a deformation form

Source: Internet
Author: User
Use VC ++ to create a deformation form

First, we need to create a project distort Based on the dialog box to generate three classes, namely caboutdlg, cdistort, and cdistortdlg. We will make major changes to the cdistortdlg class, therefore, we provide the original code of this file. If there is any change, we can use a pink table and add comments.

// Distortdlg. h: header file
//

# If! Defined (afx_distortdlg_hsf-b21move 69_5a48_11d7_a464_00055de445c11_included _)
# Define afx_distortdlg_hsf-b21move 69_5a48_11d7_a464_00055de445c11_included _

# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000

// Cdistortdlg Dialog

Class cdistortdlg: Public cdialog
{
// Construction
Public:
Cdistortdlg (cwnd * pparent = NULL); // standard Constructor

// Dialog data
{Afx_data (cdistortdlg)
Enum {IDD = idd_distort_dialog };
// Note: The classwizard will add data members here
} Afx_data

// Classwizard generated virtual function overrides
// {Afx_virtual (cdistortdlg)
Protected:
Virtual void dodataexchange (cdataexchange * PDX); // DDX/DDV support
} Afx_virtual

// Implementation
Protected:
Hicon m_hicon;

// Generated message map Functions
// {Afx_msg (cdistortdlg)
Virtual bool oninitdialog ();
Afx_msg void onsyscommand (uint NID, lparam );
Afx_msg void onpaint ();
Afx_msg hcursor onquerydragicon ();
} Afx_msg

// Reload the wm_ncthitest message. The single-click dialog box can be moved anywhere.
Afx_msg uint onnchittest (cpoint point );
// Timer to make the Form Deformation
Afx_msg void ontimer (uint nideevent );
// Disable the timer in the Undo dialog box
Afx_msg void ondestroy ();
Declare_message_map ()

PRIVATE:

// The initial rectangular object of the form
Crect m_rectwnd;
// Set the region of the form
Crgn m_rgn;

};

{Afx_insert_location }}

# Endif //! Defined (afx_distortdlg_hsf-b21csc69_5a48_11d7_a464_00055de445c1
_ Supported ded _)

Next, set stype to popup in the properties of the dialog box, and border to none,

In this implementation file, we skipped the functions automatically generated by the system and only wrote the modified ones.

// Distortdlg. cpp: implementation file
//

# Include "stdafx. H"
# Include "distort. H"
# Include "distortdlg. H"

# If def _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif

//////////////////////////////////////// ///////////////////////
// Caboutdlg Dialog used for app about

Class caboutdlg: Public cdialog
{
// Skip automatically generated class members
......
// Message macro projection
Begin_message_map (cdistortdlg, cdialog)
{Afx_msg_map (cdistortdlg)
On_wm_syscommand ()
On_wm_paint ()
On_wm_querydragicon ()

On_wm_nchittest ()
On_wm_timer ()
} Afx_msg_map
End_message_map ()
// Below we will add the virtual function oninitdiolog () that will be reloaded by the dialog box program. It is a message response function that processes the // wm_initdialog message.

# Define event_redraw 0

Bool cdistortdlg: oninitdialog ()
{
Cdialog: oninitdialog ();

// Add "about..." menu item to system menu.

// Idm_aboutbox must be in the system command range.
Assert (idm_aboutbox & 0xfff0) = idm_aboutbox );
Assert (idm_aboutbox <0xf000 );

Cmenu * psysmenu = getsystemmenu (false );
If (psysmenu! = NULL)
{
Cstring straboutmenu;
Straboutmenu. loadstring (ids_aboutbox );
If (! Straboutmenu. isempty ())
{
Psysmenu-> appendmenu (mf_separator );
Psysmenu-> appendmenu (mf_string, idm_aboutbox, straboutmenu );
}
}

// Set the icon for this dialog. The framework does this automatically
// When the application's main window is not a dialog
Seticon (m_hicon, true); // set big icon
Seticon (m_hicon, false); // set small icon

// Todo: add extra initialization here
// Obtain the size of the customer area in the dialog box
Getclientrect (m_rectwnd );
// Create a form dialog box. The size of the customer area
M_rgn.createellipticrgn (0, 0, m_rectwnd.right, m_rectwnd.bottom );
// Set the redraw window timer, which redraws the setjavaswrgn (hrgn) m_rgn, true) Every 0.1 seconds );
This-> settimer (event_redraw, 100, null );

// ------------------------- Set time ----------------------------
Return true;
// Return true unless you set the focus to a control

The elliptic effect is indicated by INT setjavaswrgn (hrgn, bool bredraw );
The hrgn parameter is the handle of the form area, and the bredraw parameter determines whether to redraw the form. In this example, Windows sends the wm_paint message to the form and calls the onpaint () function,
Next let's modify this function.

Void cdistortdlg: onpaint ()
{
Cpaintdc DC (this );
If (isiconic ())
{
// Device context for painting

Sendmessage (wm_iconerasebkgnd, (wparam) DC. getsafehdc (), 0 );

// Center icon in client rectangle
Int cxicon = getsystemmetrics (sm_cxicon );
Int cyicon = getsystemmetrics (sm_cyicon );
Crect rect;
Getclientrect (& rect );
Int x = (rect. Width ()-cxicon + 1)/2;
Int y = (rect. Height ()-cyicon + 1)/2;

// Draw the icon
DC. drawicon (X, Y, m_hicon );
}
Else
{
// Add By Myself ---------------------------------------------------
DC. selectstockobject (null_pen); // draw an elliptic without edges

// Obtain the size of the customer Zone
Crect rect;
Getclientrect (rect );

Cbrush * pbrushold;
Cbrush brushnew;

// Define the paint brush, where you can change the color
Brushnew. createsolidbrush (RGB (204,255, 66 ));
// Select a paint brush as a drawing device
Pbrushold = Dc. SelectObject (& brushnew );
// Draw an ellipse
DC. ellipse (rect );
// Restore and release the paint brush
DC. SelectObject (pbrushold );
Brushnew. deleteobject ();
}
}

// Write the following function to support any form.

Hcursor cdistortdlg: onquerydragicon ()
{
Return (hcursor) m_hicon;
}

Uint cdistortdlg: onnchittest (cpoint point)
{
Uint nhittest = cdialog: onnchittest (point );
Return (nhittest = htclient )? Htcaption: nhittest;
}

// Write the real number of the timer below

// If you want the result to be round after N changes, set this value to 1.035
# Define multi 1.1

Void cdistortdlg: ontimer (uint nidevent)
{
Static ncount = 0; // number of changes to the form, increasing by 1 each time
Static border = 1; // It indicates to increase or decrease
Static double dmulti = 1; // form change multiple

Crect rectwnd;
Cpoint ptmiddle, pttopleft, ptbtmright;

// Confirm the form region Center
Ptmiddle = m_rectwnd.centerpoint ();

// Handle various scheduled events in different situations
Switch (nidevent)
{
Case event_redraw:
Ncount ++;
If (border = 1)
{// Larger
Dmulti * = multi;
}
Else
{// Indicates smaller
Dmulti/= multi;
}

// Determine the rectangle size of the form
Pttopleft. x = (long) (ptmiddle. x-m_rectWnd.Width ()/(2 * dmulti ));
Pttopleft. Y = (long) (ptmiddle. y-m_rectWnd.Height ()/2 );
Ptbtmright. x = (long) (ptmiddle. x + m_rectwnd.width ()/(2 * dmulti ));
Ptbtmright. Y = (long) (ptmiddle. Y + m_rectwnd.height ()/2 );
Rectwnd. setrect (pttopleft, ptbtmright );

// Release the object
M_rgn.detach ();

// Create an elliptical Region
M_rgn.createellipticrgn (pttopleft. X, pttopleft. Y, ptbtmright. X, ptbtmright. y );

Setjavaswrgn (hrgn) m_rgn, true );

If (ncount = 10) // sets the number of changes. This is 10 times.
{
Ncount = 0;
Border = (border = 1 )? -; // Reverse changes after 10 times
}
Break;
}
Cdialog: ontimer (nidevent );
}

Final Effect

We hope that you will first understand the dialog box mechanism of MFC through this example (for details, see vicual C ++ advanced programming).

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.