Use VC ++ to implement full screen display

Source: Internet
Author: User
Use VC ++ to implement full screen display


I. Implementation Method

It is actually easy to implement the full screen of a program. The implementation idea is: first, you must define a window class and reload the onpaint () or ondraw () Functions of the window class, implement the specific window display function, and then call the API function getdevicecaps (INT nindex) to obtain the size of the current display screen, for example, if nindex is set to the logpixelsx value, the screen width (in pixels) can be obtained. If nindex is set to the logpixelsy value, the screen height can be obtained. The screen size is not enough. You must call an API function movewindow () to locate the current window to the entire screen. You need to note that, in order to realize the interaction between the window and the user, you need to load a cursor in the window. In addition, to restore the full screen display to normal state, you need to process the wm_keydown message in this window class. In this message processing function, identify whether the user has pressed the ESC (exit) key. If yes, the program returns to the normal display status. The following code implements all of the above functions and responds to the mouse operation to display the digital curve on the screen. The procedure is as follows:

1. Define a window class cgribblewnd. It contains two integer variables m_pixelsx and m_pixelsy, which are used to store the window size respectively;

2. Use classwizard to reload the onpain () function of the window class to implement the specific display function;

3. Use classwizard to add message processing functions such as wm_create, wm_erasebkgnd, wm_keydown, and wm_lbuttondown for the window class to implement full screen Windows and respond to user operations;

--

 

Ii. program code:

//////////////////////////////////////// /// Header file of the cgribblewnd class

# If! Defined (afx_gribblewnd_h1_6e1d4ed1_d9a6_11d4_9b9f_525400dae6a01_encoded _)

# Define afx_gribblewnd_h1_6e1d4ed1_d9a6_11d4_9b9f_525400dae6a01_encoded _

# If _ msc_ver> 1000

# Pragma once

# Endif // _ msc_ver> 1000

Class cgribblewnd: Public cwnd

{

// Construction

Public:

Cgribblewnd ();

// Attributes

Public:

// Screen stuff

Int m_pixelsx; // The Screen width;

Int m_pixelsy; // The height of the screen;

CDC * m_pdc; // context object of the device;

Hcursor m_hcursor; // cursor handle;

Colorref m_backcolor; // color object, used to store custom colors;

// Operations

// Overrides

// Classwizard generated virtual function overrides

// {Afx_virtual (cgribblewnd)

//} Afx_virtual

// Implementation

Virtual ~ Cgribblewnd ();

// Generated message map Functions

Protected:

// {Afx_msg (cgribblewnd)

Afx_msg bool onerasebkgnd (CDC * PDC );

Afx_msg int oncreate (maid );

Afx_msg void onlbuttondown (uint nflags, cpoint point );

Afx_msg void onpaint ();

Afx_msg void onkeydown (uint nchar, uint nrepcnt, uint nflags );

//} Afx_msg

Declare_message_map ()

};

// {Afx_insert_location }}

# Endif //! Defined (afx_gribblewnd_h1_6e1d4ed1_d9a6_11d4_9b9f_525400dae6a01_encoded _)

//////////////////////////////////////// ///////////// Implementation file of the cgriblewnd class;

# Include "stdafx. H"

# Include "gribblewnd. H"

# Include

# Ifdef _ debug

# Define new debug_new

# UNDEF this_file

Static char this_file [] = _ file __;

# Endif

Cgribblewnd: cgribblewnd () // constructor;

{

}

Cgribblewnd ::~ Cgribblewnd () // destructor;

{

}

Begin_message_map (cgribblewnd, cwnd)

// {Afx_msg_map (cgribblewnd)

On_wm_erasebkgnd ()

On_wm_create ()

On_wm_lbuttondown ()

On_wm_paint ()

On_wm_keydown ()

On_wm_timer ()

//} Afx_msg_map

End_message_map ()

--

 

Bool cgribblewnd: onerasebkgnd (CDC * PDC) // redraws the background of the background window;

{

M_backcolor = RGB (125,200,125); // custom color;

// Generate a new paint brush and refresh the display area with custom colors;

Cbrush CB (m_backcolor );

Hbrush holdbrush = (hbrush) PDC-> SelectObject (CB );

Rect = {0, 0, m_pixelsx, m_pixelsy };

PDC-> fillrect (& rect, & CB );

PDC-> SelectObject (holdbrush );

CB. deleteobject ();

Return true;

}

Int cgribblewnd: oncreate (maid) // create window;

{

If (cwnd: oncreate (lpcreatestruct) =-1)

Return-1;

// Todo: add your specialized creation code here

M_hcursor = loadcursor (null, idc_arrow); // load the mouse to interact with the user;

Setcursor (m_hcursor); // attaches the loaded mouse to the generated form;

Showcursor (true); // displays the mouse;

M_pdc = getdc (); // get the device context object of the current form;

   

M_pixelsx = m_pdc-> getdevicecaps (horzres); // obtain the screen width;

M_pixelsy = m_pdc-> getdevicecaps (vertres); // gets the screen height;

Movewindow (, m_pixelsx, m_pixelsy); // displays the full screen of the current window;

Return 0;

}

Void cgribblewnd: onlbuttondown (uint nflags, cpoint point) // responds to the user's mouse click operation;

{

Invalidate (); // redraw the window;

}

Void cgribblewnd: onpaint () // to display the specific effect. This example is used to display the mathematical curve;

{

Cpaintdc DC (this); // obtain the context object of the device;

// The following code is used to display the mathematical curve;

Static int s_nlisxcoef = 1;

Static int s_nlisycoef = 3;

Static int s_nlisyoffset = 44;

++ S_nlisxcoef;

++ S_nlisycoef;

Int X, Y;

For (float T = 0; t <32767; t + = 0.2 ){

X = sin (s_nlisxcoef * t) * m_pixelsx/2 + m_pixelsx/2;

Y = sin (s_nlisycoef * t + s_nlisyoffset) * m_pixelsy/2 + m_pixelsy/2;

DC. setpixel (X, Y, RGB (255, 80 ));

}

}

// Responds to the user's key operation. After the user presses the ESC (exit) key, the program returns to normal display;

Void cgribblewnd: onkeydown (uint nchar, uint nrepcnt, uint nflags)

{

If (vk_escape = nchar) // determines whether the currently pressed key is "ESC"

Destroywindow (); // destroy the current form;

Cwnd: onkeydown (nchar, nrepcnt, nflags );

}

Iv. Summary

There are many ways to implement full screen display of programs. For example, another feasible idea is:

1) A structure windowplacement provided by Windows API is used to store the position and display information of the front view and the main frame window on the screen, and the explicit and hidden information of the non-customer window, to restore the original window when the screen is displayed;

2) use the API function: getwindowtopwindow () to get the desktop window. The size of the display is obtained by: getwindowrect;

3) Use: adjustwindowrect () to calculate the size of the window corresponding to the screen size when the customer area is large;

4) Use: setwindowplacement () to set the View window and the main window as the client area size and screen size;

5) process the window message wm_getminmaxinfo so that the window can be smoothly maximized and minimized. It can be seen that compared with the implementation method in this article, this method is very cumbersome to implement and is not suitable

 

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.