Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Win32 (http://www.go4expert.com/articles/win32-tutorials/)
-   -   My Project:ShutDown Timer ver 1.0 (http://www.go4expert.com/articles/projectshutdown-timer-ver-10-t179/)

alok 25Sep2004 18:05

My Project:ShutDown Timer ver 1.0
 
1 Attachment(s)
The sample project using MFC is intended to either shutdown, logoff, or restart the computer at a given time or immediately. This is a Visual C++ Project using the concept of Win32 APIs with MFC. The Shutdown Timer application also uses System Tray Interface to handle it with menu options.

API for ShutDown:
The ExitWindowEx is the API which can be used in Visual C++ for shutting down a workstation. There is one more API for this purpose named InitiateSystemShutdown, but that API works only on Window 2000 and above. The ExitWindowEx API works directly in Win9x/ME, but in Windows multi-user systems, it requires a special privilege before execution.

Here is the code for enabling that privilege for Windows multi-user platforms in Visual C++:
Code: CPP

HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES |
       TOKEN_QUERY, &hToken); // Get the LUID for shutdown privilege.             
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;   
// Get shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges. 
if (GetLastError() != ERROR_SUCCESS)   
   MessageBox("AdjustTokenPrivileges enable failed.");

Now, ExitWindowsEx has two parameters. The function declaration in <Windows.h> looks like:
Code: CPP

BOOL ExitWindowsEx(
  UINT uFlags,
  DWORD dwReason
);

And for uFlag, we have four options:

1. EWX_LOGOFF: Logoff the current user.
2. EWX_RESTART: Simply restart the computer.
3. EWX_SHUTDOWN: Shut down but 'AT' style.
4. EWX_POWEROFF: Shut down but 'ATX' style.

I think the above Win32 APIs are very simple, right?

System Tray Icon :

The next logical step to be understood is how to use the system tray icon and the way it handles the messages. First, I will show you the code, after that, I will describe to you the required details about the System Tray Icon handling.
Code: CPP

NOTIFYICONDATA m_niData;
//handling the ICON at System Tray Icon
m_niData.cbSize=sizeof(NOTIFYICONDATA);
m_niData.hIcon=m_Icon;
m_niData.hWnd=this->m_hWnd;
sprintf(m_niData.szTip,"Shut Down Alarm :My Father Software inc @ 2003");
m_niData.uCallbackMessage=WM_USER+75;
m_niData.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;
m_niData.uID=ID_ICONDATA;

Shell_NotifyIcon(NIM_ADD,&m_niData);

Now, here you see one new variable of type NOTIFYICONDATA Structure.
Code: CPP

BOOL Shell_NotifyIcon(     
    DWORD dwMessage,
    PNOTIFYICONDATA lpdata);

The NOTIFYICONDATA structure according to MSDN 'Contains information that the system needs to process taskbar status area messages'. The variables are:

1. cbSize: this variable is created to provide compatibility with other versions of NotifyIconData.
2. hIcon: icon for the system tray.
3. hWnd: handle to window which will handle the system tray icon message.
4. szTip: show tip when mouse hovers over icon.
5. CallbackMessage: application-defined message identifier. The system uses this identifier to send notifications to the window identified in hWnd. These notifications are sent when a mouse event occurs in the bounding rectangle of the icon, or when the icon is selected or activated with the keyboard.
6. uFlag: this flag notifies the system which of the above variable to use or not.
7. uID: any unique ID for Icon.

Now, Shell_NotifyIcon adds this icon to system tray. It takes three parameters as arguments:

1. NIM_ADD: add icon to system tray.
2. NIM_DELETE: delete icon from system tray.
3. NIM_MODIFY: modify the system tray icon (for e.g., when you connect to Yahoo! chat using Yahoo! messenger, the icon becomes active, and when you logoff, the icon becomes inactive).

Handling the system tray message:

Message Mapping: This notification is handled using MFC user defined messages .

ON_MESSAGE(WM_USER+75,OnSystemBarMessage)

Now, the code to handle message in MFC library ( Visual C++ ) is :
Code: CPP

void DlgShutDown::OnSystemBarMessage(WPARAM wParam, LPARAM lParam)
{
    Switch(lParam){
        case WM_LBUTTONDOWN:
            this->ShowWindow(SW_RESTORE);
            this- >ShowWindow(SW_SHOW);
            bMiniShow=FALSE; break;
        case WM_RBUTTONDOWN:
        {
            CMenu mnu;
            mnu.LoadMenu(IDR_MENU1);
            //CMenu *PopUpMenu;
            PopUpMenu=mnu.GetSubMenu(0);
            SetForegroundWindow();
            CPoint pt;
            GetCursorPos(&pt);
            PopUpMenu->TrackPopupMenu(TPM_RIGHTALIGN,pt.x,pt.y,this);
            //this->ShowWindow(SW_MINIMIZE);
            break;
        }
    }
}

This code demonstrates "Mouse Right Button Click" by which I show the popup menu when user clicks on system tray icon.

The Attchment contain the source of Project and a Executable

shabbir 26Sep2004 09:09

Re: My Project:ShutDown Timer ver 1.0
 
Nice project and It looks good that people like you love to contibute in this community.

Thanks
Shabbir Bhimani

alok 26Sep2004 12:39

Re: My Project:ShutDown Timer ver 1.0
 
please Shabbir,
Mention Not and thanks
iT's every programmer duty to help there fellow programmer and that the main reason behind creation Go4Expert.com.

note:if time permit me,i will submit some more projectmade by me

shabbir 27Sep2004 11:13

Re: My Project:ShutDown Timer ver 1.0
 
Hey Alok please see this for better looks that you can give to your posts as good as this thread.
http://go4expert.com/forums/misc.php?do=bbcode

Thanks
Shabbir Bhimani

coderzone 27Sep2004 11:17

Re: My Project:ShutDown Timer ver 1.0
 
Nice application but I guess you can go for better looks though functionality is perfectly fine on my machine.

alok 27Sep2004 11:49

Re: My Project:ShutDown Timer ver 1.0
 
Yeah.
Thanks Shabbir for guiding me to VbCode,i will try to use them while submiting my post.thanks
and
for Coderzone:-
i will try enhance look of Apllication,could you give me any suggestion for improving it.

thanks Shabbir Amd CoderZone
Alok

coderzone 27Sep2004 12:02

Re: My Project:ShutDown Timer ver 1.0
 
Quote:

could you give me any suggestion for improving it.
Some suggestions are

Better Image, Left, Right border, Always on top can be given depending on user choice. Properties, Time on same horizontal level to mention some.

shabbir 27Sep2004 12:03

Re: My Project:ShutDown Timer ver 1.0
 
Quote:

Thanks Shabbir for guiding me to VbCode
My pleasure

alok 27Sep2004 12:13

Re: My Project:ShutDown Timer ver 1.0
 
Quote:

Originally Posted by coderzone
Some suggestions are

Better Image, Left, Right border, Always on top can be given depending on user choice. Properties, Time on same horizontal level to mention some.

About Better Image-->
i will try improve Image quality,but adverse effect is that it will effect Application Size.

And Other Idea are really Good,i will try improve it

coderzone 27Sep2004 12:21

Re: My Project:ShutDown Timer ver 1.0
 
Quote:

i will try improve Image quality,but adverse effect is that it will effect Application Size.
I am not saying increase size of image but showing in unstretchable will also do but some kb increase in size will not matter much.


All times are GMT +5.5. The time now is 03:18.