TrayIcon Menue
-
Moin, ich weiß nicht warum, aber mein Menue wird nicht erzeugt

hoffe ihr steigt da durch.
// Main.cpp #include <windows.h> #include <windowsx.h> #include <shellapi.h> #include <iostream.h> #include "CpuLoad.h" #include "MinimizeToTray.h" struct sAuslastung { int CpuEins; int CpuZwei; int CpuDrei; int CpuVier; int Ram; }Auslastung; int main() { HWND hwnd = GetConsoleHwnd(); MSG messages; minimize(hwnd); while(GetMessage (&messages, NULL, 0, 0)) { TranslateMessage(&messages); DispatchMessage(&messages); CpuUpdate(); Sleep(1000); Auslastung.CpuEins = CpuLoad(0); Auslastung.CpuZwei = CpuLoad(1); cout << Auslastung.CpuZwei; // Auslastung.CpuDrei = CpuLoad(2); // Auslastung.CpuVier = CpuLoad(3); MEMORYSTATUS lpBuffer; lpBuffer.dwLength = sizeof(MEMORYSTATUS); GlobalMemoryStatus (&lpBuffer); Auslastung.Ram = lpBuffer.dwMemoryLoad; //Ausgabe } CpuExit(); return 0; } // Ende Main.cpp// MinimizeToTray.h #ifndef _TRAY_H_ #define _TRAY_H_ #include <windowsx.h> #include <windows.h> #include <shellapi.h> HWND GetConsoleHwnd(); int minimize(HWND hwnd); LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); #define IDI_ICON 0 #define IDI_TRAYICON 1 #define IDM_TRAYEXIT 2 #define IDM_TRAYABOUT 3 #endif //Ende header//MinimizeToTray.cpp #include "MinimizeToTray.h" HWND GetConsoleHwnd() { #define MY_BUFSIZE 1024 // Buffer size for console window titles. HWND hwnd; // This is what is returned to the caller. char pszNewWindowTitle[MY_BUFSIZE]; char pszOldWindowTitle[MY_BUFSIZE]; GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE); wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId()); SetConsoleTitle(pszNewWindowTitle); Sleep(40); hwnd=FindWindow(NULL, pszNewWindowTitle); SetConsoleTitle(pszOldWindowTitle); return(hwnd); } int 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_MESSAGE | NIF_ICON //we're adding an icon | NIF_TIP; nid.uCallbackMessage = (WM_USER + 1); //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 "CPU.ico", 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. LR_LOADFROMFILE); strcpy (nid.szTip, "Dies ist die In-Tray .EXE der Projektdatei"); Shell_NotifyIcon(NIM_ADD, &nid); //ShowWindow(hwnd, SW_HIDE); return 0; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { POINT pt; switch(message) { case (WM_USER + 1): { if (wParam != IDI_TRAYICON) break; //Falsches ICON if (lParam == WM_LBUTTONUP) //Left Button UP { //BallonTip break; } else if (lParam == WM_RBUTTONUP) //Right Button UP { SetForegroundWindow(hWnd); HMENU TrayMenu = NULL; TrayMenu = CreatePopupMenu(); AppendMenu(TrayMenu, MF_STRING, IDM_TRAYEXIT, "Exit"); AppendMenu(TrayMenu, MF_STRING, IDM_TRAYABOUT, "About"); GetCursorPos(&pt); UINT clicked = TrackPopupMenu(TrayMenu, TPM_RETURNCMD | TPM_NONOTIFY /*don't send me WM_COMMAND messages about this window, instead return the identifier of the clicked menu item*/, pt.x, pt.y, 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) PostQuitMessage(0); else if (clicked == IDM_TRAYABOUT) MessageBox(hWnd, "Tray Example: Demonstrates minimizing a window to the tray.", "About Tray Example", MB_OK | MB_ICONINFORMATION); } break; } } return DefWindowProc(hWnd, message, wParam, lParam); } //Ende MinimizeToTray.cppIch hab einfach mal fast alles hier reingezogen. Mein Problem ist, dass ich kein Menue erzeugen kann. Ich glaube dass der fehler in der Callback funktion liegt. Aber vllt. liege ich auch falsch.
Hoffe mir kann einer helfenmfg
CMW
-
Deine WndProc wird doch gar nicht benutzt! Woher glabst Du denn, dass die WndProc einen Bezug zu Deinem System bekommt?
Dein grndsätzlicher Fehler: Du hast eine Consolen Applikation gebaut und keine Windows Anwendung!
Und versuche sagr nicht: Du kannst ein Consolen Fenster nicht subclassen.
-
d.h. wenn ich das ganze mit Borland Trubo c++ als VCL anwendung schreibe, funktioniert der code?
2. Frage wie schreibe ich dafür die messagemap?danke schonmal für die antwort
-
Ich kann Dir nur sagen, dass Du eine Windows-UI Applikation brauchst, wie da sin VS geht kann ich Dir sagen!
Ich habe von so einem Kram wie "Borland" keine Ahnung und ich will es auch nicht wissen...
