Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5463597
  • 博文数量: 671
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 7310
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-14 09:56
文章分类

全部博文(671)

文章存档

2011年(1)

2010年(2)

2009年(24)

2008年(271)

2007年(319)

2006年(54)

我的朋友

分类: C/C++

2008-09-14 20:41:15

Introduction

The starting question: Why VB & Delphi programmers can easily use fancy controls?

I wrote these classes because my customers was really bored interacting with the usual controls, dialogs and forms: always the same font, the same color, always rectangular shapes... and I agree.

As you know, using only the standard MFC classes, it's really hard to modify the aspect of UI, so I decided to create my own quickly solution for a fancy look for all new programs. Of course these aren't classes which you cannot live without. But, I'm sure, sometime users believe a program is very useful if it has a colored look... [ XP teaches!? -my personal opinion- ]

Here is a description of the classes and few tips for their use.

The Classes

There are these classes in this set:

replaces CStatic and gives you a better control over static text
replaces CEdit improving edit box behavior
replaces CEdit too, but it's dedicated to numerical inputs
replaces check box class
replaces CComboBox, either drop down and drop list
replaces CListBox
replaces CListCtrl in any view
a new CDialog class with colors and images as background
Are you bored using rectangular dialogs?
replaces CFormView. Your MDI applications will never be the same.
replaces CFormView. Your MDI applications with a new look.

Someone could ask me: "What about radio buttons? And buttons?".

My answer: "Sorry, I hare radio buttons, thus... Buttons: I can suggest Davide Calabro's & or Davide Pizzolato's & ... [ Italians do buttons better :)) ]".

If you just have your preferred CMySpecialEdit (derived from CEdit class) and want to integrate its features with CHMXEdit features, all you have to do is derive CHMXEdit from CMySpecialEdit instead of CEdit. In most cases this could be enough.

This is the simplest class of the group. It allows you to have a better control over a static text.
To use a control as CHMXStatic you have to declare it as CHMXStatic instead of CStatic. To do so search the declaration of the control you created and replace CStatic with CHMXStatic.
Don't forget to add

#include "HMXControls.h"

before its use.

Using this control you can:

Set text color SetTextClr(clr) clr represents the color you want to use
Get text color GetTextClr(clr) clr will keep the control text color
Set background color SetBkClr(clr) clr represents the color you want to use
Get background color GetBkClr(clr) clr will keep the control background color
Set transparency SetTransparent(bTransparent) bTransparent represents the flag for transparency; if a control is transparent the background isn't painted
Get transparency GetTransparent(bTransparent) bTransparent will keep the transparency status
Set text font SetTextFont(nHeight, bBold, bItalic, sFaceName) nHeight, bBold, bItalic, sFaceName represent the height in point, the bold flag, the italic flag and the name of the font
Set text font SetTextFont(LogFont) LogFont represent a LOGFONT structure
Get text font GetTextFont(lpLogFont) lpLogFont represent the pointer to a LOGFONT structure
Set text font height SetTextFontHeight(nHeight) nHeight represents the height in point of the font
Set text font bold SetTextFontBold(bBold) bBold represents the flag for the bold style
Set text font italic SetTextFontItalic(bItalic) bItalic represents the flag for the italic style
Set text font face name SetTextFontFaceName(sFaceName) sFaceName represents the name of the font
Set font rotation SetFontRotation(nAngle) nAngle represents the angle of the rotation
Set tooltip text SetToolTipText(sToolTip, bActivate) sToolTip, bActivate represent the tooltip text and the activation flag
Activate tooltip ActivateToolTip(bActivate) bActivate represents the activation flag

Of course, if you're looking for a 'full of features' CStatic class, I can suggest [thanks Mr. Almond, your job is fantastic].

This class replaces CEdit class. It allows you to control the edit box appearance. As you can see, I created a particular method: EnableEditing; in this way this kind of control acts better than CEdit with EnableWindow or SetReadOnly.

To use a control as CHMXEdit you have to declare it as CHMXEdit instead of CEdit. To do so, refer to CHMXStatic section.

Don't forget to

#include "HMXControls.h"

before its use.

Using this control you can:

Set text color SetTextClr(clr) clr represents the color you want to use
Get text color GetTextClr(clr) clr will keep the control text color
Set text color focus SetTextClrFocus(clr) clr represents the color you want to use if control has focus
Get text color focus GetTextClrFocus(clr) clr will keep the control text color when the control has the focus
Set background color SetBkClr(clr) clr represents the color you want to use
Get background color GetBkClr(clr) clr will keep the control background color
Set background color focus SetBkClrFocus(clr) clr represents the color you want to use when control has focus
Get background color focus GetBkClrFocus(clr) clr will keep the control background color when control has focus
Set text font SetTextFont(nHeight, bBold, bItalic, sFaceName) nHeight, bBold, bItalic, sFaceName represent the height in point, the bold flag, the italic flag and the name of the font
Set text font SetTextFont(LogFont) LogFont represent a LOGFONT structure
Get text font GetTextFont(lpLogFont) lpLogFont represent the pointer to a LOGFONT structure
Set text font height SetTextFontHeight(nHeight) nHeight represents the height in point of the font
Set text font bold SetTextFontBold(bBold) bBold represents the flag for the bold style
Set text font italic SetTextFontItalic(bItalic) bItalic represents the flag for the italic style
Set text font face name SetTextFontFaceName(sFaceName) sFaceName represents the name of the font
Set tooltip text SetToolTipText(sToolTip, bActivate) sToolTip, bActivate represent the tooltip text and the activation flag
Activate tooltip ActivateToolTip(bActivate) bActivate represents the activation flag
Enable editing EnableEditing(bEditing) bEditing represents the editing flag

This class depends on CHMXEdit and must be used if you have numeric input. Once created, this control allows you to type only digits, decimal separator, plus, minus and 'e'.

I implemented a solution for 'localization & decimal separator problem'. In a normal CEdit control, if you type '.' you get (of course) the dot. If you try to do the same using Calc (windows calculator) if you type '.' you get the decimal separator (as defined in Control Panel). Excel does the same. I mean it's really comfortable typing numbers using only the numpad, especially for Europeans that use the comma as decimal separator. This class allows you to have this useful feature. To complete the operation, you have to use

setlocale( LC_ALL, ".OCP" );

in this way you inform your program to use regional settings from the control panel: every time you press '.' you'll get your own decimal separator.

To use a control as CHMXNumEdit you have to declare it as CHMXNumEdit instead of CEdit. See previous sections.

As you know,

#include "HMXControls.h"

before its use.

It's derived from CHMXEdit so you can refer to CHMXEdit section. Using this control you also can:

Set engineer format SetEngFormat(bEngFormat) bEngFormat represents the flag for engineer format
Get engineer format GetEngFormat(bEngFormat) bEngFormat will keep the format used

This class, improving the standard CButton class, allows you to control the check box appearance. To use a check box as CHMXCheckBox you have to declare it as CHMXCheckBox instead of CButton.
Don't forget to add

#include "HMXControls.h"

before its use.

Using this control you can:

Set text color SetTextClr(clr) clr represents the color you want to use
Get text color GetTextClr(clr) clr will keep the control text color
Set background color SetBkClr(clr) clr represents the color you want to use
Get background color GetBkClr(clr) clr will keep the control background color
Set transparency SetTransparent(bTransparent) bTransparent represents the flag for transparency; if a control is transparent the background isn't painted
Get transparency GetTransparent(bTransparent) bTransparent will keep the transparency status
Set text font SetTextFont(nHeight, bBold, bItalic, sFaceName) nHeight, bBold, bItalic, sFaceName represent the height in point, the bold flag, the italic flag and the name of the font
Set text font SetTextFont(LogFont) LogFont represent a LOGFONT structure
Get text font GetTextFont(lpLogFont) lpLogFont represent the pointer to a LOGFONT structure
Set text font height SetTextFontHeight(nHeight) nHeight represents the height in point of the font
Set text font bold SetTextFontBold(bBold) bBold represents the flag for the bold style
Set text font italic SetTextFontItalic(bItalic) bItalic represents the flag for the italic style
Set text font face name SetTextFontFaceName(sFaceName) sFaceName represents the name of the font
Set tooltip text SetToolTipText(sToolTip, bActivate) sToolTip, bActivate represent the tooltip text and the activation flag
Activate tooltip ActivateToolTip(bActivate) bActivate represents the activation flag
Enable editing EnableEditing(bEditing) bEditing represents the editing flag

This class replaces CCombobox class either drop list and drop down. It allows you to control the combo box appearance. To use a control as CHMXComboBox you have to declare it as CHMXComboBox instead of CComboBox.

Don't forget to

#include "HMXControls.h"

before its use.

Using this control you can:

Set text color SetTextClr(clr) clr represents the color you want to use
Get text color GetTextClr(clr) clr will keep the control text color
Set background color SetBkClr(clr) clr represents the color you want to use
Get background color GetBkClr(clr) clr will keep the control background color
Set text font SetTextFont(nHeight, bBold, bItalic, sFaceName) nHeight, bBold, bItalic, sFaceName represent the height in point, the bold flag, the italic flag and the name of the font
Set text font SetTextFont(LogFont) LogFont represent a LOGFONT structure
Get text font GetTextFont(lpLogFont) lpLogFont represent the pointer to a LOGFONT structure
Set text font height SetTextFontHeight(nHeight) nHeight represents the height in point of the font
Set text font bold SetTextFontBold(bBold) bBold represents the flag for the bold style
Set text font italic SetTextFontItalic(bItalic) bItalic represents the flag for the italic style
Set text font face name SetTextFontFaceName(sFaceName) sFaceName represents the name of the font
Set tooltip text SetToolTipText(sToolTip, bActivate) sToolTip, bActivate represent the tooltip text and the activation flag
Activate tooltip ActivateToolTip(bActivate) bActivate represents the activation flag
Enable editing EnableEditing(bEditing) bEditing represents the editing flag

If you want to use a list box and want to set font, colors e background colors you have to use the CHMXListBox class.

Don't forget to

#include "HMXControls.h"

before its use.

Using this control you can:

Set text color SetTextClr(clr) clr represents the color you want to use
Get text color GetTextClr(clr) clr will keep the control text color
Set background color SetBkClr(clr) clr represents the color you want to use
Get background color GetBkClr(clr) clr will keep the control background color
Set text font SetTextFont(nHeight, bBold, bItalic, sFaceName) nHeight, bBold, bItalic, sFaceName represent the height in point, the bold flag, the italic flag and the name of the font
Set text font SetTextFont(LogFont) LogFont represent a LOGFONT structure
Get text font GetTextFont(lpLogFont) lpLogFont represent the pointer to a LOGFONT structure
Set text font height SetTextFontHeight(nHeight) nHeight represents the height in point of the font
Set text font bold SetTextFontBold(bBold) bBold represents the flag for the bold style
Set text font italic SetTextFontItalic(bItalic) bItalic represents the flag for the italic style
Set text font face name SetTextFontFaceName(sFaceName) sFaceName represents the name of the font
Set tooltip text SetToolTipText(sToolTip, bActivate) sToolTip, bActivate represent the tooltip text and the activation flag
Activate tooltip ActivateToolTip(bActivate) bActivate represents the activation flag
Enable editing EnableEditing(bEditing) bEditing represents the editing flag

Refers to CHMXListBox.

With this class you can create a dialog having a colored background or a bitmap as background too. Using a BMP as background, it possible to have the image centered, tiled or stretched.
This class allows you to:

Set background color SetBkClr(clr) clr is the color of the dialog background
Get background color GetBkClr(clr) clr will keep the dialog background color
Set background bitmap SetBitmap(sFileName, nStyle) sFileName is the file contains the image, nStyle represents the style of the image painting method: centered, tiled or stretched
Set background bitmap SetBitmap(nResource, nStyle) nResource is the resource ID contains the image, nStyle represents the style of the image painting method

To use this dialog you have to design your own dialog via resource editor as you normally do using Visual Studio. Remember to increase control sizes if you plan to use custom text for labels (static), check boxes and edit boxes; combo boxes increase their sizes automatically. Now, using ClassWizard, create your MyDialog.cpp & MyDialog.h (your class name could by CMyDialog). ClassWizard helps you to create variables associated to controls like m_edtText for a CEdit control, m_lblLabel for a CStatic control and so on.

In order to let CHMXDialog derived class works correctly make following changes:

  • open MyDialog.h,
  • type
    #include "HMXControls.h"
    just before the class definition,
  • replace CDialog with CHMXDialog in class definition,
  • if required, convert each control in this header file using this conversion table:

    Old New notes
    CStatic CHMXStatic  
    CEdit CHMXEdit for alpha & numeric edit box
    CEdit CHMXNumEdit only numeric edit box
    CButton CHMXCheckBox NOT FOR BUTTON!! ONLY CHECKBOX
    CComboBox CHMXComboBox Drop Down & Drop List
    CListBox CHMXListBox  
    CListCtrl CHMXListCtrl  

  • It's necessary to override OnInitDialog in order to modify dialog & controls make-up,
  • open MyDialog.cpp,
  • make following changes:

    Where Old New
    constructor CDialog(
    CMyDialog::IDD, pParent)
    CHMXDialog(
    CMyDialog::IDD, pParent)
    DoDataExchange CDialog::DoDataExchange(pDX); CHMXDialog::
    DoDataExchange(pDX);
    BEGIN_MESSAGE_MAP BEGIN_MESSAGE_MAP(CMyDialog, CDialog) BEGIN_MESSAGE_MAP(CMyDialog, CHMXDialog)
    OnInitDialog CDialog::OnInitDialog(); CHMXDialog::
    OnInitDialog();

As the name suggests, with this class you can create a dialog with its own shape, depending on the bitmap file selected.
This class allows you to set a bitmap as 'mask' via a method like this:

SetRegionFile( sFileName, clrTransp )

where sFileName represents the file containing BMP image and clrTransp is the color that must be used as transparent color.

Important: this dialog must be without caption and border.

This class allows you to:

Set background color SetBkClr(clr) clr is the color of the dialog background
Get background color GetBkClr(clr) clr will keep the dialog background color
Set background bitmap SetBitmap(sFileName, nStyle) sFileName is the file contains the image, nStyle represents the style of the image painting method: centered, tiled or stretched
Set background bitmap SetBitmap(nResource, nStyle) nResource is the resource ID contains the image, nStyle represents the style of the image painting method

Using Visual Studio Resource Editor, design your own form and, via Class wizard, create your CMyFormView.
In order to let CHMXFormView derived class works correctly make following changes:
  • open MyFormView.h,
  • type
    #include "HMXControls.h"
    just before the class definition,
  • replace CFormView with CHMXFormView in class definition,
  • if required, convert each control in this header file using this conversion table:

    Old New notes
    CStatic CHMXStatic  
    CEdit CHMXEdit for alpha & numeric edit box
    CEdit CHMXNumEdit only numeric edit box
    CButton CHMXCheckBox NOT FOR BUTTON!! ONLY CHECKBOX
    CComboBox CHMXComboBox Drop Down & Drop List
    CListBox CHMXListBox  
    CListCtrl CHMXListCtrl  

  • It's necessary to override OnInitalUpdate in order to modify form & controls make-up,
  • open MyFormView.cpp,
  • make following changes:

    Where Old New
    constructor CFormView(CMyFormView::IDD) CHMXFormView(CMyFormView::IDD)
    DoDataExchange CFormView::
    DoDataExchange(pDX);
    CHMXFormView(CMyFormView::IDD)
    BEGIN_MESSAGE_MAP BEGIN_MESSAGE_MAP(CMyFormView, CFormView) BEGIN_MESSAGE_MAP(CMyFormView, CHMXFormView)
    OnInitialUpdate CFormView::
    OnInitalUpdate();
    CHMXFormView::
    OnInitalUpdate();

In method CMyFormView::OnInitialUpdate(), just before CHMXFormView::OnInitalUpdate() I suggest to type:

	GetParentFrame()->RecalcLayout();
	ResizeParentToFit(FALSE);

In this way the form will have the right size.

This section is a little bit tricky... if you follow this instructions you can get the right results. Desing your own form using Resource Editor and, via Class Wizard, create your own CMyShapedFormView. Now follow these steps:

  • open main file MyProject.h
  • add extern CString g_sMyShapedFormViewBMPFile;
  • add extern COLORREF g_rgbMyShapedFormViewRGBTransp;
  • open main file MyProject.cpp
  • type #include "HMXcontrols.h"
  • replace RUNTIME_CLASS(CChildFrame), with RUNTIME_CLASS(CHMXChildFrame) in Document templates registrations
  • in InitInstance add g_sMyShapedFormViewBMPFile = "MyImage.BMP";
  • in InitInstance add g_rgbMyShapedFormViewRGBTransp = RGB( x, y, z);
  • Design form via Resource Editor
  • Create, using Class Wizard, the class CMyShapedFormView (derived from CFormView)
  • Open MyShapedFormView.h
  • type #include "HMXcontrols.h"
  • replace class CMyShapedFormView : public CFormView with class CMyShapedFormView : public CHMXShapedFormView
  • open MyShapedFormView.cpp
  • add CString g_sMyShapedFormViewBMPFile;
  • add COLORREF g_rgbMyShapedFormViewRGBTransp;
  • replace CMyShapedFormView(CMyShapedFormView::IDD ) with CMyShapedFormView(CMyShapedFormView::IDD, g_sMyShapedFormViewBMPFile, g_rgbMyShapedFormViewRGBTransp )
  • replace CFormView::DoDataExchange(pDX) with CHMXShapedFormView::DoDataExchange(pDX)
  • replace BEGIN_MESSAGE_MAP(CMyShapedFormView, CFormView) with BEGIN_MESSAGE_MAP(CMyShapedFormView, CHMXShapedFormView)
  • in OnInitialUpdate: add GetParentFrame()->RecalcLayout(); & ResizeParentToFit(FALSE);

Special thanks to:

  • Davide Calabro' and his CButtonST
  • Norm Almond and his CLabel
  • David Gallardo Llopis and his AnyForm project
  • Jörg König and his CDIBitmap & CBmpPalette
  • Donatella, my wife, for her huge patience :)

Disclaimer

THE SOFTWARE AND THE ACCOMPANYING FILES ARE DISTRIBUTED "AS IS" AND WITHOUT ANY WARRANTIES WHETHER EXPRESSED OR IMPLIED. NO RESPONSIBILITIES FOR POSSIBLE DAMAGES OR EVEN FUNCTIONALITY CAN BE TAKEN. THE USER MUST ASSUME THE ENTIRE RISK OF USING THIS SOFTWARE.

Terms of use

THIS SOFTWARE IS FREE FOR PERSONAL USE, FREEWARE OR COMMERCIAL APPLICATIONS.

Last and least

Few minutes to waste? Visit my home page and sing my guestbook.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found

阅读(1499) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~