A Sample Code

Discussion in 'Win32' started by AhmedHan, Oct 17, 2005.

  1. AhmedHan

    AhmedHan New Member

    Joined:
    Oct 11, 2005
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.stirve.com
    I want to donate my code and ask some questions...
    Code:
    #define _WIN32_IE 0x0601
    #define _WIN32_WINNT 0x0501
    #include <windows.h>
    #include <shellapi.h> //for system tray and balloon tool-tips
    #include <stdlib.h> //for atoi() and _itoa()
    #include <string.h>
    HWND hbSetTimer, hbReset;
    HWND hsT, hsHM, hsMS;
    HWND heH, heM, heS;
    HWND hbShutdown, hbRestart, hbLogOff, hbForce;
    HWND hWnd;
    const UINT NID_ICOID = 500; //Randomly choosen
    const UINT NID_CBMSG = 600; //Randomly choosen
    NOTIFYICONDATA nid;
    BOOLEAN bShutdown=FALSE, bRestart=FALSE, bForce=FALSE;
    BOOLEAN bAutoRun = FALSE;
    char cWindowState;
    DWORD dwEditStyle;
    INT iSecond, iMinute, iHour;
    char szHour[3]="00", szMinute[3]="00", szSecond[3]="00";
    UINT_PTR IDT_TIMER;
    LONG Height = 125;
    LONG Width = 225;
    char szClassName[] = "WindowsApp";
    LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
    VOID CALLBACK TimerProcedure (HWND, UINT, UINT, DWORD);
    int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
    {
    if (strlen(lpszArgument) == 9)
    {
    szHour[0] = lpszArgument[3];
    szHour[1] = lpszArgument[4];
    szMinute[0] = lpszArgument[5];
    szMinute[1] = lpszArgument[6];
    szSecond[0] = lpszArgument[7];
    szSecond[1] = lpszArgument[8];
    bAutoRun = TRUE;
    }
     
    	MSG messages;
    	IDT_TIMER=100; //A randomly choosen number. Actually I don't know how to use timers :)
    	dwEditStyle = (DWORD) (GWL_EXSTYLE, WS_VISIBLE | WS_CHILD | ES_LEFT | ES_NUMBER | WS_TABSTOP);
     
    	WNDCLASSEX WindowClass;
    WindowClass.cbSize = sizeof (WNDCLASSEX);
    WindowClass.style = CS_DBLCLKS; 
    WindowClass.lpfnWndProc = WindowProcedure;
    WindowClass.cbClsExtra = 0;
    WindowClass.cbWndExtra = 0;
    WindowClass.hInstance = hThisInstance;
    WindowClass.hIcon = (HICON) LoadImage(NULL, "Icon.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
    WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
    WindowClass.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    WindowClass.lpszMenuName = NULL;
    WindowClass.lpszClassName = szClassName;
    WindowClass.hIconSm = (HICON) LoadImage(NULL, "SmIcon.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
     
    	if (!RegisterClassEx (&WindowClass)) return 0;
    	hWnd = CreateWindowEx (
    		 0,
    		 szClassName,
    		 "Shutdown v1.0",
    		 WS_MINIMIZEBOX | WS_SYSMENU,
    		 CW_USEDEFAULT,
    		 CW_USEDEFAULT,
    		 Width,
    		 Height,
    		 HWND_DESKTOP,
    		 NULL,
    		 hThisInstance,
    		 NULL);
    	ShowWindow(hWnd, nFunsterStil);
     
    LONG tLeft=5;
    LONG tTop=5;
    LONG tWidth=135;
    LONG tHeight=16;
    	hsT = CreateWindowEx( 
    	 0,
    	 "STATIC",
    	 "Remaining Time =",
    	 WS_VISIBLE | WS_CHILD | SS_LEFT | WS_TABSTOP,
    	 tLeft,
    	 tTop,
    	 tWidth,
    	 tHeight,
    	 hWnd,
    	 NULL,
    	 (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
    	 NULL);
     
    LONG hLeft = tLeft + tWidth;
    LONG hTop=tTop;
    LONG hWidth=20;
    heH = CreateWindowEx( 
    	 0,
    	 "EDIT",
    	 szHour,
    	 dwEditStyle,
    	 hLeft,
    	 hTop,
    	 hWidth,
    	 tHeight,
    	 hWnd,
    	 NULL,
    	 (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
    	 NULL);
     
    LONG hmLeft = hLeft + hWidth;
    LONG hmTop=tTop;
    LONG hmWidth=5;
    	hsHM = CreateWindowEx( 
    	 0,
    	 "STATIC",
    	 ":",
    	 WS_VISIBLE | WS_CHILD | SS_LEFT | WS_TABSTOP,
    	 hmLeft,
    	 hmTop,
    	 hmWidth,
    	 tHeight,
    	 hWnd,
    	 NULL,
    	 (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
    	 NULL);
     
    LONG mLeft = hmLeft + hmWidth;
    LONG mTop=tTop;
    LONG mWidth=20;
    heM = CreateWindowEx( 
    	 0,
    	 "EDIT",
    	 szMinute,
    	 dwEditStyle,
    	 mLeft,
    	 mTop,
    	 mWidth,
    	 tHeight,
    	 hWnd,
    	 NULL,
    	 (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
    	 NULL);
     
    LONG msLeft = mLeft + mWidth;
    LONG msTop=tTop;
    LONG msWidth=5;
    	hsMS = CreateWindowEx( 
    	 0,
    	 "STATIC",
    	 ":",
    	 WS_VISIBLE | WS_CHILD | SS_LEFT | WS_TABSTOP,
    	 msLeft,
    	 msTop,
    	 msWidth,
    	 tHeight,
    	 hWnd,
    	 NULL,
    	 (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
    	 NULL);
     
    LONG sLeft = msLeft + msWidth;
    LONG sTop=tTop;
    LONG sWidth=20;
    heS = CreateWindowEx( 
    	 0,
    	 "EDIT",
    	 szSecond,
    	 dwEditStyle,
    	 sLeft,
    	 sTop,
    	 sWidth,
    	 tHeight,
    	 hWnd,
    	 NULL,
    	 (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
    	 NULL);
     
    	LONG rsLeft=tLeft-7;
    LONG rsTop = tTop + tHeight + 5;
    LONG rsWidth=81;
    LONG rsHeight=16;
    	hbShutdown = CreateWindowEx( 
    	 0,
    	 "BUTTON",
    	 "Shutdown",
    	 WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP,
    	 rsLeft,
    	 rsTop,
    	 rsWidth,
    	 rsHeight,
    	 hWnd,
    	 NULL,
    	 (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
    	 NULL);
    if (!bAutoRun) SendMessage(hbShutdown, BM_SETCHECK, BST_CHECKED, (LPARAM) NULL);
     
    	LONG rrLeft = tLeft + rsWidth;
    LONG rrTop = rsTop;
    LONG rrWidth=66;
    LONG rrHeight=rsHeight;
    	hbRestart = CreateWindowEx( 
    	 0,
    	 "BUTTON",
    	 "Restart",
    	 WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP,
    	 rrLeft,
    	 rrTop,
    	 rrWidth,
    	 rrHeight,
    	 hWnd,
    	 NULL,
    	 (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
    	 NULL);
     
    	LONG rlLeft = rrLeft + rrWidth;
    LONG rlTop = rsTop;
    LONG rlWidth=75;
    LONG rlHeight=rsHeight;
    	hbLogOff = CreateWindowEx( 
    	 0,
    	 "BUTTON",
    	 "Log-Off",
    	 WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON | WS_TABSTOP,
    	 rlLeft,
    	 rlTop,
    	 rlWidth,
    	 rlHeight,
    	 hWnd,
    	 NULL,
    	 (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
    	 NULL);
     
    	LONG cfLeft=tLeft;
    LONG cfTop = rrTop + rrHeight + 5;
    LONG cfWidth=200;
    LONG cfHeight=rsHeight;
    	hbForce = CreateWindowEx( 
    	 0,
    	 "BUTTON",
    	 "Force (Not recommended!)",
    	 WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX | WS_TABSTOP,
    	 cfLeft,
    	 cfTop,
    	 cfWidth,
    	 cfHeight,
    	 hWnd,
    	 NULL,
    	 (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
    	 NULL);
     
    	LONG stLeft=tLeft;
    LONG stTop = cfTop + cfHeight + 5;
    LONG stWidth=100;
    LONG stHeight=25;
    	hbSetTimer = CreateWindowEx( 
    	 0,
    	 "BUTTON",
    	 "Set Timer",
    	 WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | WS_TABSTOP,
    	 stLeft,
    	 stTop,
    	 stWidth,
    	 stHeight,
    	 hWnd,
    	 NULL,
    	 (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
    	 NULL);
     
    LONG brLeft = stLeft + stWidth + 5;
    LONG brTop=stTop;
    LONG brWidth=100;
    LONG brHeight=25;
    	hbReset = CreateWindowEx( 
    	 0,
    	 "BUTTON",
    	 "Reset",
    	 WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | WS_TABSTOP,
    	 brLeft,
    	 brTop,
    	 brWidth,
    	 brHeight,
    	 hWnd,
    	 NULL,
    	 (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
    	 NULL);
     
    nid.cbSize	= sizeof(NOTIFYICONDATA);
    nid.hWnd	= hWnd;
    nid.uID	 = NID_ICOID;
    nid.uFlags	= NIF_MESSAGE | NIF_TIP | NIF_INFO | NIF_ICON;
    nid.uCallbackMessage = NID_CBMSG;
    nid.hIcon	= (HICON) LoadImage(NULL, "TrayIcon.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
    lstrcpy(nid.szTip, "Double-Click To Maximize.");
    nid.dwState	= NIS_SHAREDICON;
    nid.dwStateMask = NIS_SHAREDICON;
    lstrcpy(nid.szInfo, "Doublelick to maximize...");
    nid.uTimeout = 10;
    lstrcpy(nid.szInfoTitle,"Shutdown v1.0");
    nid.dwInfoFlags = NIF_INFO;
    //nid.guidItem = NULL;
     
    if (bAutoRun)
    {
    char cRequest = lpszArgument[0];
    char cForce = lpszArgument[1];
    cWindowState = lpszArgument[2];
     
    switch (cRequest)
    {
    case 'S':
    	lstrcpy(nid.szInfo, "Shutdown will proceed after the countdown.");
    	lstrcpy(nid.szInfoTitle,"Shutdown in progress!");
    	bShutdown = TRUE;
    	SendMessage(hbShutdown, BM_SETCHECK, BST_CHECKED, (LPARAM) NULL);
    	break;
    case 'R':
    	lstrcpy(nid.szInfo, "Restart will proceed after the countdown.");
    	lstrcpy(nid.szInfoTitle,"Restart in progress!");
    	bRestart = TRUE;
    	SendMessage(hbRestart, BM_SETCHECK, BST_CHECKED, (LPARAM) NULL);
    	break;
    default:
    	lstrcpy(nid.szInfo, "Log-Off will proceed after the countdown.");
    	lstrcpy(nid.szInfoTitle,"Log-Off in progress!");
    	SendMessage(hbLogOff, BM_SETCHECK, BST_CHECKED, (LPARAM) NULL);
    	break;
    }
     
    switch (cWindowState)
    {
    case 'N':
    	//Do nothing
    	break;
    case 'M':
    	ShowWindow(hWnd, SW_MINIMIZE);
    	break;
    case 'S':
    	Shell_NotifyIcon(NIM_ADD, &nid);
    	ShowWindow(hWnd, SW_HIDE);
    	break;
    }
     
    if (cForce == 'F')
    {
    bForce=TRUE;
    SendMessage(hbForce, BM_SETCHECK, BST_CHECKED, (LPARAM) NULL);
    }
     
    SendMessage(heH, WM_GETTEXT, (WPARAM) 3, (LPARAM) szHour);
    SendMessage(heM, WM_GETTEXT, (WPARAM) 3, (LPARAM) szMinute);
    SendMessage(heS, WM_GETTEXT, (WPARAM) 3, (LPARAM) szSecond);
    iHour =atoi(szHour);
    iMinute=atoi(szMinute);
    iSecond=atoi(szSecond);
     
    SetTimer(hWnd, IDT_TIMER, 1000, (TIMERPROC) TimerProcedure);
    EnableWindow(hbSetTimer, FALSE);
    }
     
    	while (GetMessage (&messages, NULL, 0, 0))
    	{
    		TranslateMessage(&messages);
    		DispatchMessage(&messages);
    	}
    	return messages.wParam;
    }
    LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch (message)
    	{
    		case WM_COMMAND:
    if (HIWORD(wParam) == BN_CLICKED)
    			{
    	if ((HANDLE) lParam == (HANDLE) hbSetTimer)
    	 {
    	 SendMessage(heH, WM_GETTEXT, (WPARAM) 3, (LPARAM) szHour);
    	 SendMessage(heM, WM_GETTEXT, (WPARAM) 3, (LPARAM) szMinute);
    	 SendMessage(heS, WM_GETTEXT, (WPARAM) 3, (LPARAM) szSecond);
    	 iHour =atoi(szHour);
    	 iMinute=atoi(szMinute);
    	 iSecond=atoi(szSecond);
     
    	 SetTimer(hWnd, IDT_TIMER, 1000, (TIMERPROC) TimerProcedure);
    	 EnableWindow(hbSetTimer, FALSE);
    	 }
    	else if ((HANDLE) lParam == (HANDLE) hbReset)
    	 {
    	 KillTimer(hWnd, IDT_TIMER);
    	 EnableWindow(hbSetTimer, TRUE);
    	 }
    }
    			break;
    		case WM_SYSCOMMAND:
    DefWindowProc (hWnd, message, wParam, lParam);
    if (wParam == SC_MINIMIZE)
    {
    	Shell_NotifyIcon(NIM_ADD, &nid);
    	ShowWindow(hWnd, SW_HIDE); //Wild goose chase!
    	ShowWindow(hWnd, SW_SHOWNORMAL); //Sorry! Couldn't find a better way...
    	ShowWindow(hWnd, SW_HIDE);
    } 
    break;
    		case WM_DESTROY:
    KillTimer(hWnd, IDT_TIMER);
    Shell_NotifyIcon(NIM_DELETE, &nid);
    			PostQuitMessage (0);
    			break;
    case NID_CBMSG:
    if ((wParam == NID_ICOID) && (lParam == WM_LBUTTONDBLCLK))
    {
    	ShowWindow(hWnd, SW_RESTORE);
    	Shell_NotifyIcon(NIM_DELETE, &nid);
    }
    break;
    default:
    			return DefWindowProc (hWnd, message, wParam, lParam);
    	}
     
    	return 0;
    }
    VOID CALLBACK TimerProcedure(HWND hWnd, UINT message, UINT idTimer, DWORD dwTime)
    {
    if ((iHour==0) && (iMinute==0) && (iSecond==0))
    {
    KillTimer(hWnd, IDT_TIMER);
     
    if (!bAutoRun)
    {
    LRESULT lResult;
     
    lResult = SendMessage(hbShutdown, BM_GETCHECK, (WPARAM) NULL, (LPARAM) NULL);
    if (lResult == BST_CHECKED) bShutdown=TRUE; else bShutdown=FALSE;
     
    lResult = SendMessage(hbRestart, BM_GETCHECK, (WPARAM) NULL, (LPARAM) NULL);
    if (lResult == BST_CHECKED) bRestart=TRUE; else bRestart=FALSE;
     
    lResult = SendMessage(hbForce,	BM_GETCHECK, (WPARAM) NULL, (LPARAM) NULL);
    if (lResult == BST_CHECKED) bForce=TRUE; else bForce=FALSE;
    }
     
    UINT uFlags;
    DWORD dwReason = 0;
    if (bShutdown)
    {
    if (bForce)
    {
    	uFlags = EWX_POWEROFF | EWX_FORCE;
    }
    else
    {
    	uFlags = EWX_POWEROFF;
    }
    }
    else if (bRestart)
    {
    if (bForce)
    {
    	uFlags = EWX_REBOOT | EWX_FORCE;
    }
    else
    {
    	uFlags = EWX_REBOOT;
    }
    }
    else
    {
    if (bForce)
    {
    	uFlags = EWX_LOGOFF | EWX_FORCE;
    }
    else
    {
    	uFlags = EWX_LOGOFF;
    }
    }
     
    ExitWindowsEx((UINT) uFlags, (DWORD) dwReason);
     
    return; 
    }
     
    iSecond--;
     
    if (iSecond<0)
    {
    iSecond=59;
    iMinute--;
    }
     
    if (iMinute<0)
    {
    iMinute=59;
    if (iHour>0) iHour--;
    }
     
    _itoa(iHour, szHour, 10);
    _itoa(iMinute, szMinute, 10);
    _itoa(iSecond, szSecond, 10);
     
    if (strlen(szHour) == 1)
    {
    szHour[1] = szHour[0];
    szHour[0] = '0';
    szHour[2] = (char) NULL;
    }
    if (strlen(szMinute) == 1)
    {
    szMinute[1] = szMinute[0];
    szMinute[0] = '0';
    szMinute[2] = (char) NULL;
    }
    if (strlen(szSecond) == 1)
    {
    szSecond[1] = szSecond[0];
    szSecond[0] = '0';
    szSecond[2] = (char) NULL;
    }
     
    SendMessage(heH, WM_SETTEXT, (WPARAM) NULL, (LPARAM) szHour);
    SendMessage(heM, WM_SETTEXT, (WPARAM) NULL, (LPARAM) szMinute);
    SendMessage(heS, WM_SETTEXT, (WPARAM) NULL, (LPARAM) szSecond);
    }
    This program simply shuts down, restarts or logs-off the Windows after a time period chosen by user. May be run with parameters (Automatic mode). It is written in DevCPP.
    This is also my first Win32 program. I studied Win32 programming by writing this program.

    My first problem is... Edit controls doesn't look like normal edit controls. I mean they look flat. I want to make a 3D appearance.

    The second, I couldn't add resource files to the .exe file. Program uses icons from external files. I know it is done by using .rc files, but HOW?

    A attached the entire project. You may download and have look.
     

    Attached Files:

  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You need to give the property of the EDIT control to have a border. You are using dwEditStyle Add to the styles WS_BORDER.
    You dont need to embed the rc in the exe but the compiler should do this when creating the exe. The rc should always link to the external file.
     
  3. AhmedHan

    AhmedHan New Member

    Joined:
    Oct 11, 2005
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.stirve.com
    1) With WS_BORDER it really looks bad. There just appears a black rectangle enclosing the Edit control.
    2) I said I knew that compiler does that linking operation. But what I don't know is how to prepare an rc file and add it to the project. Whatever, forget about it... Just an answer for (1) is enough for me.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You need to make the Apperance as 3D its by default as Flat
     

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