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.

alok 27Sep2004 12:44

Re: My Project:ShutDown Timer ver 1.0
 
May be you are right.
actually i don't have any graphics Software installed in my Computer.
could you do me the favour,please make one for me and send it to me.

i will include that in next release,in which i will include functionality of network.
i.e. Network ShutdownTimer
thanks

coderzone 27Sep2004 12:49

Re: My Project:ShutDown Timer ver 1.0
 
Will do that when I become a bit free.

alok 27Sep2004 13:09

Re: My Project:ShutDown Timer ver 1.0
 
Thanks for same

coderzone 27Sep2004 16:46

Re: My Project:ShutDown Timer ver 1.0
 
1 Attachment(s)
Shutdown timer image.

alok 27Sep2004 18:05

Re: My Project:ShutDown Timer ver 1.0
 
thanks CoderZone,
But little Problem,i never like to Publisize My NAme with project except in about Column.
It's Better if you provide me the Image Without Any Name (as i already told you i have no Graphic s/w loaded in my comp)or Could You Please Chose another Name for Project. thanks.

ps:Can i know your real Name please,if You Don't Mind

coderzone 27Sep2004 18:32

Re: My Project:ShutDown Timer ver 1.0
 
You have the Paint loaded in Windows I guess and just remove the name from there but if you are unable to do that will do that later and will give you.

Quote:

Can i know your real Name please,if You Don't Mind
Why you need to know my reall name though?

alok 27Sep2004 19:21

Re: My Project:ShutDown Timer ver 1.0
 
May i guess your name CoderZone if you don't mind , you are..................Shabbir Bimani.sorry for revealing you identity.

And About Paint ,actually my paint got corrupted so i think,you can understand my situation.
thanks

coderzone 28Sep2004 09:55

Re: My Project:ShutDown Timer ver 1.0
 
Quote:

May i guess your name CoderZone if you don't mind , you are..................Shabbir Bimani.sorry for revealing you identity.
Not at all thats not correct I am his brother (Yahoo chat friend). Yes as I am not an Adobe expert the image has been done by me and not sure why you made that guess.

I have edited the image and uploaded it.

alok 28Sep2004 09:59

Re: My Project:ShutDown Timer ver 1.0
 
Yeah OK,
Thanks for Image,Sorry If i hurt your feeling.Actually for me programming is game of guess and i apply same thing in my life too.

thanks for all help you and your brother provided to Programmer Community to meet

coderzone 28Sep2004 10:05

Re: My Project:ShutDown Timer ver 1.0
 
Quote:

Actually for me programming is game of guess and i apply same thing in my life too.
For me programming is just opposite to guess. I never make a guess in programming but yes in hacking its guess.

alok 29Oct2004 15:16

Re: My Project:ShutDown Timer ver 1.0
 
Hi CoderZone,
I am still waiting for Image,that you have to made for me.anyway happy Navratri and Dussera to u also

shabbir 29Oct2004 15:32

Re: My Project:ShutDown Timer ver 1.0
 
Hello Alok

I see you post saying you are waiting for the image

Quote:

Originally Posted by coderzone
I have edited the image and uploaded it.

and after this post I see

Quote:

Originally Posted by alok
Yeah OK,
Thanks for Image,

So I guess he has uploaded the image and I see the image in his posts also and kindly refer to that.

Thanks
Shabbir Bhimani

alok 30Oct2004 09:55

Re: My Project:ShutDown Timer ver 1.0
 
as i told coderzone that day i have no Graphics S/w Loaded in my computer and if i strech this image ,i think it become Blurr.
that why i asked codezone to give me new one.

Thanks

coderzone 31Oct2004 04:06

Re: My Project:ShutDown Timer ver 1.0
 
»I am still waiting for Image,that you have to made for me.«
I edited the very particular post and uploaded the same there and told you that its done.

»anyway happy Navratri and Dussera to u also«
Same to you.

»as i told coderzone that day i have no Graphics S/w Loaded in my computer«
You told me this when I had your name in the image and told me to edit and remove it as you dont like that.

»and if i strech this image ,i think it become Blurr.«
I dont remember anything such and also checked my Private message box also. If thats the case do let me know whats the size in pixels you want the image to be or else either it will be large or small so resizing will distort the image.


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