Topwnd a key to the implementation of the window, share the source code

Source: Internet
Author: User

Topwnd for a small and practical green software, you can top most of the window, a key to the top, a button to cancel!

Paste on:

I've been working on this gadget, curious today, to decide how it's going to work inside.

Online search, did not find the source of this gadget.

Helpless had had to step by step of their own to achieve it.

Finally, the mfc+ global Hook successfully imitated a similar program.

  1. Preparatory work
    • Visual Studio 2012
    • Building MFC Project
    • Drag several controls
    • Reference article: http://blog.csdn.net/friendan/article/details/12168273
  2. Global Hooks
    • Source Directory, MouseHook.dll
    • In the DLL program, customize a message as follows:

      • #define WM_MYMSG Wm_user + 305//custom message for communication with the main program
    • In the hook installation function, save the main program window handle, the code is as follows:
      • BOOL WINAPI Starthookmouse (HWND hwnd)  {      G_hwnd=hWnd;      Hhkmouse=::setwindowshookex (Wh_mouse_ll,lowlevelmouseproc,g_hinstdll,0);       if (hhkmouse==NULL)      {          return  FALSE;      }        Else       {          return  TRUE;      }  }  
    • In the mouse hook response function, we will capture the message, sent to the main program, the code is as follows:
      • LRESULT CALLBACK Lowlevelmouseproc (intNCode,//Hook CodeWPARAM WPARAM,//message identifierLPARAM LPARAM//Mouse coordinates                           )  {        //when you have a mouse message, send it to the main program    if(g_hwnd!=null&&ncode==hc_action)      {:: SendMessage (G_hwnd,wm_mymsg,wparam,lparam); }      returnCallNextHookEx (Hhkmouse,ncode,wparam,lparam); }  
  3. Handling the communication between the main program and the hook DLL
    • Define a message in the main program header file as defined by the DLL
      • #define WM_MYMSG Wm_user + 305
    • Declaring a message-handler function in a header file
      • afx_msg LRESULT onmymsg (WPARAM wparam,lparam LPARAM);
    • Write a message map in the main file
      • On_message (WM_MYMSG,ONMYMSG)
    • Implement the message handler function, implement the top and restore functions within the function
      • LRESULT chookmousetestdlg::onmymsg (WPARAM wparam,lparam LPARAM) {}

  4. Master Program Implementation Control
    • To open a connection function to a DLL
      • 1     //Load Hook DLL2G_hinstdll=loadlibrary (_t ("MouseHook.dll"));3     if(g_hinstdll==NULL)4     {5AfxMessageBox (_t ("No MouseHook.dll"));6         return;7     }8 9typedef BOOL (CALLBACK *starthookmouse) (HWND hwnd); Ten Starthookmouse Starthook; One  AStarthook= (starthookmouse):: GetProcAddress (G_hinstdll,"Starthookmouse"); -     if(starthook==NULL) -     { theAfxMessageBox (_t ("func starthookmouse not found!")); -         return; -     } -  +     //Pass the handle of this program to the hook DLL for communication -     if(Starthook ( This-m_hwnd)) +     { A Setdlgitemtext (idc_state,state1); at     } -     Else -     { - Setdlgitemtext (idc_state,state2); -}

    • To close a connection function to a DLL
      • typedef VOID (CALLBACK *stophookmouse) ();    Stophookmouse Stophook;     = LoadLibrary (_t ("MouseHook.dll"));     = (Stophookmouse):: GetProcAddress (G_hinstdll,"stophookmouse");     if (Stophook = = NULL) {        setdlgitemtext (idc_state,state2);    }    Stophook ();     if (G_hinstdll! = NULL) {        :: FreeLibrary (G_hinstdll);    }

    • sticky button, restore button
      • Call Open DLL function
      • Change the bool variable openorclose make the ONMYMSG function handle the top or the recovery separately
      • 1 //Start a sticky2BOOL Openorclose =FALSE;3 voidCtopwnddlg::onbnclickedopen ()4 {5 Openhook ();6GetDlgItem (Id_open)EnableWindow (FALSE);7Openorclose =TRUE;8 }9 Ten //Resume pinned One voidctopwnddlg::onbnclickedclose () A { - Openhook (); -GetDlgItem (Id_close)EnableWindow (FALSE); theOpenorclose =FALSE; -}

    • True functionality in message-handling functions
      • Determine if the received mouse event is a click
      • Handle to the program where the mouse is located when clicked
      • Call the SetWindowPos function to make the window sticky. Parameter 1: Handle to the target program window, Parameter 2: (hwnd_topmost) top parameter, parameter 7: Does not change the window size and position
      • Processing ends, closing the connection to the DLL
      • 1 //handle the information of the hooks, the implementation of sticky and recovery2 LRESULT ctopwnddlg::onmymsg (WPARAM wparam,lparam LPARAM) {3 CString str;4     Staticpmsllhookstruct mousehookstruct;5MouseHookStruct =(pmsllhookstruct) LParam;6     if(WParam = =wm_lbuttondown) {7HWND Hwnd=::windowfrompoint (mousehookstruct->PT);8 TCHAR Title[max_path];9 :: GetWindowText (Hwnd,title,max_path);Ten         if(openorclose) { One             //hwnd_topmost window pinned Swp_nomove | Swp_nosize does not change window size and position A:: SetWindowPos (HWnd, Hwnd_topmost,0,0,0,0, Swp_nomove |swp_nosize); -GetDlgItem (Id_open)EnableWindow (TRUE); -Str. Format (_t ("%s%s"), state3,title); the setdlgitemtext (idc_state, str); -         } -         Else{ -:: SetWindowPos (HWnd, Hwnd_notopmost,0,0,0,0, Swp_nomove |swp_nosize); +GetDlgItem (Id_close)EnableWindow (TRUE); -Str. Format (_t ("%s%s"), state4,title); + Setdlgitemtext (IDC_STATE,STR); A         } at Closehook (); -     } -     return 1; -}
  5. Source: C + + manual implementation of Topwnd source code
  6. Thank you for watching ~

Topwnd a key to the implementation of the window, share the source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.