-Create and display form instances

Source: Internet
Author: User
Tags bool win32

This example uses API functions to create, destroy, display, and hide forms under the SDK.

1. Open VC6, click the File/new menu, select Win32 Application, and set up an application named Windowdemo for the project.

2, select "A Simple Win32 Application"

3, add a menu resource to the project, ID idr_main, add a submenu in the menu "Test Form 1", contains four menu items,

The IDs are: "Id_mi_wnd1_create", "Id_mi_wnd1_destroy", "Id_mi_wnd1_show", "Id_mi_wnd1_hide",

The text is create, destroy, show, hide.

Add the Second submenu "Test Form 2" for Idr_main, just change "1" to "2"

///

MyWnd.h #define My_wnd_prop_name _t ("_my_wnd_prop_name_") #define My_wnd_class_name _t ("_my_wnd_class_name_") CLA
	SS Cmywnd {Public:cmywnd ();
	Virtual ~cmywnd (); Create a form hwnd CreateEx (HInstance HInst, DWORD dwExStyle, LPCTSTR Lptext, DWORD dwstyle, LPRECT Lprcwnd, hwnd hparent, I
	NT Nmenuid);
	HWND CreateEX (DWORD dwstyle, int x,int y, int nwidth, int nheight, hwnd hparent);
	Destroys the form void Destroy ();
	Show or hide the form BOOL Show (int ncmd);
	Gets the form handle HWND gethwnd ();
The operator that can return the form handle directly operator HWND () const;
	Protected:///Pre-processing derived class call opportunity virtual BOOL PREPROCESSMSG (HWND hwnd, UINT nmsg, WPARAM WP, LPARAM LP, LRESULT *presult);
	Form Create message Virtual LRESULT OnCreate (HWND hwnd);
	form destroy message virtual BOOL OnDestroy ();
	Handling command button Messages virtual BOOL oncommand (int ncmdid);
	Draw form Virtual BOOL OnPaint ();
	Draw background Virtual BOOL ONERASEBK (hdc hdc);

Timer message virtual BOOL OnTimer (int nID);
	Protected: Whether or not the detection class is registered, if not registered, BOOL Checkclass (LPCTSTR lpclassname) is registered; form procedure Static LRESULT CALlback WndProc (HWND hwnd, UINT nmsg, WPARAM WP, LPARAM LP);
	Protected:hwnd m_hwnd;
	HMENU M_hmenu;

HINSTANCE m_hinstance;

};
 #endif//!defined (AFX_MYWND_H__6B084A95_8F56_430C_909F_6869193CD5AA__INCLUDED_)


 

MyWnd.cpp:implementation of the Cmywnd class. #include "stdafx.h" #include "MyWnd.h"// Construction/destruction///////////////// Cmywnd::cmywnd (): M_hwnd (null), M_hmenu (null), M_hinstance (
	NULL) {} cmywnd::~cmywnd () {if (m_hwnd) {Destroy (); }} hwnd Cmywnd::createex (HInstance HInst, DWORD dwExStyle, LPCTSTR Lptext, DWORD dwstyle, LPRECT Lprcwnd, HWND hpare
	NT, int nmenuid) {///If it has been created, returns, prevents duplicate creation if (m_hwnd) {return m_hwnd;

	}///hinst is passed into m_hinstance by WinMain = HInst;
	Calculate form size int x,y,nwidth,nheight;
	if (Lprcwnd = = NULL) {x = y =nwidth = nheight = Cw_usedefault;
		} else {x = lprcwnd->left;
		y = lprcwnd->top;
		nwidth = lprcwnd->right-lprcwnd->left;
	nheight = lprcwnd->bottom-lprcwnd->top; }//If there is a menu, load menu, and as Form menu if (Nmenuid > 0) M_hmenu = LoadMenu (M_hinstance, Makeintresource (Nmenuid));

	Detects if the class name has been registered Checkclass (my_wnd_class_name); Create form m_hwnd = CreateWindowEx (dwExStyle, My_wnd_class_name, Lptext, dwstyle, X, Y, nwidth, nheight, Hparent, M_hMen

	U, m_hinstance, this);
return m_hwnd;  } HWND Cmywnd::createex (DWORD dwstyle, int x,int y, int nwidth, int nheight, HWND hparent) {/////Create an overloaded function for form RECT rc = {x
	, y, x + nwidth, y + nheight};
Return CreateEx (NULL, 0, _t ("Mywnd"), Dwstyle, &RC, hparent, 0);
		} void Cmywnd::D Estroy () {///Destroy form if (m_hwnd) {DestroyWindow (m_hwnd);
	M_hwnd = NULL;

}} BOOL cmywnd::show (int ncmd) {///Show or hide the window return ShowWindow (M_hwnd, ncmd);}

HWND Cmywnd::gethwnd () {//Returns the form handle return m_hwnd;}

Cmywnd::operator HWND () const {return m_hwnd;} BOOL Cmywnd::p reprocessmsg (HWND hwnd, UINT nmsg, WPARAM WP, LPARAM LP, LRESULT *presult) {/////return TR before returning all message processing
The UE representative continues to be processed by the corresponding message response function, otherwise it is not processed or returned by the return value of the message returned by Presult to TRUE; } LRESULT Cmywnd::oncreaTE (hwnd hwnd) {//wm_create message response function return 0;}

BOOL Cmywnd::ondestroy () {return true;}

BOOL cmywnd::oncommand (int ncndid) {return true;}

BOOL Cmywnd::onpaint () {return true;}

BOOL CMYWND::ONERASEBK (hdc hdc) {return true;}

BOOL cmywnd::ontimer (int nID) {return true;}
	BOOL Cmywnd::checkclass (LPCTSTR lpclassname) {///Detect class name, register wndclass WC = {0} if not registered; if (! GetClassInfo (M_hinstance, Lpclassname, &WC)) the///getclassinfo () function is used to detect whether a class is registered {Wc.hbrbackground = (hbrush) COLOR_
		Appworkspace;
		Wc.lpfnwndproc = WndProc;
		Wc.lpszclassname = Lpclassname;
	Return registerclass (&WC);

} return true; } LRESULT CALLBACK Cmywnd::wndproc (HWND hwnd, UINT nmsg, WPARAM WP, LPARAM LP) {////Message Processing Conventions:///1, derived classes have processed a message handler function, no longer
	Calling base class processing, calling the base class means that the default processing///2, message processing, except special messages, returns True to call the default processing, otherwise does not call the default processing LRESULT LRESULT = 0;
	BOOL Bcalldefault = true; Remove and handle the corresponding class Cmywnd *pwnd = (cmywnd*) getprop (hWnd, my_wnd_prop_name);////getprop () function if (pWnd = = NULL) {if (nmsg!)
= wm_create)		{return DefWindowProc (HWnd, NMSG,WP, LP);
			} else {lpcreatestruct pSt = (lpcreatestruct) LP;
			PWnd = (cmywnd*) pst->lpcreateparams;
		SetProp (HWnd, My_wnd_prop_name, pWnd);

	}} if (!pwnd->preprocessmsg (hWnd, Nmsg, WP, LP, &lresult)) return lResult;
			Processing message switch (NMSG) {case wm_create: {pwnd->m_hwnd = hWnd;
			LResult = Pwnd->oncreate (hWnd);
			Bcalldefault = false;
		Break
		} Case Wm_destroy:bcalldefault = Pwnd->ondestroy ();

	Break
		Case Wm_command:bcalldefault = Pwnd->oncommand (LoWord (WP));
	Break
		Case wm_paint:bcalldefault = Pwnd->onpaint ();

	Break
		Case Wm_erasebkgnd:bcalldefault = PWND->ONERASEBK (HDC) WP);////???????????
	Break
		Case Wm_close:pwnd->destroy ();
		Bcalldefault = false;
	Break
		Case Wm_timer:bcalldefault = Pwnd->ontimer ((int) WP);
	Break
	Default:break;
	}///Call the default processing according to the result of the message processing, or return directly if (Bcalldefault) return DefWindowProc (HWnd, Nmsg, WP, LP); ELSE retUrn LResult; }


Add three Classes "Cmainwnd", "CChildWnd1", "CChildWnd2" to the project, all inherit from Cmywnd

Mainwnd.h:interface for the Cmainwnd class.
//////////////////////////////////////////////////////////////////////

#if!defined (AFX_MAINWND_H__ 2910b9f6_dcdc_4e34_b367_dddddeb4f1ad__included_)
#define Afx_mainwnd_h__2910b9f6_dcdc_4e34_b367_ Dddddeb4f1ad__included_

#if _msc_ver >
#pragma once
#endif//_msc_ver >

#include " MyWnd.h "
#include" ChildWnd1.h "
#include" ChildWnd2.h "

class Cmainwnd:public Cmywnd  
{
Public:
	Cmainwnd ();
	Virtual ~cmainwnd ();
Protected:
	BOOL oncommand (int ncmdid);
	BOOL OnDestroy ();
Private:
	CChildWnd1 m_wndchild1;///Test Form 1
	CChildWnd2 m_wndchild2;///Test form 2

};

#endif//!defined (AFX_MAINWND_H__2910B9F6_DCDC_4E34_B367_DDDDDEB4F1AD__INCLUDED_)
MyWnd.cpp:implementation of the Cmywnd class. #include "stdafx.h" #include "MyWnd.h"// Construction/destruction///////////////// Cmywnd::cmywnd (): M_hwnd (null), M_hmenu (null), M_hinstance (
	NULL) {} cmywnd::~cmywnd () {if (m_hwnd) {Destroy (); }} hwnd Cmywnd::createex (HInstance HInst, DWORD dwExStyle, LPCTSTR Lptext, DWORD dwstyle, LPRECT Lprcwnd, HWND hpare
	NT, int nmenuid) {///If it has been created, returns, prevents duplicate creation if (m_hwnd) {return m_hwnd;

	}///hinst is passed into m_hinstance by WinMain = HInst;
	Calculate form size int x,y,nwidth,nheight;
	if (Lprcwnd = = NULL) {x = y =nwidth = nheight = Cw_usedefault;
		} else {x = lprcwnd->left;
		y = lprcwnd->top;
		nwidth = lprcwnd->right-lprcwnd->left;
	nheight = lprcwnd->bottom-lprcwnd->top; }//If there is a menu, load menu, and as Form menu if (Nmenuid > 0) M_hmenu = LoadMenu (M_hinstance, Makeintresource (Nmenuid));

	Detects if the class name has been registered Checkclass (my_wnd_class_name); Create form m_hwnd = CreateWindowEx (dwExStyle, My_wnd_class_name, Lptext, dwstyle, X, Y, nwidth, nheight, Hparent, M_hMen

	U, m_hinstance, this);
return m_hwnd;  } HWND Cmywnd::createex (DWORD dwstyle, int x,int y, int nwidth, int nheight, HWND hparent) {/////Create an overloaded function for form RECT rc = {x
	, y, x + nwidth, y + nheight};
Return CreateEx (NULL, 0, _t ("Mywnd"), Dwstyle, &RC, hparent, 0);
		} void Cmywnd::D Estroy () {///Destroy form if (m_hwnd) {DestroyWindow (m_hwnd);
	M_hwnd = NULL;

}} BOOL cmywnd::show (int ncmd) {///Show or hide the window return ShowWindow (M_hwnd, ncmd);}

HWND Cmywnd::gethwnd () {//Returns the form handle return m_hwnd;}

Cmywnd::operator HWND () const {return m_hwnd;} BOOL Cmywnd::p reprocessmsg (HWND hwnd, UINT nmsg, WPARAM WP, LPARAM LP, LRESULT *presult) {/////return TR before returning all message processing
The UE representative continues to be processed by the corresponding message response function, otherwise it is not processed or returned by the return value of the message returned by Presult to TRUE; } LRESULT Cmywnd::oncreaTE (hwnd hwnd) {//wm_create message response function return 0;}

BOOL Cmywnd::ondestroy () {return true;}

BOOL cmywnd::oncommand (int ncndid) {return true;}

BOOL Cmywnd::onpaint () {return true;}

BOOL CMYWND::ONERASEBK (hdc hdc) {return true;}

BOOL cmywnd::ontimer (int nID) {return true;}
	BOOL Cmywnd::checkclass (LPCTSTR lpclassname) {///Detect class name, register wndclass WC = {0} if not registered; if (! GetClassInfo (M_hinstance, Lpclassname, &WC)) the///getclassinfo () function is used to detect whether a class is registered {Wc.hbrbackground = (hbrush) COLOR_
		Appworkspace;
		Wc.lpfnwndproc = WndProc;
		Wc.lpszclassname = Lpclassname;
	Return registerclass (&WC);

} return true; } LRESULT CALLBACK Cmywnd::wndproc (HWND hwnd, UINT nmsg, WPARAM WP, LPARAM LP) {////Message Processing Conventions:///1, derived classes have processed a message handler function, no longer
	Calling base class processing, calling the base class means that the default processing///2, message processing, except special messages, returns True to call the default processing, otherwise does not call the default processing LRESULT LRESULT = 0;
	BOOL Bcalldefault = true; Remove and handle the corresponding class Cmywnd *pwnd = (cmywnd*) getprop (hWnd, my_wnd_prop_name);////getprop () function if (pWnd = = NULL) {if (nmsg!)
= wm_create)		{return DefWindowProc (HWnd, NMSG,WP, LP);
			} else {lpcreatestruct pSt = (lpcreatestruct) LP;
			PWnd = (cmywnd*) pst->lpcreateparams;
		SetProp (HWnd, My_wnd_prop_name, pWnd);

	}} if (!pwnd->preprocessmsg (hWnd, Nmsg, WP, LP, &lresult)) return lResult;
			Processing message switch (NMSG) {case wm_create: {pwnd->m_hwnd = hWnd;
			LResult = Pwnd->oncreate (hWnd);
			Bcalldefault = false;
		Break
		} Case Wm_destroy:bcalldefault = Pwnd->ondestroy ();

	Break
		Case Wm_command:bcalldefault = Pwnd->oncommand (LoWord (WP));
	Break
		Case wm_paint:bcalldefault = Pwnd->onpaint ();

	Break
		Case Wm_erasebkgnd:bcalldefault = PWND->ONERASEBK (HDC) WP);////???????????
	Break
		Case Wm_close:pwnd->destroy ();
		Bcalldefault = false;
	Break
		Case Wm_timer:bcalldefault = Pwnd->ontimer ((int) WP);
	Break
	Default:break;
	}///Call the default processing according to the result of the message processing, or return directly if (Bcalldefault) return DefWindowProc (HWnd, Nmsg, WP, LP); ELSE retUrn LResult; }



Childwnd1.h:interface for the CChildWnd1 class.
//////////////////////////////////////////////////////////////////////

#if!defined (AFX_CHILDWND1_H_ _179cb7ba_1d79_40c7_a603_9ad343a003d5__included_)
#define Afx_childwnd1_h__179cb7ba_1d79_40c7_a603_ 9ad343a003d5__included_

#if _msc_ver >
#pragma once
#endif//_msc_ver >

#include " MyWnd.h "

class Cchildwnd1:public Cmywnd  
{public
:
	CChildWnd1 ();
	Virtual ~cchildwnd1 ();
	
	BOOL ONERASEBK (hdc hdc);
};

#endif//!defined (AFX_CHILDWND1_H__179CB7BA_1D79_40C7_A603_9AD343A003D5__INCLUDED_)


ChildWnd1.cpp:implementation of the CChildWnd1 class.
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
# Include "ChildWnd1.h"

//////////////////////////////////////////////////////////////////////
// Construction/destruction
//////////////////////////////////////////////////////////////////////

Cchildwnd1::cchildwnd1 ()
{

}

cchildwnd1::~cchildwnd1 ()
{

}

BOOL Cchildwnd1::o NERASEBK (hdc hdc) {///
	Test Form 1 Draws a background of green
	hbrush HB = CreateSolidBrush (RGB (0,127,0));
	Rect rect = {0};
	GetClientRect (M_hwnd, &rect);
	FillRect (HDC, &rect, HB);
	DeleteObject (HB);
	return 0;
}


Childwnd2.h:interface for the CChildWnd2 class.
//////////////////////////////////////////////////////////////////////

#if!defined (AFX_CHILDWND2_H_ _b96fdc39_0ce0_45fa_b2fb_8cbf93346686__included_)
#define Afx_childwnd2_h__b96fdc39_0ce0_45fa_b2fb_ 8cbf93346686__included_

#if _msc_ver >
#pragma once
#endif//_msc_ver >

#include " MyWnd.h "

class Cchildwnd2:public Cmywnd  
{public
:
	CChildWnd2 ();
	Virtual ~cchildwnd2 ();
    BOOL ONERASEBK (hdc hdc);
};

#endif//!defined (AFX_CHILDWND2_H__B96FDC39_0CE0_45FA_B2FB_8CBF93346686__INCLUDED_)


ChildWnd2.cpp:implementation of the CChildWnd2 class.
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
# Include "ChildWnd2.h"

//////////////////////////////////////////////////////////////////////
// Construction/destruction
//////////////////////////////////////////////////////////////////////

Cchildwnd2::cchildwnd2 ()
{

}

Cchildwnd2::~cchildwnd2 ()
{

}


BOOL Cchildwnd2::o NERASEBK (hdc hdc) {///
	Test Form 2 Draws a background of LAN color
	hbrush HB = CreateSolidBrush (RGB (0,0,127));
	Rect rect = {0};
	GetClientRect (M_hwnd, &rect);
	FillRect (HDC, &rect, HB);
	DeleteObject (HB);
	return 0;
}


In WindowDemo.cpp

WindowDemo.cpp:Defines the entry point for the application.

#include "stdafx.h"
#include "MainWnd.h"
//#include <tchar.h>


int apientry WinMain ( HInstance hinstance,
                     hinstance hprevinstance,
                     LPSTR     lpcmdline,
                     int       ncmdshow)
{
 	// Todo:place code here.
	
	Establish main form
	Cmainwnd wnd;
	HWND hwnd = WND. CreateEx (hinstance, 0, _t ("main form"), Ws_overlappedwindow, NULL, NULL, idr_main);

	Wnd. Show (sw_show);

	if (!hwnd)
	{
		MessageBox (null,_t ("Create main form Error ..."), NULL, MB_OK);
		return 0;
	}
	Message loop
	msg msg = {0};
	while (GetMessage (&msg, NULL, 0, 0))
	{
		translatemessage (&msg);
		DispatchMessage (&msg);
	}
	return 0;
}




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.