Posts

Creating tray application and popup context menu in Windows

If you want to add your application in system tray and popup a context menu #include<windows.h> #include "resource.h" HINSTANCE g_hInstance; const int WM_EX_MESSAGE = (WM_APP + 1); BOOL TrayIcon(HWND hWnd, HICON hIcon, DWORD msg) { NOTIFYICONDATA nid; ZeroMemory(&nid,sizeof(NOTIFYICONDATA)); nid.cbSize = sizeof(NOTIFYICONDATA); nid.hWnd = hWnd; nid.uID = IDI_ICON2; nid.uFlags = NIF_TIP | NIF_ICON | NIF_MESSAGE; nid.uCallbackMessage = WM_EX_MESSAGE; nid.hIcon = hIcon; lstrcpyn(nid.szTip, __TEXT("This is my tray app!"), 64); return Shell_NotifyIcon(msg,&nid); } void ContextMenu(HWND hwnd) { HMENU hmenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_MENU2)); HMENU hmnuPopup = GetSubMenu(hmenu,0); SetMenuDefaultItem(hmnuPopup,IDOK,FALSE); POINT pt; GetCursorPos(&pt); TrackPopupMenu(hmnuPopup,TPM_LEFTALIGN,pt.x,pt.y,0,hwnd,NULL); DestroyMenu(hmnuPopup); DestroyMenu(hmenu); } BOOL CALLBACK APP_DlgProc(HWND hDlg, UINT uiMsg,...

Adding shortcut menu in the Windows Start menu

In relation to the previous article the simplest way to add a shortcut menu in the Windows Start menu is to add the shortcut menu into the similar folder below.. C:\Documents and Settings\earl\Start Menu\Programs\ That's it! =)

Adding desktop shortcut programmtically in Windows

I was tinkering of how I can put a desktop shortcut of my program and I thought this is one of those things I still have to learn. So, I wander into the internet to find a most simple example and read lots of articles and discussion and somehow I find it daunting to read all those codes where all I want is to create a simple desktop shortcut. After a lot of reading and experimentation I finally have the code below. #include<windows.h> #include<shlobj.h>  int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) {     createShortcut();     return 0; } void createShortcut() {     HRESULT hr;     ::CoInitialize(NULL); IShellLink *pShellLink; hr = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL,IID_IShellLink, (void**)&pShellLink); hr = pShellLink->SetPath("C:\\MyProjects\\OCXTest\\Debug\\OCXTest.exe");  // Path to the object we are referring to ...

How to asynchronously update GUI in Java

Image
There are times an application needs to display feedback to the user that certain activity is going on like status of a progress perhaps. In Java an application cannot update the GUI asynchronously while you're also processing using the same thread. What normally happens is that your GUI will be updated only after the processing have completed which is not we want. So, to do what we want we need to use only the main application thread for GUI update while we will create a separate thread for processing in that way the GUI is updated asynchronously. Sample Code: public class ProgressDialog extends javax.swing.JDialog{     public ProgressDialog(java.awt.Frame parent, boolean modal) {         super(parent, modal);         initComponents();         worker = new Worker();     } private void StartActionPerformed(java.awt.event.ActionEvent evt) { ...

Using javah to create JNI interfaces

There are times that you will need to interface with the underlying operating system APIs or you have a dynamic link libraries that needs to be called from Java. It can be done using JNI using the javah tool. First, you will need to create a java class that expose interfaces that you will implement in C. ////////////////////////////////// MyJavaInterfaces.java //////////////////////////////////// public class MyJavaInterfaces{     public MyJavaInterfaces(){     }     public native int MyFunction();     static{         System.loadLibrary("MyNativeInterfaces");     } } ////////////////////////////////////////////////////////////////////////////////////////////////////////// The declaration, public native int MyFunction(); is the native function that needs to be implemented in C. The call, System.loadLibrary("MyNativeInterfaces"); is the library that you will cre...

How to display a control in JPanel during runtime

I had a problem in a Java project about not able to display a JTree into a JPanel. The JPanel is created during design time. The problem is JTree is created during runtime and adding the JTree into the JPanel will not display the control when executing the application. The code below won't work: JTree treeObject = new JTree(); panelObject.add(treeObject); There is one more thing to do to make it work by adding a group layout. //---------- START JTree treeObject = new JTree(); panelObject.add(treeObject); javax.swing.GroupLayout myLayout = new javax.swing.GroupLayout(panelObject); panelObject.setLayout(myLayout); myLayout.setHorizontalLayout(       myLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)      .addComponent(treeObject, panelObject.getWidth(), panelObject.getWidth(), panelObject.getWidth()) ); myLayout.setVerticalGroup(       myLayout.createParallelGroup(javax....

How to add and delete contents in menu.lst in Linux

I was creating a RPM for Linux in one of my projects. I needed to add contents to the menu.lst which is found in /boot/grub/. The operating system that I'm using is Suse. There are two problems that needs to be done. 1. Need to add content to the file 2. Remove the content from the file. I use the bash script to do both. To add content to the file:  cat /boot/grub/menu.tmp >> /boot/grub/menu.lst  wc -l /boot/grub/menu.lst > /boot/grub/menu.lstcount The first line is to append the content in menu.tmp to  menu.lst. The second is count the number of lines in the menu.lst and save the result to menu.lstcount. To delete the content of the file: num=`grep -o ".*[0-9]" /boot/grub/menu.lstcount` let begin=$num-3 mv /boot/grub/menu.lst /boot/grub/menu.orig sed "$begin,$num"d /boot/grub/menu.orig > /boot/grub/menu.lst The first line is retrieve the line number stored in menu.lstcount Second line is to assign the difference of the line numb...