Visual C ++ Form Design Tips

xiaoxiao2021-04-10  412

How to make transparent form

Using SetLayeredWindowAttributes can easily make transparent forms, this function is supported above W2K, and if you want to use it, you may need to download the latest SDK. However, this function is implemented in the W2K user32.dll, so if you don't want to download huge SDK, you can use GetProcAddress to get the pointer of the function.

The function prototype of setlayeredWindowAttributes is as follows:

Bool setlayeredWindowAttributes

HWND HWND, // Handle to the Layered Window

ColorRef CRKEY, / / ​​SPECIFIES THE Color Key

Byte Balpha, // Value for the Blend Function

DWORD DWFLAGS // Action

);

Windows NT / 2000 / XP: Included in Windows 2000 and Later.

Windows 95/98 / me: unsupported. (Note that in Win9x is not available)

Header: declared in winuser.h; include windows.h.

Library: USE User32.lib.

Some constants:

WS_EX_LAYERED = 0x80000;

LWA_ALPHA = 0x2;

LWA_COLORKEY = 0x1;

Where dwflags have lwa_alpha and lwa_colorKey

LWA_ALPHA is set, the transparency is determined by Balpha.

If LWA_COLORKEY is set, specify that the transparent color is CRKEY, and other colors are displayed normally.

To make the form possesses a transparent effect, you must first have a WS_EX_LAYERED extension attribute (the old SDK does not define this property, so you can specify directly as 0x80000).

Example code:

Add in OnInitDialog ():

/ / Add WS_EX_LAYED extension attribute

SetWindowlong (this-> getsafehwnd (), gwl_exstyle,

GetWindowlong (this-> getsafehwnd (), gwl_exstyle) ^ 0x80000);

Hinstance hinst = loadingLibrary ("user32.dll");

IF (hinst)

{

Typedef Bool (WinApi * Myfunc) (HWND, ColorRef, Byte, DWORD);

Myfunc fun = null;

// get the setlayeredWindowAttributes function pointer

Fun = (MyFunc) GetProcaddress (Hinst, "SetlayeredWindowAttributes");

IF (fun) Fun (this-> getsafehwnd (), 0, 128, 2);

Freelibrary (HINST);

}

Slightly modification can also make a fade out of the fade out. Pay attention to the third parameter (128) Don't get too small, it is completely transparent to 0 words, can't see it.

How to make the icon of the frame window as an animation

You can use Timer, but Timer cannot be valid. Because Timer sent a window message, when the window is busy with the message, the mouse and other messages cannot process Timer in time, the interval time becomes very long.

You can consider using a separate Timer thread to resolve this issue with SLEEP ().

Uint Timer (LPVOID Param)

{

HWnd hwnd = (hwnd) param

While (1)

{

SLEEP (MS);

Postmessage (hwnd, ch_picture, null, null)

}

}

Sleep (MS) Send a custom message. The message processing function selects an ICON or Bitmap to display. Such as: mybotton.setbitmap (hbitmap) Bitmap [i]);

Bitmap is a bitmap array that stores J bitmaps. The message processing function is running once, i is accumulating once, when i == j, i will return to 0;

Prevent window flashing method

1. Replace INVALIDATE () in invalidateecture ().

Invalidate () will cause image redraw between the entire window, which requires a longer time, and invalidateERECT () only retires content in the Rect area, so the required time will be less. Before the worms are too lazy, INVALIDATE () often calls invalidate (), don't want to calculate the RECT that needs to be heavy, but the truth is that if you really need to improve the blinking, calculate a RECT. Time is much less than the time you need to redraw more than you don't need redraw.

2, prohibiting the system from eradicating your window.

The system will help you use the specified background color to remove the window with the specified background. However, it may be very small in the area that needs to be heavy. Alternatively, you have to start a lot of calculations between these things. At this time you can prohibit the system from dropping the original image. Until you have calculated all the data, you will cover the parts that need to be removed with a background (such as: Dc.FillRect (Rect, & Brush); Rect is the area that needs to be removed, brush is a brush with a background color. ), Draw new graphics. To prohibit the system to remove your window, you can override the onerasebkgnd () function, let it return to true. Such as

Bool CMYWIN :: OneRaseBkGnd (CDC * PDC)

{

Return True;

// Return CWND :: OneRaseBkGnd (PDC); // comment out this statement of the system.

}

3. Effectively remove it.

When you remove the background, don't take it without the places. For example, you put a big Edit box on a window, almost all the windows, then you frequently remove the entire window background will result in EDIT non-stop to form a dramatic flashing. In fact, you can create a region that needs to be removed, just removes this part. Such as

GetClientRect (RectClient);

Rgn1.createRectrGnIndirect (RectClient);

Rgn2.createRectrGnIndirect (m_regit);

IF (RGN1.Combinergn (& RGN1, & RGN2, RGN_XOR) == Error) // After processing RGN1 only includes a customer area other than the Edit box, this, Edit will not be overwritten by my background and lead to heavy draws.

{

Assert (false);

Return;

}

Brush.createsolidbrush (m_clrbackgnd);

PDC-> FillRgn (& RGN1, & Brush);

Brush.deleteObject ();

Note: Method 2 is used at the same time when using this method. Don't forget, when you say that insects, the worm is not good.

4. Use Memorydc first in memory, replicate the picture, and then copy it on the screen.

This is used for a long time for a drawing process. After all, the memory operation is relatively fast, and the copy to the screen is one-time, at least there will be clearly seen that a situation from the left to the right.

Void cmywin :: onpaint ()

{

CPAINTDC DC1 (this); // Device Context for Painting

DcMemory.createCompatibleDC (& DC1); cbitmap bmp; // The bitmap here is required, otherwise it is a big black block.

Bmp.createCompaTibleBitmap (& DC1, RectClient.width (), RectClient.Height ());

DcMemory.selectObject (& BMP);

// What do you want to paint?

//dcmemory.FillRect(autclient ";

DC1.Bitblt (0, 0, RectClient.width (), RectClient.Height (), & DCMEMORY, 0, 0, SRCCOPY;

DcMemory.deletedc ();

// do not call cWnd :: onpaint () for Painting Messages

}

How to implement full screen display

Full-screen display is an essential function of some application software programs. For example, when using resources such as VC editing a project source file or editing dialog, select the menu "ViewFull Screen", you can enter the full screen display status, and press the "ESC" button to exit the full screen display status.

In VC 6.0 we use AppWizard to generate a single document interface by default. Here, you will first discuss the method of clicking on the menu item "ViewFull Screen" to implement full screen display, and how to exit the full screen display status after pressing the "ESC" button.

1) In the CMAINFRAME class, increase the following three member variables.

Private:

WindowPlaceMent M_OldWndplacement; // Used to save the original window location

BOOL M_BFULLSCREEN; / / full screen display logo

CRECT M_FULLSCREENRECT; / / indicates the window position when the full screen is displayed

2) Edit menu IDR_MAINFRAME in the resource editor. Add menu items "full screen" under the "View" menu bar. In its properties box, the ID is set to ID_FULL_SCREEN, and CAPTION is "full screen". You can also add a new tool icon in the toolbar, and make it associated with the menu item "full screen", which is also set to ID_FULL_SCREEN.

3) Design full screen display processing function, add the response function of the above-described menu item ID_full_screen message at the CMAINFRAME class. The response function is as follows:

Void CMAINFRAME :: onfullscreen ()

{

GetWindowPlacement (& M_OLDWNDPLACEMENT);

CRECT WINDOWRECT;

GetWindowRect (& WindowRect);

CRECT ClientRect;

Repositionbars (0, 0xfff, AFX_IDW_PANE_FIRST, ReposQuery, & ClientRect);

ClientToscreen (& ClientRect);

/ / Get the resolution of the screen

INT nfulwidth = getSystemMetrics (SM_CXSCREEN);

Int nfullheight = getSystemMetrics (SM_CYSCREEN);

// The full screen of the customer area except the control bar is displayed to the NFullWidth, NfullHeight area, extends the original window and the resolution strip in two points (NFULLWIDTH, NFULLHEIGHT). The difference between the client area location is the window location displayed by the full screen.

M_FullscreenRect.Left = WindowRect.Left-ClientRect.Left;

M_FullScreenRect.top = WindowRect.top-clientRect.top; m_fullscreenRect.right = WindowRect.right-clientRect.Right NFullWidth;

M_FullscreenRect.Bottom = WindowRect.Bottom-ClientRect.bottom NfullHeight;

m_bfullscreen = true; // Setting full screen display flag to true

// Enter the full screen display status

WINDOWPLACEMENT WNDPL;

Wndpl.length = sizeof (Windowplacement);

Wndpl.flags = 0;

Wndpl.showcmd = sw_shownormal;

Wndpl.rcnorMalposition = m_fullscreenRect;

Setwindowplacement (& WNDPL);

}

4) The ONGETMAXINFO function of the CMAINFRAME class is overloaded, and the full screen display is provided when the full screen is displayed.

Void CMAINFRAME :: ONGETMINMAXINFO (MinMaxInfo Far * LPMMI)

{

IF (m_bfullscreen)

{

LPMMI-> PTMAXSIZE.X = m_fullscreenRect.width ();

LPMMI-> PTMAXSIZE.Y = M_FullscreenRect.Height ();

LPMMI-> ptmaxposition.x = m_fullscreenRect.width ();

LPMMI-> PTMaxPosition.y = m_fullscreenRect.Height ();

// The largest TRACK size must also change

LPMMI-> PTMaxTracksize.x = m_fullscreenRect.width ();

LPMMI-> PTMaxTracksize.y = m_fullscreenrect.Height ();

}

CframeWnd :: ONGETMINMAXINFO (LPMMI);

}

After completing the above programming, you can connect FullScreen.exe, select the menu "ViewFull Screen" or click the toolbar button associated with it to enter the full screen display status. But now you need to increase the user's operating interface that exits the full-screen display status. Here, how to program how to implement the "ESC" button to exit full screen display status.

1) Select CMAINFRAME in ClassView and click the right mouse button, select "Add Member Function ...", add the member function of the public type EndFullScreen, which will complete the operation of exiting the full screen display.

Void CMAINFRAME :: endfullscreen ()

{

IF (m_bfullscreen)

{// Exit full screen display, restore the original window display

ShowWindow (SW_HIDE);

SetWindowPlacement (& M_OldWndplacement);

}

}

2) The function endfullscreen can exit full-screen display status, and how is the "ESC" button is called to execute this function. Since the view class can handle the relevant messages entered by the keyboard (such as WM_KEYDOWN means that the user presses a key), we will add the Processing button message WM_KEYDOWN in the view class CFULLSCREENVIEW. To determine if the button is pressed for the "ESC" button, the function endfulscreen of the CMAINFRAME class can be quit the full screen display status.

Void cfullscreenview :: onkeydown (uint nchar, uint nrepcnt, uint nflags) {

IF (nchar == vk_escape) // If the button is pressed for the ESC button

{// Get the pointer to the main frame window

CMAINFRAME * PFRAME = (CMAINFRAME *) AFXGetApp () -> m_pmainwnd;

// Call the custom function endfullscreen of the main window class, you can exit full screen display status

Pframe-> endfullscreen ();

}

CView :: onkeyDown (nchar, nrepcnt, nflags);

}

Change the window icon and display it in the taskbar

The following two functions can display a task strip to the taskbar for each sub-window in the application and change their icons. Applications that hide the main window as QQ are particularly useful.

// Function Uses: Change the icon of a window and display it in the taskbar, task switch, task manager

//Parameter Description:

// hwnd To change the window handle of the icon

// HLARGEICON displays icon 32 * 32 on the task cut strip

// HSMallicon Displays icon 16 * 16 other than the task switch

// HiCon displayed icon, 32 * 32, which is automatically compressed into 16 * 16 when displayed outside of the task switching strip.

// Note:

// This function is powerful for the mode dialog.

// If hic is null, the function does not change the window icon, but the original icon is displayed to the taskbar.

// Task switching strip, in the task manager.

// This function is displayed by empty the parent window pointer of the window to the taskbar, task cutting strip,

// In the task manager, after the call is completed, its parent window pointer is no longer available.

Bool Sendwndicontotaskbar (Hind Hwnd, Hicon Hlargeicon, Hicon Hsmallicon);

Bool Sendwndicontotaskbar (HWND HWND, HICON HICON);

Bool Cuiapp :: Sendwndicontotaskbar (Hind Hwnd, Hicon HLARGEICON, HICON HSMALLICON)

{

BOOL RET = True;

ASSERT (HWND);

IF (! :: iswindow (hwnd))

Return False;

/ / Get window pointer

CWND * PWND;

PWND = PWND-> fromHandle (hwnd);

ASSERT (PWND);

IF (! PWND)

Return False;

// Set the parent window to NULL

IF (PWND-> getParent ())

IF (:: setwindowlong (hwnd, gwl_hwndparent, null) == 0)

Return False;

IF (! (pwnd-> modifystyle (null, ws_overlappedwindow)))))

Ret = false;

// Set the window icon

IF (HLARGEICON && HSMALLICON)

{

PWND-> Seticon (HSMallicon, False);

PWND-> Seticon (HLARGEICON, TRUE);

}

Return Ret;

}

Bool Cuiapp :: SendWndicontotaskbar (HWND HWND, HICON HICON)

{

BOOL RET = True;

ASSERT (HWND);

IF (! :: iswindow (hwnd))

Return False;

/ / Get window pointer

CWND * PWND;

PWND = PWND-> fromHandle (hwnd);

Assert (PWND); if (! Pwnd)

Return False;

// Set the parent window to NULL

IF (PWND-> getParent ())

IF (:: setwindowlong (hwnd, gwl_hwndparent, null) == 0)

Return False;

IF (! (pwnd-> modifystyle (null, ws_overlappedwindow)))))

Ret = false;

// Set the window icon

PWND-> Seticon (Hicon, True);

PWND-> Seticon (Hicon, False);

Return Ret;

}

How to hide the application on the taskbar

For CFrameWnd, you can modify the style of the window in the PrecreateWindow () function.

Bool CMAINFRAME :: PrecreateWindow (CreateStruct & Cs)

{

CS.Style | = WS_POPUP; // Make the main window invisible

cs.dwexstyle | = WS_EX_TOOLWINDOW; / / Do not display the task button

Return CFrameWnd :: PrecreateWindow (CS);

}

For other windows, you can use ModifyStyle () and ModifyStyleEx () to modify its style before ShowWindow after ShowWindow after ShowWindow.

How to control the maximum minimum size of the window frame?

To control the maximum minimum size of a frame, you need two things.

Step 1: Handling the message WM_GETMAXINFO in the inheritance class of CFrameWnd, the structure MinMaxInfo sets the entire window class, so remember to consider the size of the toolbar, scroll bar, and the like.

// Maximum smallest dimensional pixel point - example

#define minX 200

#define miny 300

#define maxX 300

#define maxy 400

Void CMYFRAMEWND :: ONGETMINMAXINFO (MinMaxInfo Far * LPMMI)

{

CRECT Rectwindow;

GetWindowRect (& RectWindow);

CRECT RECLIENT;

GetClienceRect (& RectClient);

// Get Offset of Toolbars, Scrollbars, etc.

INT nwidthoffset = RectWindow.width () - RectClient.width ();

INT NHEIGHTOFFSET = RectWindow.height () - RectClient.Height ();

LPMMI-> PTMINTRACKSIZE.X = minx nwidthoffset

LPMMI-> PTMINTRACKSIZE.Y = Miny NHEIGHTOFFSET;

LPMMI-> PTMaxTracksize.x = maxX nwidthoffset;

LPMMI-> PTMaxTracksize.y = MAXY NHEIGHTOFFSET;

}

Step 2: Remove the WS_MaximizeBox message in the PrecreateWindow function of the inheritance class of CFrameWnd, otherwise you will not get expected results when maximizing.

Bool CMYFrameWnd :: PrecreateWindow (CreateStruct & Cs)

{

CS.Style & = ~ ws_maximizebox;

Return CFrameWnd :: PrecreateWindow (CS);

}

How to modify the background color of the Frame window?

The client area of ​​the MDI window is overwritten by another window owned by the Frame window. In order to change the color of the Frame window background, only the background color of this customer area is required. You must handle your WM_ERASEBKND message. Here is the work steps: Create a class inherited from the CWND class, called it cmdiclient;

Add a CMDICLIENT variable in cmdiframewnd; (see the following code)

#include "mdiclient.h"

Class CMAINFRAME: PUBLIC CMDIFRAMEWND

{

...

protected:

Cmdiclient M_WndmdmdphmdineT;

}

Heavy Duty CMDIFRAMEWND :: OnCreateClient, below is this code, please note the subclasswindow ();

Bool CMAINFRAME :: OnCreateClient (lpcreateStruct LPCS, CCReateContext * PContext)

{

IF (cmdiframeWnd :: OnCreateClient (LPCS, PCONTEXT))

{

m_wndmdiclient.subclasswindow (m_hwndmdiclient);

Return True;

}

Else

Return False;

}

Finally, add a function that processes WM_RASEBKGND in cmdinticlient.

How to change the background color of the view?

To change the background color of CVIEW, CFRAMEWND, or CWND object, you need to handle the WM_ERASEBKGND message, the following is a sample code:

Bool csampleview :: OneraseBkGnd (CDC * PDC)

{

/ / Set brush for the background color of the hope

Cbrush backbrush (RGB (255, 128, 128);

/ / Save the old brush

CBRUSH * POLDBRUSH = PDC-> SelectObject (& BACKBRUSH);

CRECT RECT;

PDC-> GetClipbox (& Re);

/ / Drawing area

PDC-> Patblt (Rect. Wid, Rect.top, Rect.width (), Rect.Height (), PATCopy;

PDC-> SELECTOBJECT (POLDBRUSH);

Return True;

}

To change the background color of the CFROMVIEW inherited class, below is a sample code:

Hbrush CMYFORMVIEW :: ONCTLCOLOR (CDC * PDC, CWND * PWND, UINT NCTLCOLOR)

{

Switch (nctlcolor)

{

Case CTLColor_BTN:

Case CTLCOLOR_STATIC:

{

PDC-> setbkmode (transparent);

/ / Do not add any handling or set up a background of transparency

}

Case CTLCOLOR_DLG:

{

CBRUSH * back_brush;

ColorRef color;

Color = (ColorRef) Getsyscolor (color_btnface);

Back_brush = new cbrush (color);

Return (Hbrush) (back_brush-> m_hobject);

}

}

Return (CFormView :: ONCTLCOLOR (PDC, PWND, NCTLCOLOR);

}

How to display an application icon in the taskbar status area

The relevant data is described by the Notifyicondata Structure:

Typedef struct _notifyicondata

{

DWORD CBSIZE; / / The size of the structure must be set

HWND HWND; // Accept the handle of the window of the callback message

UINT UID; // Application Defined Icon Sign

UINT UFLAGS; / / Sign, can be nif_icon, nif_message, nif_tip, or combination thereof

UINT UCALLBACKMESSAGE; / / Application defined callback message logo

Hicon Hicon; // Icon Handle

Char sztip [64]; // Tips string

Notifyicondata, * PNOTIFYICIONDATA;

Function description

Add, delete, and change the icon message from the shell_notifyicon () function.

Winshellapi Bool WinApi Shell_Notify, PNOTIFYICIONDATA PNID;

DwMessage is a sign for the message:

NIM_ADD Add Icons to the Taskbar Notification Area;

NIM_DELETE Deletes the icon of the taskbar notification area;

NIM_MODIFY Changes the icon of the taskbar notification area, callback message flag, callback window handle or prompt string;

PNID is a pointer for the Notifyicondata structure.

The acquisition and processing of callback information

If a taskbar icon has a callback message defined by the application, then when this icon has a mouse operation, the system will send the following message to the window of the HWnd flag:

MessageID = ucallbackmessage

WPARAM = UID

LPARAM = Mouse Event (for example, wm_lbuttondown)

In this way, the system notifies the application user's operation of the icon. If an application generates more than two icons, you can determine which icon returned by the wPARAM. Typically, the standard WIN95 taskbar icon has the following mouse operation response:

When the mouse is on the icon, the system should display the prompt information tooltip;

When you right-click the icon using a mouse, the application should display shortcut menus;

When using the left mouse button to double-click the icon, the application should perform the default menu item of the shortcut menu.

In the Microsoft Windows environment, 0x8000 to 0xBFFF messages are reserved, and applications can define custom messages.

For details on the message processing, please refer to the next section.

Source code and implementation

This article is called CTRAYICON in this article, this class is derived from the ccmdtarget (or COBJECT) class, which has the following member variables and member functions:

// trayicon.h

// ctrayicon Command Target

Class CTRAYICON: PUBLIC CCMDTARGET

{

PUBLIC:

Notifyicondata M_NID; // Notifyicondata structure, your icon is used.

Bool m_ICONEXIST; / / Sign, look at the icon is existing

CWND * m_notificationWnd; // Accept the window of the callback message, there is no need to often AFXGETMAINWND

PUBLIC:

CWND * getNotificationWnd () const; // Get m_notificationWnd

Bool setNotificationWnd (CWND * PNOTIFYWND); // Setting (Change) M_NotificationWnd

CTRAYICON (); // Constructor

Virtual ~ ctrayicon (); // Destructor

Bool Createicon (CWND * PNOTIFYWND, UINT UID, HICON HICON,

LPSTR LPSZTIP, UINT CALLBACKMESSAGE); // Generate icon Bool deleteicon () on the taskbar (); // Delete icons on the taskbar

Virtual LRESULT Onnotify (WPARAM WPARAM, LPARAM LPARAM); // Message Response Function

Bool settiptext (uint nid); // Set (Change) Tip String

BOOL settiptext (lpctstr lpsztip); // Setting (Change) Tip String

BOOL CHANGEICON (HICON HICON); / / Change Icon

BOOL CHANGEICON (UINT NID); // Change Icon

BOOL CHANGEICON (LPCTSTR LPSZICONNAME); // Changing icon

Bool ChangeStandardIn (LPCTSTR LPSZICONNAME); / / Change to Standard Icon

......

}

Below is the definition of member functions:

// TRAYICON.CPP

// ctrayicon

CTRAYICON :: CTRAYICON ()

{//Initialization parameters

m_ICONEXIST = FALSE;

m_notificationWnd = null;

MEMSET (& M_NID, 0, SIZEOF (M_NID));

m_nid.cbsize = sizeof (m_nid); // This parameter will not change

}

CTRAYICON :: ~ ctrayicon ()

{

IF (m_ICONEXIST)

Deleteicon (); // Delete icon

}

Bool CTRAYICON :: Createicon (CWND * PNOTIFYWND, UINT UID, HICON HICON,

LPSTR LPSZTIP, UINT CALLBACKMESSAGE

{

/ / Determine the window that accepts the callback message is effective

Assert (PNOTIFYWND && :: Iswindow (pNOTIFYWND-> GetsafehWnd ()));

Assert (CallbackMessage> = WM_USER); / / Determine that the callback message does not conflict

Assert (_TCSLEN (LPSZTIP) <= 64); // Tip string cannot exceed 64 characters

m_notificationWnd = pNotifyWnd; // Get m_notificationWnd

// Set the Notifyicondata structure

m_nid.hwnd = pNotifyWnd-> getsafehwnd ();

m_nid.uid = uid;

m_nid.hicon = hic;

M_nid.uflags = nif_message | NIF_ICON | NIF_TIP;

M_nid.ucallbackMessage = CallbackMessage;

/ / Set the prompt string of the Notifyicondata structure

IF (LPSZTIP)

LSTRCPYN (m_nid.sztip, lpsztip, sizeof (m_nid.sztip);

Else

m_nid.sztip [0] = '/ 0';

// Display icon

m_ICONEXIST = shell_notifyicon (nim_add, & m_nid);

Return M_ICONEXIST;

}

Bool ctrayicon :: deleteicon ()

{// Remove icon

IF (! m_ICONEXIST)

Return False;

m_ICONEXIST = FALSE;

Return shell_notifyicon (nim_delete, & m_nid);

}

Lresult CTRAYICON :: ONNOTIFY (WPARAM WPARAM, LPARAM LPARAM)

{// Process the message if the icon is returned (WPARAM! = M_NID.UID) // If the message is not the icon, quickly return

Return 0L;

// Prepare shortcut menu

CMenu Menu;

If (! menu.loadmenu (idr_popup)) / / You must determine that there is IDR_POPUP menu in the resource

Return 0;

Cmenu * psubmenu = menu.getsubmenu (0); // Get the submenu of IDR_POPUP

IF (! psubmenu)

Return 0;

IF (lparam == wm_rbuttonup)

{// Right-click pop-up shortcut menu

/ / Set the first menu item to the default

:: setmenudefaultItem (psubmenu-> m_hmenu, 0, true);

Cpoint Pos;

GetCursorpos (& POS);

// Display and track menu

m_notificationWnd-> setForegroundWindow ();

Psubmenu-> TRACKPOPUPMENU (TPM_RightAlign | TPM_LEFTBUTTON

| TPM_RightButton, POS.X, POS.Y, M_NNOTIFICATIONWND, NULL;

}

ELSE IF (lparam == wm_lbuttondown)

{// Left click recovery window

m_notificationWnd-> showwindow (sw_show); // Recovery Window

M_NotificationWnd-> setForegroundWindow (); // Place in front

}

Else IF (lparam == wm_lbuttondblclk)

{// Left button Double-click to perform the default menu item

m_notificationWnd-> SendMessage (WM_Command,

Psubmenu-> getMenuItemID (0), 0);

}

Return 1L;

}

Bool CTRAYICON :: SetTipText (LPCTSTR LPSZTIP)

{// Setting the prompt text

IF (! m_ICONEXIST)

Return False;

_TCSCPY (m_nid.sztip, lpsztip);

M_nid.uflags | = nif_tip;

Return shell_notifyicon (nim_modify, & m_nid);

}

Bool ctrayicon :: settiptext (uint NID)

{// Setting the prompt text

CString Sztip;

Verify (Sztip.LoadString (NID));

Return Settiptext (SZTIP);

}

Bool CTRAYICON :: Changeicon (HiCon Hicon)

{// Changing icon

IF (! m_ICONEXIST)

Return False;

m_nid.hicon = hic;

m_nid.uflags | = nif_icon;

Return shell_notifyicon (nim_modify, & m_nid);

}

Bool CTRAYICON :: Changeicon (Uint Nid)

{// Changing icon

Hicon Hicon = AFXGetApp () -> Loadicon (NID);

Return Changeicon (HICON);

}

Bool CTRAYICON :: Changeicon (LPCTSTR LPSZICONNAME)

{// Changing icon

Hicon Hicon = AFXGetApp () -> Loadicon (LPSZICONNAME);

Return Changeicon (HICON);

}

Bool CTRAYICON :: ChangeStr LpsziconName {// Changk to Standard Icon

Hicon Hicon = AFXGETAPP () -> LoadStandardIn (LPSZICONNAME);

Return Changeicon (HICON);

}

Bool ctrayicon :: setNotificationWnd (CWND * PNOTIFYWND)

{// Set the window that accepts the callback message

IF (! m_ICONEXIST)

Return False;

/ / Determine the window is effective

Assert (PNOTIFYWND && :: Iswindow (pNOTIFYWND-> GetsafehWnd ()));

M_NotificationWnd = PNOTIFYWND;

m_nid.hwnd = pNotifyWnd-> getsafehwnd ();

M_nid.uflags | = nif_message;

Return shell_notifyicon (nim_modify, & m_nid);

}

CWND * CTRAYICON :: getNotificationWnd () const

{// Return to the window to receive the callback message

Return M_NotificationWnd;

}

Three points to add:

Additional instructions on using callback messages:

First, add your own message code in mainfrm.cpp;

// mainfrm.cpp: Implementation of the cmainframe class

//

#define mywm_iconnotify wm_user 10 // Define your own message code

The second step adds a message mapping and function declaration, and for custom messages cannot be added to the ClassWizard to add a message mapping and can only be added manually.

// mainfrm.cpp: Implementation of the cmainframe class

Begin_MESSAGE_MAP (CMAINFRAME, CMDIFRAMEWND)

// {{AFX_MSG_MAP (CMAINFRAME)

// Other message mappings

......

//}} AFX_MSG_MAP

ON_MESSAGE (WM_ICONNNOTIFY, ONNOTIFY)

END_MESSAGE_MAP ()

And add a function declaration in the header file

// mainfrm.h

AFX_MSG LRESULT ONNOTIFY (WPARAM WPARAM, LPARAM LPARAM);

The third step adds the message processing function definition

Lresult CMAINFRAME :: Onnotify (WPARAM WPARAM, LPARAM LPARAM)

{

Return Trayicon.onnotify (WPARAM, LPARAM); // Call the processing function of the CTRAYICON class

}

How to hide buttons on the taskbar

The following two methods can be used:

1. Use the WS_EX_TOOLWINDOW window pattern in the CreateWindowEx function (the opposite if you want to make sure the application generates a button on the taskbar, you can use the WS_EX_APPWindow window model). The Problem with this is what The Window Decorations Are As for a Small Floating Toolbar, Which isn't normally what's wanted.

2. Generate an empty hidden Top-Level window and make it as a parent window of the visual window.

3. Use the sw_hide style to call the showWindow () function in the initInstance () function of the application.

// pmaInframe-> showwindow (m_ncmdshow);

PMAINFRAME-> ShowWindow (sw_hide); PMAINFRAME-> UpdateWindow ();

How to animate icons on the taskbar

Add the following two functions in the TRAYICON class:

Bool CTRAYICON :: Setanimateicons (Hicon * Hicon, Uint Number)

{/ / Set animation icon

Assert (Number> = 2); // Icon must be more than two

Assert (hic); // Icon must not be empty

m_animateicons = new hicon [number];

CopyMemory (M_animateicons, Hicon, Number * SizeOf (HICON);

m_animateiconsnumber = number;

Return True;

}

Bool CTRAYICON :: Animate (uint index)

{/ Animation TRAYICON

UINT I = index% m_animateiconsnumber;

Return Changeicon (M_animateicons [i]);

}

How to add the corresponding menu and functions in the application

Void CMAINFRAME :: onmenuaniMate ()

{// animation TRAYICON, set icon and timer

Settimer (1, 500, null);

Hicon Hicon [3];

Hicon [0] = AFXGetApp () -> loadicon (idR_mainframe);

Hicon [1] = AFXGETAPP () -> Loadicon (iDR_MyType);

Hicon [2] = AFXGetApp () -> LoadStandardIn (IDI_HAND);

Trayicon.setanimateicons (Hicon, 3);

}

Void CMAINFRAME :: ONTIMER (uint nidevent)

{/ Animation TRAYICON

Uint static i;

i = 1;

Trayicon.Animate (i);

CmdiframeWnd :: ONTIMER (Nidevent);

}

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

New Post(0)