悬浮可拖动的控制台窗体

2020年5月18日第一次更新:两个窗口重叠时会同时移动,解决这个小问题!

函数:WindowFromPoin

功能为获取坐标结构下的窗口句柄,这里判断是否是自己窗口的句柄就能知道是否在窗体内

于是就不需要之前的坐标判断了,直接用函数判断句柄 hwnd==WindowFromPoint(xy)

不知道哪个方法速度快呢?

#include <windows.h>
//#include <winuser.h>
#include <stdio.h>

DWORD WINAPI T_move(LPVOID lpParam){
    POINT xy,xy2;
    RECT rect;
    HANDLE hwnd=GetConsoleWindow();
while(1)
{//============================================= 
    Sleep(33);
    
    if(GetAsyncKeyState(1)){
        GetCursorPos(&xy);
        GetWindowRect(hwnd,&rect);
        if(hwnd==WindowFromPoint(xy)){
            xy2.x=xy.x-rect.left;
            xy2.y=xy.y-rect.top;
            while(GetAsyncKeyState(1)){
            Sleep(16);
            GetCursorPos(&xy);
            SetWindowPos(hwnd,HWND_TOPMOST,xy.x-xy2.x,xy.y-xy2.y,0,0,SWP_NOSIZE);
            }
        }
        else{
            while(GetAsyncKeyState(1)){
                Sleep(33);
            }
        }
            
    }        
}//============================================= 
} 

void mmain(){
    SetWindowPos(GetConsoleWindow(),HWND_TOPMOST,0,0,600,600,SWP_NOMOVE);
    SetWindowLongPtrA(
        GetConsoleWindow(),
        GWL_STYLE,
        GetWindowLongPtrA(GetConsoleWindow(),GWL_STYLE)
        & ~ WS_CAPTION & ~ WS_SIZEBOX
    );
    HANDLE hStdin=GetStdHandle(STD_INPUT_HANDLE);  
    DWORD mode;  
    GetConsoleMode(hStdin,&mode);  
    mode&= ~ ENABLE_QUICK_EDIT_MODE;
    mode&= ~ ENABLE_INSERT_MODE;
    mode&= ~ ENABLE_MOUSE_INPUT;
    SetConsoleMode(hStdin,mode);
}
int main(void){
    CreateThread(NULL,0,&T_move,0,0,0);
    mmain();
    
    while(1){
        Sleep(100);
    
    }

}

废话少说,直接上源码,使用时调用那个函数就好啦!

不过拖动操作是多线程监控的,建议直接套用整个源码。

#include <windows.h>
//#include <winuser.h>//这里注释掉是为了兼容TDM编译
#include <stdio.h>

DWORD WINAPI T_move(LPVOID lpParam){
    POINT xy,xy2;
    RECT rect;
    HANDLE hwnd=GetConsoleWindow();
while(1)
{//============================================= 
    Sleep(33);
    
    if(GetAsyncKeyState(1)){
        GetCursorPos(&xy);
        GetWindowRect(hwnd,&rect);
        if(xy.x>rect.left && xy.x<rect.right && xy.y>rect.top && xy.y<rect.bottom){
            xy2.x=xy.x-rect.left;
            xy2.y=xy.y-rect.top;
            while(GetAsyncKeyState(1)){
            Sleep(16);
            GetCursorPos(&xy);
            SetWindowPos(hwnd,HWND_TOPMOST,xy.x-xy2.x,xy.y-xy2.y,0,0,SWP_NOSIZE);
            }
        }
        else{
            while(GetAsyncKeyState(1)){
                Sleep(33);
            }
        }
            
    }        
}//============================================= 
} 

void mmain(){
    SetWindowPos(GetConsoleWindow(),HWND_TOPMOST,0,0,600,600,SWP_NOMOVE);
    SetWindowLongPtrA(
        GetConsoleWindow(),
        GWL_STYLE,
        GetWindowLongPtrA(GetConsoleWindow(),GWL_STYLE)
        & ~ WS_CAPTION & ~ WS_SIZEBOX
    );
    HANDLE hStdin=GetStdHandle(STD_INPUT_HANDLE);  
    DWORD mode;  
    GetConsoleMode(hStdin,&mode);  
    mode&= ~ ENABLE_QUICK_EDIT_MODE;
    mode&= ~ ENABLE_INSERT_MODE;
    mode&= ~ ENABLE_MOUSE_INPUT;
    SetConsoleMode(hStdin,mode);
}
int main(void){
    CreateThread(NULL,0,&T_move,0,0,0);
    mmain();
    
    while(1){
        Sleep(100);
    }

}