有时想让一个窗口总在顶层,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;
}