My Project:ShutDown Timer ver 1.0

Discussion in 'Win32' started by alok, Sep 25, 2004.

?

Do u Like The Software??

  1. Yes

    66.7%
  2. No

    0 vote(s)
    0.0%
  3. Somewhat

    33.3%
Multiple votes are allowed.
  1. alok

    alok New Member

    Joined:
    Jul 24, 2004
    Messages:
    127
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg
    Home Page:
    http://www.thatsalok.com
    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:
    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:
    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:
    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:
    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:
    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
     

    Attached Files:

  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Nice project and It looks good that people like you love to contibute in this community.

    Thanks
    Shabbir Bhimani
     
  3. alok

    alok New Member

    Joined:
    Jul 24, 2004
    Messages:
    127
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg
    Home Page:
    http://www.thatsalok.com
    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
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    39
    Trophy Points:
    28
    Nice application but I guess you can go for better looks though functionality is perfectly fine on my machine.
     
  6. alok

    alok New Member

    Joined:
    Jul 24, 2004
    Messages:
    127
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg
    Home Page:
    http://www.thatsalok.com
    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
     
  7. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    39
    Trophy Points:
    28
    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.
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    My pleasure
     
  9. alok

    alok New Member

    Joined:
    Jul 24, 2004
    Messages:
    127
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg
    Home Page:
    http://www.thatsalok.com
    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
     
  10. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    39
    Trophy Points:
    28
    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.
     
  11. alok

    alok New Member

    Joined:
    Jul 24, 2004
    Messages:
    127
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg
    Home Page:
    http://www.thatsalok.com
    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
     
  12. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    39
    Trophy Points:
    28
    Will do that when I become a bit free.
     
  13. alok

    alok New Member

    Joined:
    Jul 24, 2004
    Messages:
    127
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg
    Home Page:
    http://www.thatsalok.com
    Thanks for same
     
  14. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    39
    Trophy Points:
    28
    Shutdown timer image.
     

    Attached Files:

    • alok.jpg
      alok.jpg
      File size:
      15.6 KB
      Views:
      642
    Last edited: Sep 28, 2004
  15. alok

    alok New Member

    Joined:
    Jul 24, 2004
    Messages:
    127
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg
    Home Page:
    http://www.thatsalok.com
    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
     
  16. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    39
    Trophy Points:
    28
    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.

    Why you need to know my reall name though?
     
  17. alok

    alok New Member

    Joined:
    Jul 24, 2004
    Messages:
    127
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg
    Home Page:
    http://www.thatsalok.com
    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
     
  18. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    39
    Trophy Points:
    28
    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.
     
  19. alok

    alok New Member

    Joined:
    Jul 24, 2004
    Messages:
    127
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg
    Home Page:
    http://www.thatsalok.com
    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
     
  20. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    39
    Trophy Points:
    28
    For me programming is just opposite to guess. I never make a guess in programming but yes in hacking its guess.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice