#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Posts
    30
    Rep Power
    7

    Is C the right language to create an app that pins to a window


    I like to learn by taking a project I'm interested in, then trying to build it. I've already learned PHP albeit I've still a hell of a lot to learn and along the way I've got the basics in html, javascript, json, PDO etc

    My next project i want to build an application. One thing i find difficult is to get clear information for what language is ideal for certain tasks. I know its pretty general but certain languages seem to have strengths in some areas for instance i can parse a file in php but for large files it would be silly to use PHP, i would guess that something like C would be better, i have found it hard to find that type of information, if there is a resource that has this i would appreciate a link.

    So to my project. I want to develop a toolbar that can interact with an application. Lets take Skype as an example, i want to develop a toolbar that can pin onto the skype titlebar so if i move Skype to a new window it always remains visible. The toolbar will have a couple of icons, each will do something simple like copy the text and send the text in lets say Json format to a url which will process it or take a screenshot, it wont need to actually interact with Skype as such but instead will interact with the clipboard to get information from the app manually.

    Obviously i can process the json and handle it with PHP but what would be the main languages that could help me develop the toolbar itself that can pin to a window.

    One other that is important but isn't essential is i would like to be able to have it pin to other applications, so MSN Messenger, MS Word etc i should be able to drop a marker/pin on it and it holds onto the app by reading the window title and maybe the x,y position if the user wants to always pin it in a certain location or something to that affect.

    Can you recommend a language that would be efficient for this, or to put this better, if you were designing this and were equally proficient in all languages which would you choose. My preference would be Phyton because ive heard its easy to grasp but the advice i have gotten was although Python might do the job it wouldnt be the best language for this kind of job. I'm assuming C might be more suitable but thats why im asking you guys in here

    I imagine this is the type of question that comes up again and again so ill probably get bashed for asking it and i have tried to be specific in exactly what i want to do but i really do appreciate any help or advice you can give me.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    259
    Rep Power
    128
    If I were doing it, I'd use whichever language I felt comfortable with that was able to leverage the win32 api. In my case, I'd like to present a short example in C that should help you get started.

    The code simply creates a dialog box with a button and an edit control. When you click the button, all mouse input is captured. This allows you to then click on another window, with the coordinates of the mouse-click forwarded to your program. I then use these coordinates to find out which window was clicked. After doing this, I attempt to get the window text, before displaying it in the edit box.

    In your case, I'd imagine that once you have the HWND of the target window, you could query its position on screen, before moving your window to the appropriate place.

    As for dealing with the case when the target window is moed or closed, the easiest (& nastiest) thing that comes to mind immediately is to set a timer. Each time the timer fires, you could simply use your stored tgt HWND to check the position or existence of the window before taking the appropriate measure.

    Anyway, that's enough chatter - here's the code:

    main.cpp
    Code:
    #include <windows.h>
    #include <commctrl.h>
    #include <stdio.h>
    #include "resource.h"
    
    HINSTANCE hInst;
    bool isCaptured = false;
    
    void onBtn(HWND hwnd)
    {
        SetCapture(hwnd);
        isCaptured = true;
    }
    
    BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch(uMsg)
        {
        case WM_INITDIALOG:
        {
        }
        return TRUE;
    
        case WM_LBUTTONDOWN:
            if (isCaptured == false)
            {
                SetDlgItemText(hwndDlg, IDC_EDIT1, "Not captured");
            }
    
            else
            {
                POINT mousePos;
                HWND tgt;
                char *wndText;
                int wndTextLen;
    
                ReleaseCapture();
                isCaptured = false;
    
                mousePos = (POINT) {LOWORD(lParam), HIWORD(lParam)};
                ClientToScreen(hwndDlg, &mousePos);
                tgt = WindowFromPoint(mousePos);
                wndTextLen = GetWindowTextLength(tgt);
                wndText = new char[wndTextLen+2];
                GetWindowText(tgt, wndText, wndTextLen+1);
                SetDlgItemText(hwndDlg, IDC_EDIT1, wndText);
                delete wndText;
    
             //   SetForegroundWindow(hwndDlg);
            }
    
            break;
    
        case WM_CLOSE:
        {
            EndDialog(hwndDlg, 0);
        }
        return TRUE;
    
        case WM_COMMAND:
        {
            switch(LOWORD(wParam))
            {
            case IDC_BUTTON1:
                onBtn(hwndDlg);
                break;
            }
        }
        return TRUE;
        }
        return FALSE;
    }
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
        hInst=hInstance;
        InitCommonControls();
        return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
    }
    resource.h
    Code:
    #ifndef IDC_STATIC
    #define IDC_STATIC (-1)
    #endif
    
    #define DLG_MAIN                                100
    #define IDC_BUTTON1                             1000
    #define IDC_EDIT1                               1001
    resource.rc
    Code:
    // Generated by ResEdit 1.5.11
    // Copyright (C) 2006-2012
    // http://www.resedit.net
    
    #include <windows.h>
    #include <commctrl.h>
    #include <richedit.h>
    #include "resource.h"
    
    
    
    
    //
    // Dialog resources
    //
    LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
    DLG_MAIN DIALOG 0, 0, 278, 56
    STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
    EXSTYLE WS_EX_TOPMOST
    CAPTION "Dialog"
    FONT 8, "Ms Shell Dlg"
    {
        PUSHBUTTON      "Button1", IDC_BUTTON1, 7, 7, 50, 14
        EDITTEXT        IDC_EDIT1, 38, 35, 233, 14, ES_AUTOHSCROLL
    }
    
    
    
    //
    // Manifest resources
    //
    LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
    1                  RT_MANIFEST    ".\\manifest.xml"
    manifest.xml
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
      <dependency>
        <dependentAssembly>
          <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
        </dependentAssembly>
      </dependency>
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
          <requestedPrivileges>
            <requestedExecutionLevel
              level="asInvoker"
              uiAccess="false"/>
          </requestedPrivileges>
        </security>
      </trustInfo>
    </assembly>
    Notes:
    1) I built and ran this with gcc 4.7.? using Code::Blocks as the IDE - you could easily use Visual Studio.
    2) I've also linked the following libraries - gdi32, user32, kernel32, comctl32.
    3) The code isn't always successful at getting the window;s text - I didn't bother to look into why. I don't know if an invalid HWND was being returned from WindowFromPoint, or if the window is simply without text.
    4) If I click the button then click on this post-entry box, I get the text: "" (i.e nothing) If however, I click on click on the empty panel to the right of this panel and the emoticons, I get the text "Dev Shed Forums - Reply to Topic - Google Chrome".

    Have fun!

IMN logo majestic logo threadwatch logo seochat tools logo