1. 程式人生 > >按F1開啟幫助文件

按F1開啟幫助文件

// PowerAssist.cpp : Defines the entry point for the application.
//


#include "stdafx.h"
#include "pbassist.h"
#include "PowerAssist.h"


#define MAX_LOADSTRING 100


// Global Variables:
HINSTANCE hInst;// current instance
TCHAR szTitle[MAX_LOADSTRING];// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];// the main window class name


// Forward declarations of functions included in this code module:
ATOMMyRegisterClass(HINSTANCE hInstance);
BOOLInitInstance(HINSTANCE, int);
LRESULT CALLBACKWndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACKAbout(HWND, UINT, WPARAM, LPARAM);
#include <Richedit.h>
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{

     
    
    UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);


 // TODO: Place code here.
MSG msg;
HACCEL hAccelTable;


// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_POWERASSIST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);


// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}


hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_POWERASSIST));
    
    HANDLE hThread;
    DWORD dwThread;


    hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)   my_HotKey, 0, NULL, &dwThread);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}


return (int) msg.wParam;
}






//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;


wcex.cbSize = sizeof(WNDCLASSEX);


wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc= WndProc;
wcex.cbClsExtra= 0;
wcex.cbWndExtra= 0;
wcex.hInstance= hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_POWERASSIST));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName= MAKEINTRESOURCE(IDC_POWERASSIST);
wcex.lpszClassName= szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));


return RegisterClassEx(&wcex);
}


//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;


   hInst = hInstance; // Store instance handle in our global variable


   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, 400, 300, NULL, NULL, hInstance, NULL);


   if (!hWnd)
   {
      return FALSE;
   }


   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);


   return TRUE;
}


//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND- process the application menu
//  WM_PAINT- Paint the main window
//  WM_DESTROY- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;


switch (message)
{
case WM_COMMAND:
wmId    = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}


// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;


case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}








__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
    DWORD SHIFT_key=0;
    DWORD CTRL_key=0;
    DWORD ALT_key=0;
    DWORD PBIDE = 0 ; 
    if  ((nCode == HC_ACTION) &&   ((wParam == WM_SYSKEYDOWN) ||  (wParam == WM_KEYDOWN)))      
    {
        KBDLLHOOKSTRUCT hooked_key =    *((KBDLLHOOKSTRUCT*)lParam);
        DWORD dwMsg = 1;
        dwMsg += hooked_key.scanCode << 16;
        dwMsg += hooked_key.flags << 24;
        char lpszKeyName[1024] = {0};
        //lpszKeyName[0] = '[';


        int i = GetKeyNameText(dwMsg,   (lpszKeyName+1),0xFF) + 1;
        //lpszKeyName[i] = ']';


        int key = hooked_key.vkCode;


        SHIFT_key = GetAsyncKeyState(VK_SHIFT);
        CTRL_key = GetAsyncKeyState(VK_CONTROL);
        ALT_key = GetAsyncKeyState(VK_MENU);
            
        POINT pt;
        GetCursorPos(&pt);


           
           // DWORD dwProcessId ;
            //GetWindowThreadProcessId(hwnd,&dwProcessId);
           /* GUITHREADINFO  gi;
            memset(&gi,0,sizeof(gi));
            gi.cbSize = sizeof(gi);
            GetGUIThreadInfo(0,&gi);*/
             
            
           /* AttachThreadInput(dwProcessId,::GetCurrentThreadId(),  TRUE);


            POINT pos;
            GetCaretPos(&pos);
            ClientToScreen(hwnd,&pos);*/
            HWND hwnd;
            GUITHREADINFO pg;
            POINT point;//游標位置 
            pg.cbSize=48;
            ::GetGUIThreadInfo(NULL,&pg);
            hwnd=pg.hwndCaret;
            if (pg.hwndCaret)
            {
                point.x=pg.rcCaret.right;
                point.y=pg.rcCaret.bottom;
                ::ClientToScreen(pg.hwndCaret,&point);
            }


 
        GetClassName( hwnd,name,20) ;
        PBIDE = (strcmp(name,"wedit") ==0)?1:0; 


        
        if(PBIDE)
        {
            hInputWindow = hwnd; 


            if(  CTRL_key && key ==74)
            {
                if(!IsWindow(hAssistWindow) )
                {
                    hAssistWindow = CreateAssistWindow(point.x,point.y);
                     return 1;
                }
                 
                if(IsWindowVisible(hAssistWindow))
                    return 1;
            }




        }
       /* else
        { 
            if(hInputWindow !=hwnd)
            { 
            ShowWindow(hPopWindow,SW_HIDE);
            return CallNextHookEx(hKeyboardHook,    nCode,wParam,lParam);
            }
        }*/
         ////現在不做其它功能
         return CallNextHookEx(hKeyboardHook,    nCode,wParam,lParam);


        //輸入視窗已經是顯示狀態下
        //---------------------------------------------


        if(IsWindow( hInputWindow ) && IsWindowVisible(hInputWindow)) 
        {
            if(key ==VK_ESCAPE )
            {
                ShowWindow(hPopWindow,SW_HIDE);
                return 1;
            }
             
            if(key==VK_BACK || 
                key ==VK_SPACE || 
                key ==VK_RETURN ||
                key ==VK_LCONTROL || 
                key ==VK_SHIFT)  {
                ShowWindow(hPopWindow,SW_HIDE);
                return CallNextHookEx(hKeyboardHook,    nCode,wParam,lParam);
            }


            if(key ==VK_UP||
                key ==VK_DOWN||
                key == VK_LEFT||
                key == VK_RIGHT )  
            {
                   
                    return CallNextHookEx(hKeyboardHook,    nCode,wParam,lParam);
            }






            if( key ==VK_CONTROL)
            {
                return CallNextHookEx(hKeyboardHook,    nCode,wParam,lParam);
            }   
            
            if(key ==VK_CAPITAL  )
            {
                 return CallNextHookEx(hKeyboardHook,    nCode,wParam,lParam);
            }




            hInputWindow = hwnd; 


            
        
            
            if(IsWindow(hPopWindow) )
            {
                SetWindowPos(hPopWindow,HWND_TOPMOST,point.x,point.y,0,0,SWP_NOSIZE| SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_DRAWFRAME);
                //ShowWindow(hPopWindow,SW_SHOWNOACTIVATE|SW_SHOW); 
            }
            else
            {
                CreatePopup(point.x,point.y); 
            }
             //MessageBox(GetDesktopWindow(),"ok","k",MB_OK);
            //HWND helpwnd=HtmlHelp(hwnd,"D:\\Program Files\\Sybase\\Help\\pbman125.chm",HH_DISPLAY_INDEX,(DWORD)lptstr) ;


            //return 1 ; 
        }
 
        //printf("lpszKeyName = %s\n",  lpszKeyName );
        //  printf("%s",  lpszKeyName );
    }
    return CallNextHookEx(hKeyboardHook,    nCode,wParam,lParam);
}


DWORD WINAPI my_HotKey(LPVOID lpParm)
{
    HINSTANCE hInstance = GetModuleHandle(NULL);
    if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm); 
    if (!hInstance) return 1;


    hKeyboardHook = SetWindowsHookEx (  WH_KEYBOARD_LL, (HOOKPROC) KeyboardEvent,   hInstance,  NULL    );
    MessageLoop();
    UnhookWindowsHookEx(hKeyboardHook);
    return 0;
}
 


int CreatePopup(int xpos,int ypos)
{
    WNDCLASSEX wcex;


    wcex.cbSize = sizeof(WNDCLASSEX);


    wcex.style= CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc= PopupWindow;
    wcex.cbClsExtra= 0;
    wcex.cbWndExtra= 0;
    wcex.hInstance= hInst;
    wcex.hIcon= NULL;
    wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName= NULL;
    wcex.lpszClassName= "PopWindow";
    wcex.hIconSm= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));


    RegisterClassEx(&wcex); 
    POINT point;
    GetCursorPos(&point);
    hPopWindow = CreateWindowEx(WS_EX_TOOLWINDOW   ,"PopWindow", "程式碼助手", WS_CAPTION|WS_THICKFRAME   ,
        xpos, ypos+ 2, 200, 80, NULL, NULL, hInst, NULL);


    ShowWindow(hPopWindow,SW_SHOWNOACTIVATE);
    return 0 ; 
}


LRESULT WINAPI PopupWindow(HWND hwnd,UINT uMessage,WPARAM wParam,LPARAM lParam)
{
    switch(uMessage)
    {
    case WM_CREATE:
        CreateListBox(hwnd);
        return 1;  
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_SIZE:
    {
        RECT rc ; 
        GetWindowRect(hwnd,&rc);
        MoveWindow(hListBox,0,0,rc.right,rc.bottom,FALSE);
    }
    break;
    default:break;
    }
    return DefWindowProc(hwnd,uMessage,wParam,lParam);
}


int CreateListBox(HWND hParent)
{
      hListBox  = CreateWindowEx(0,"listbox","",WS_CHILD|WS_VISIBLE|LBS_SORT,0,0,300,20,hParent,NULL,0,0);
    SendMessage(hListBox,LB_ADDSTRING,0,(LPARAM)"MessageBox");
    SendMessage(hListBox,LB_ADDSTRING,0,(LPARAM)"MB_IDOK");
    SendMessage(hListBox,LB_ADDSTRING,0,(LPARAM)"SetWindowText");
    return 1; 
}