Push Button Background Collection Revision (Complete Engineering)

zhaozj2021-02-16  59

About Push Button, there have been two more classic articles:

http://www.9cbs.net/develop/read_article.asp?id=11690

http://www.9cbs.net/develop/read_article.asp?id=8432

(Second or http://www.9cbs.net/develop/read_article.asp?id=11689, the same content)

But these two are not perfect,

There are many mistakes in the first one, and there is no way to compile, not suitable for beginners, especially the buttons, no text, and the second article can only be said to be a pseudo code, just a thinking, given this, especially Out a complete example and code):

First of all, it is still from CButton to inherit a new class: CMYBUTTON

head File:

#if! defined (AFX_MYBUTTON_H__35978C4D_94AF_40FD_A82C_6DB1847775D7__INCluded _) # define afx_mybutton_h__35978c4d_94af_40fd_a82c_6db1847775d7__included_

#if _msc_ver> 1000 # pragma overce # endif //_msc_ver> 1000 // MyButton.h: header file //

/// CMYBUTTON WINDOW

Class CMYBUTTON: PUBLIC CBUTTON {// Construction // Declare_Dynamic (CMYButton)

Public: cmybutton (); Virtual ~ cmyButton (); // attributespublic: // OperationSpublic:

// Overrides // ClassWizard generated virtual function overrides // {{AFX_VIRTUAL (CMyButton) public: virtual void DrawItem (LPDRAWITEMSTRUCT lpDrawItemStruct); // override this function //}} AFX_VIRTUAL

// ImplementationPublic: // {{AFX_MSG (CMYBUTTON) // Note - The classwizard will address and remove member functions here. //}} AFX_MSG

DECLARE_MESSAGE_MAP ()};

/

// {{AFX_INSERT_LOCATION}} // Microsoft Visual C Will Insert Additional Declarations Immediately Before The Previous Line.

#ENDIF / /! Defined (AFX_MYBUTTON_H___35978C4D_94AF_40FD_A82C_6DB1847775D7__INCLUDED_)

CPP file:

// mybutton.cpp: importation file //

#include "stdafx.h" #include "editor.h" #include "mybutton.h"

#ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # ENDIF

/// cmybutton

CMYBUTTON :: CMYBUTTON () {}

CMYBUTTON :: ~ cmybutton () {}

BEGIN_MESSAGE_MAP (CMyButton, CButton) // {{AFX_MSG_MAP (CMyButton) // NOTE -. The ClassWizard will add and remove mapping macros here //}} AFX_MSG_MAPEND_MESSAGE_MAP () /// CMyButton message handlers

void CMyButton :: DrawItem (LPDRAWITEMSTRUCT lpDrawItemStruct) {// TODO: Add your code to draw the specified item CDC dc; dc.Attach (lpDrawItemStruct-> hDC); // get the drawing device context CDC VERIFY (lpDrawItemStruct-> CtlType = = ODT_BUTTON);

// The steps here are: 1, first get the text of the button edited in the resource, then reactivate this text to the button, and set the background color of this text to transparent, so on the button Writing only

Const int buffer = 512; tchar buffer [buffs]; getWindowText (buffer, bufsize);

int size = strlen (buffer); // get the length DrawText (lpDrawItemStruct-> hDC, buffer, size, & lpDrawItemStruct-> rcItem, DT_VCENTER | DT_SINGLELINE | DT_TABSTOP); // draw text SetBkMode (lpDrawItemStruct-> hDC, TRANSPARENT); / /Transparent

IF (LPDRAWITEMSTRUCT-> ITEMACTION & ODA_DRAWENTIRE) // When the button is pressed {// // Rehabilizes the entire control CBRUSH Brush (RGB (255, 0, 0)); // The color is pressed to red DC. FillRect (& (lpDrawItemStruct-> rcItem), & brush); // DrawText (lpDrawItemStruct-> hDC, buffer, size, & lpDrawItemStruct-> rcItem, DT_VCENTER | DT_SINGLELINE | DT_TABSTOP); // because there were redrawn, the SetBkMode ( LPDRAWITEMSTRUCT-> HDC, Transparent; // Taking the text also to rehabilitate} Else // When the button bounces {CBRUSH Brush (RGB (255, 255, 0)); // Bush into the color to be yellow dc.FillRect & (lpDrawItemStruct-> rcItem), & brush); // DrawText (lpDrawItemStruct-> hDC, buffer, size, & lpDrawItemStruct-> rcItem, DT_VCENTER | DT_SINGLELINE | DT_TABSTOP); // ibid., redraw the text SetBkMode (lpDrawItemStruct-> hDC , TRANSPARENT);

if ((lpDrawItemStruct-> itemState & ODS_SELECTED) && (lpDrawItemStruct-> itemAction & (ODA_SELECT | ODA_DRAWENTIRE))) {// the controls is selected, the highlighted frame COLORREF fc = RGB (255-GetRValue (m_color), 255-GetGValue ( m_color && (LPDRAWITEMSTRUCT)) {// Control The selected state ends, remove the border Cbrush Brush (RGB (0,255,0)); DC.FrameRect (& lpdrawItemstruct-> rcitem, & brush); //

} Dc.detach (); //}

The new class is built well, and it will be handed next.

Create a new dialog, create a new button: IDC_Test,

At the same time, use the class wizard to add a member variable: CMYBUTTON M_TEST

(Remember to add: #include "mybutton.h") in testdlg.h

Then plug in OnInitDialog () in TestDlg.cpp:

m_test.subclassdlgitem (IDC_TEST, THIS);

At the same time, add a function onTest () for the button.

Void ctestdlg :: ontest () {MessageBox ("ok");}

Run, OK!

Since the company FTP is unable to connect,

If you need code engineering, please contact me:

Zhucde@163.com

MSN: zhucde@hotmail.com

转载请注明原文地址:https://www.9cbs.com/read-23338.html

New Post(0)