GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rating: Thread Rating: 3 votes, 5.00 average.
  #1  
Old 20-Jun-2006, 01:40
lordfuoco lordfuoco is offline
New Member
 
Join Date: Jun 2006
Posts: 5
lordfuoco is on a distinguished road
Question

Help with a complex program


First of all: forgive my bad school english, i'm an italian computer maniac

I've spent a lot of time searching for what i need in this forum, but i study onli c and not c++ so I haven't reached any good result compiling my hostic program.
In this time i'm doing a work stage in a big company, and I MUST complete a work given to me, i'll try to write a program that start an w32 windows that call a dos script (.bat) that perform the program that the company need, i can't modify the real program (wich is a java program) because I haven't access to te source code, but my work is to find the better way to start this java program, initialy the start of this java was handled by a dos script, but the dos shel where the script run is horrible and the user who use the program tend to close it. So first i've try a VBscript which make the dos shel invisible, but now the company ask me to create a .exe wich after is start and after the dos script that handle the java is call, go to the task bar e show the icon of the program.

forgive my horrible english i'm shure that the source code will explain you better than my words, for reason of company policy i've removed all the text where there is link to the company.


I use dev c++ and during the compiling of the program i had thsi error: the tray icon dosen't show and i need to locate the program and the other script in a remote folder, then i call the program wiht a start script in every machine in the company lan, the company use windows xp and 2003 server


tks a lot!!!

MAIN.cpp
CPP / C++ / C Code:
#include <windows.h>
#include "tray.h"

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "TrayMinimizerClass__";

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
system("scritp_dos.bat");                
                    
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon(NULL, IDI_WINLOGO);
    wincl.hIconSm = LoadIcon(NULL,IDI_WINLOGO);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Progrma_title",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           300,                 /* The programs width */
           60,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if ( (message == TASKBARCREATED) && (minimized) ) //have to do this out here because it won't let me use TASKBARCREATED in a case statement (something about not allowed to use it in a constant-expression)
    {
        minimize(hwnd);
        return 0;
    }
    
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
            CreateWindowEx(0, "BUTTON", "Click to minimize the program", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_TEXT | BS_CENTER | BS_VCENTER, 1, 1, 300, 25, hwnd, (HMENU)IDC_MINIMIZE, GetModuleHandle(NULL), NULL);
            break;
        case WM_COMMAND:
            if ( (HIWORD(wParam) == BN_CLICKED) && (LOWORD(wParam) == IDC_MINIMIZE) )
            {
                if (minimized) restore(hwnd);
                else minimize(hwnd);
            }
            break;
        case MSG_MINTRAYICON:
        {
            if (wParam != IDI_TRAYICON) break; //if it's not the icon we planted, then go away
            if (lParam == WM_LBUTTONUP) //the mouse button has been released. It's time to re-maximize our window. This is basically done using the reverse of the minimizing process. MAKE SURE you look for WM_LBUTTONUP and not WM_LBUTTONDOWN. Most tray icons handle the release, so if your icon disappears on mousedown, the next icon will get notified of a mouseup. This is not what you want to happen.
            {
                restore(hwnd);
            }
            
            else if (lParam == WM_RBUTTONUP) //time to display a menu.
            {
                HMENU myMenu = NULL;
                myMenu = CreatePopupMenu(); //create our menu. You'll want to error-check this, because if it fails the next few functions may produce segmentation faults, and your menu won't work.
                
                //IDM_TRAYEXIT, IDM_TRAYABOUT, and IDM_TRAYHELP are #defined constants.
                AppendMenu(myMenu, MF_STRING, IDM_TRAYEXIT, "Exit");
                AppendMenu(myMenu, MF_STRING, IDM_TRAYABOUT, "About");
                
                DWORD mp = GetMessagePos(); //get the position of the mouse at the time the icon was clicked (or, at least, the time this message was generated).
                
                SetForegroundWindow(hwnd); //even though the window is hidden, we must set it to the foreground window because of popup-menu peculiarities. See the Remarks section of the MSDN page for TrackPopupMenu.
                UINT clicked = TrackPopupMenu(myMenu, TPM_RETURNCMD | TPM_NONOTIFY /*don't send me WM_COMMAND messages about this window, instead return the identifier of the clicked menu item*/, GET_X_LPARAM(mp), GET_Y_LPARAM(mp), 0, hwnd, NULL); //display the menu. you MUST #include <windowsx.h> to use those two macros.
                SendMessage(hwnd, WM_NULL, 0, 0); //send benign message to window to make sure the menu goes away.
                if (clicked == IDM_TRAYEXIT) SendMessage(hwnd, WM_DESTROY, 0, 0);
                else if (clicked == IDM_TRAYHELP) MessageBox(hwnd, "Cliccare il pulsante per minimizzare nel sistem tray", "Help di Programma", MB_OK | MB_ICONINFORMATION);
                else if (clicked == IDM_TRAYABOUT) MessageBox(hwnd, "BIG BIG COmpany", "PRGRAM", MB_OK | MB_ICONINFORMATION);
            }
        }
            break;
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

void minimize(HWND hwnd)
{
    NOTIFYICONDATA nid = { 0 };
    nid.cbSize = sizeof(NOTIFYICONDATA); //this helps the OS determine stuff. (I have no idea, but it is necessary.
    nid.hWnd = hwnd; //the hWnd and uID members allow the OS to uniquely identify your icon. One window (the hWnd) can have more than one icon, as long as they have unique uIDs.
    nid.uID = IDI_TRAYICON; //sorry, had forgotten this in my original example. but without, the function probably wouldn't work
    nid.uFlags = //some flags that determine the tray's behavior:
        NIF_ICON //we're adding an icon
        | NIF_MESSAGE //we want the tray to send a message to the window identified by hWnd when something happens to our icon (see uCallbackMesage member below).
        | NIF_TIP; //our icon has a tooltip.
    nid.uCallbackMessage = MSG_MINTRAYICON; //this message must be handled in hwnd's window procedure. more info below.
    nid.hIcon = (HICON)LoadImage( //load up the icon:
        GetModuleHandle(NULL), //get the HINSTANCE to this program
        MAKEINTRESOURCE(IDI_ICON), //grab the icon out of our resource file
        IMAGE_ICON, //tells the versatile LoadImage function that we are loading an icon
        16, 16, //x and y values. we want a 16x16-pixel icon for the tray.
        0); //no flags necessary. these flags specify special behavior, such as loading the icon from a file instead of a resource. see source list below for MSDN docs on LoadImage.
    strcpy(nid.szTip, "NAMEPRG"); //this string cannot be longer than 64 characters including the NULL terminator (which is added by default to string literals).
    //There are some more members of the NOTIFYICONDATA struct that are for advanced features we aren't using. See sources below for MSDN docs if you want to use balloon tips (only Win2000/XP).
    Shell_NotifyIcon(NIM_ADD, &nid);
    ShowWindow(hwnd, SW_HIDE);
    minimized = true;
}

void restore(HWND hwnd)
{
    NOTIFYICONDATA nid = { 0 };
    nid.cbSize = sizeof(NOTIFYICONDATA);
    nid.hWnd = hwnd;
    nid.uID = IDI_TRAYICON;
    Shell_NotifyIcon(NIM_DELETE, &nid);
    ShowWindow(hwnd, SW_SHOW);
    minimized = false;
}


TRAY.h
CPP / C++ / C Code:
//tray.h

#ifndef _TRAY_H_ //inclusion guard
#define _TRAY_H_


#include <windows.h>
#include <windowsx.h>
#include <shellapi.h>

#ifndef RC_INVOKED //variable definitions and function prototypes just confuse the resource compiler

void minimize(HWND hwnd);
void restore(HWND hwnd);

bool minimized = false;
const int TASKBARCREATED = RegisterWindowMessage("TaskbarCreated");

#endif // 8: #ifndef RC_INVOKED

#define IDI_ICON        0
#define IDC_MINIMIZE    1
#define IDI_TRAYICON    2
#define IDM_TRAYEXIT    3
#define IDM_TRAYHELP    4
#define IDM_TRAYABOUT   5
#define MSG_MINTRAYICON (WM_USER+0)


#endif // 1: #ifndef _TRAY_H_
Last edited by LuciWiz : 20-Jun-2006 at 01:59. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 23-Jun-2006, 00:40
lordfuoco lordfuoco is offline
New Member
 
Join Date: Jun 2006
Posts: 5
lordfuoco is on a distinguished road

Re: Help with a complex program


57 Views and no one could help me... i hope the cause is not my bad bad english...
  #3  
Old 23-Jun-2006, 01:05
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,230
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Help with a complex program


Quote:
Originally Posted by lordfuoco
57 Views and no one could help me... i hope the cause is not my bad bad english...
It's probably because the 50 or so people that looked at the thread have no idea what to do -- they aren't knowledgable in Windows APIs and Windowing. The right person just hasn't come along yet.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #4  
Old 23-Jun-2006, 03:11
lordfuoco lordfuoco is offline
New Member
 
Join Date: Jun 2006
Posts: 5
lordfuoco is on a distinguished road

Re: Help with a complex program


Quote:
Originally Posted by WaltP
It's probably because the 50 or so people that looked at the thread have no idea what to do -- they aren't knowledgable in Windows APIs and Windowing. The right person just hasn't come along yet.

thanks for your word, i was thinking that my english was incomprensibile

i hope thath the right person came soon!!
  #5  
Old 23-Jun-2006, 13:00
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Help with a complex program


You need a resource file. Your comments talk about grabbing the icon out of the resource file, but you have none. All you need is:

resource.rc
CPP / C++ / C Code:
#include "tray.h"

IDI_ICON ICON "icon.ico"

Of course, icon.ico needs to be a file in your project's directory (if the filename is different, change icon.ico to whatever.ico). This icon file will be compiled into your program .exe, so you don't need to include it with the program file.
In Dev-C++, just add this resource.rc to the project and it should work (it did when I tried ).
  #6  
Old 24-Jun-2006, 06:03
lordfuoco lordfuoco is offline
New Member
 
Join Date: Jun 2006
Posts: 5
lordfuoco is on a distinguished road

Re: Help with a complex program


Quote:
Originally Posted by ubergeek
You need a resource file. Your comments talk about grabbing the icon out of the resource file, but you have none. All you need is:

resource.rc
CPP / C++ / C Code:
#include "tray.h"

IDI_ICON ICON "icon.ico"

Of course, icon.ico needs to be a file in your project's directory (if the filename is different, change icon.ico to whatever.ico). This icon file will be compiled into your program .exe, so you don't need to include it with the program file.
In Dev-C++, just add this resource.rc to the project and it should work (it did when I tried ).

tks a lot! when i rrtun to work i try!!! for the moment i'll spent my week end in relaxing
 

Recent GIDBlogGoing to Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read particular memory location ? realnapster C Programming Language 10 10-May-2006 09:11
complex number raised to a complex Darth Predator CPP / C++ Forum 6 06-Nov-2005 20:20
Type casts ? kai85 CPP / C++ Forum 12 23-Jun-2005 12:04
complex number tinzi CPP / C++ Forum 2 01-Jun-2004 14:47
Will pay money for someone to write a semi complex program bstan Computer Programming Advertisements & Offers 0 23-Feb-2004 03:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:54.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.