写了一个让窗口置顶的小程序公开源码

这是一篇关于如何创建一个能将任意窗口置顶的简单程序的文章,作者通过提供的C++代码实现该功能。代码简洁易懂,适合自学编程者参考。用户可以通过点击或右键菜单操作来开启或关闭窗口的置顶状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时想让一个窗口总在顶层,windows窗口又没这一选项

便写了一个能置顶窗口的工具,代码很短,编译一下就能用

顺便说一下,我写程是自学的,我只有相当于高中的文化,

要是有什么单词不对,请多指正。

 

程序代码:

#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "wkongyuntotop";

HDC mydc;
HDC screendc;
HBITMAP mainbitmap;
HBITMAP *mainoldbitmap;
HDC memmaindc;
HDC hwnddc;
PAINTSTRUCT paintstru;
POINT   point[100];
int pointindex;
RECT parerect;
bool btopmost;

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           WS_EX_TOOLWINDOW,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "",       /* Title Text */
           WS_POPUP | WS_VISIBLE, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           0,                 /* The programs width */
           0,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int msid,msevent;
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
            ShowWindow(hwnd,false);
            SetTimer(hwnd,14001,0,0);
            pointindex=0;
            btopmost=true;
            break;
        case WM_COMMAND:
            {
   int wmId=LOWORD(wParam);
   int wmEvent=HIWORD(wParam);
   switch(wmId){
                case 1000:
                    btopmost=true;
                break;
                case 1001:
                    btopmost=false;
                break;
                case 1002:
                    pointindex=0;
                    ShowWindow(hwnd,SW_HIDE);
                    SetTimer(hwnd,14001,5000,0);
                break;
            }
            }
            break;
        case WM_PAINT:
            hwnddc = BeginPaint(hwnd,&paintstru);
            BitBlt(hwnddc,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),memmaindc,0,0,SRCCOPY);
            EndPaint(hwnd, &paintstru);
            break;
        case WM_LBUTTONUP:
            point[pointindex].x=LOWORD(lParam);
            point[pointindex].y=HIWORD(lParam);
            pointindex++;
            if(!(GetKeyState(VK_CONTROL)&0x10000000)){
                btopmost=true;
            }else{
                btopmost=false;
            }
            break;
        case WM_MBUTTONUP:
            {
            int x=LOWORD(lParam);
            int y=HIWORD(lParam);
            HMENU menu=CreatePopupMenu();
            AppendMenu(menu,MF_STRING,0,"取消");
            AppendMenu(menu,MF_STRING,1002,"等一会儿");
            AppendMenu(menu,MF_STRING,1000,"没置顶层属性");
            AppendMenu(menu,MF_STRING,1001,"去除顶层属性");
            AppendMenu(menu,MF_SEPARATOR,1002,"dsadfadfsa");
            AppendMenu(menu,MF_STRING,1003,"Left设置顶层");
            AppendMenu(menu,MF_STRING,1003,"Ctrl+Left去除顶层");
            TrackPopupMenu(menu,TPM_LEFTALIGN,x,y,0,hwnd,0);
            }
            break;
        case WM_RBUTTONUP:
            ShowWindow(hwnd,SW_HIDE);
            for(int i=0;i<pointindex;i++){
                HWND parehwnd=WindowFromPoint(point[i]);
                HWND   parent2=parehwnd,parent;
                do{
                    parent=parent2;
                    parent2=GetParent(parent2);
                }while(parent2!=NULL);
                if(btopmost)
                    SetWindowPos(parent,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_SHOWWINDOW);
                else
                    SetWindowPos(parent,HWND_NOTOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_SHOWWINDOW);
            }
            SelectObject(memmaindc,mainoldbitmap);
            ReleaseDC(hwnd,screendc);
            PostQuitMessage(0);
            break;
        case WM_TIMER:
            msid=LOWORD(wParam);
            msevent=HIWORD(wParam);
            switch(msid){
                case 14001:
                    screendc =CreateDCW(L"DISPLAY", NULL, NULL, NULL);
                    mainbitmap=CreateCompatibleBitmap(screendc,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
                    memmaindc=CreateCompatibleDC(screendc);
                    mainoldbitmap=(HBITMAP*)SelectObject(memmaindc,mainbitmap);
                    BitBlt(memmaindc,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),screendc,0,0,SRCCOPY);
                    if(SetWindowPos(hwnd,HWND_TOPMOST,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),SWP_SHOWWINDOW)==FALSE){
                    PostQuitMessage(0);
                    }
                    SetActiveWindow(hwnd);
                    SetFocus(hwnd);
                    KillTimer(hwnd,14001);
                break;
            }
            break;
        case WM_HELP:
                MessageBox(hwnd,"to click the need topmost or need/nremove topmost window./nend with right click./n/n   ------wkongyun software------/n         author:wkongyun/n         date:2009-6-13","Go Top Most help",0);
            break;
        case WM_DESTROY:
        case WM_QUIT:
        case WM_CLOSE:
            SelectObject(memmaindc,mainoldbitmap);
            ReleaseDC(hwnd,screendc);
            PostQuitMessage(0);
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值