Development practice of Drawing program

Source: Internet
Author: User
Tags bool

Objective

This program is the use of the General Hot Key Management DLL implementation of the Hotkey function, after receiving a hotkey notification to intercept the contents of the screen and save to the BMP file. This article provides all the source code for your reference.

Program Run interface

For example, we set the picture save path for C:\, hotkey for F9 + control, and then press the Change button to set the hotkey, when we press CTRL+F9 in any program, the current interface will be saved in the C:\ under the BMP file. Let's take a look at the implementation principle and the basic structure of the program.

One, Hotkey management DLL

This DLL is actually a keyboard hook that monitors the system's keyboard events. Notifies the program's window if there are keys and key combinations that match the registration of the program. For ease of application, I made it into a standard management library for other programs through the Hot Key service, it has two output functions: Addhotkey and Deletehotkey, the program only need to call these two functions on it, if you do not change the hot key after compiling, You just need to addhotkey.

1. Global variables

All global variables are placed in a shared segment, defined as follows:

#pragma data_seg("shareddata")
  HHOOK hHook =NULL; //钩子句柄
  UINT nHookCount =0; //挂接的程序数目
  static UCHAR HotKey[MAX_KEY] = {0}; //热键虚拟键码
  static UCHAR HotKeyMask[MAX_KEY] = {0}; //组合掩码, control=4,alt=2,shift=1 可以“或”
  static HWND hCallWnd[MAX_KEY] = {0}; //window handle associated with hotkey
  static int KeyCount =0;
  static UCHAR MaskBits =0; //00000 Ctrl=4 & Alt=2 & Shift=1
#pragma data_seg()

There are several important notes about shared segments:

A. The segment must be specified in the Link option as shared, Project->settings->link->object/library, plus/SECTION:SHAREDDATA,RWS

B. The second way to specify a shared segment: Add a sentence to the sections of the DEF file Shareddata Read Write shared

C. Third method of specifying shared segments: Add a sentence to the program #pragma comment (linker, "SECTION:SHAREDDATA,RWS")

D. All variables must be initialized, otherwise the linker will put it in the normal data segment

E. If the variable is not initialized, it needs to be defined in the form of "__declspec (allocate (" shareddata)) Variable type variable name "in the segment

2. The two output functions are as follows:

//添加热键,如果cKey和cMask均为0则监视所有按键事件
BOOL __declspec(dllexport) __stdcall AddHotkey(HWND hWnd,UCHAR cKey,UCHAR cMask)
{
  BOOL bAdded=FALSE;
  for(int index=0;index<MAX_KEY;index++){
    if(hCallWnd[index]==0){
     hCallWnd[index]=hWnd;
     HotKey[index]=cKey;
     HotKeyMask[index]=cMask;
     bAdded=TRUE;
     KeyCount++;
     break;
    }
  }
  return bAdded;
}
//删除热键
BOOL __declspec(dllexport) __stdcall DeleteHotkey(HWND hWnd,UCHAR cKey,UCHAR cMask)
{
  BOOL bRemoved=FALSE;
  for(int index=0;index<MAX_KEY;index++){
    if(hCallWnd[index]==hWnd){
     if(HotKey[index]==cKey&&HotKeyMask[index]==cMask){
      hCallWnd[index]=NULL;
      HotKey[index]=0;
      HotKeyMask[index]=0;
      bRemoved=TRUE;
      KeyCount--;
      break;
     }
    }
  }
  return bRemoved;
}

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.