上代码 c++ TDM-GCC 4.9.2:
#include <windows.h>
#include<cstdio>
#include<cstring>
void movew(int *x,int *y,int *xk,int *yk,bool *ux,bool *uy,bool *uxk,bool *uyk,int k){
//x变化
if(*uxk){
*xk=*xk+k;
}
else{
*xk=*xk-k;
}
if(*xk>=1366-150){
*uxk=false;
}
else if(*xk<=0){
*uxk=true;
}
//y变化
if(*uyk){
*yk=*yk+k;
}
else{
*yk=*yk-k;
}
if(*yk>=768-150){
*uyk=false;
}
else if(*yk<=0){
*uyk=true;
}
//x高度变化
if(*ux){
*x=*x+k;
}
else{
*x=*x-k;
}
if(*x>=1366){
*ux=false;
}
else if(*x<=150){
*ux=true;
}
//y高度变化
if(*uy){
*y=*y+k;
}
else{
*y=*y-k;
}
if(*y>=768){
*uy=false;
}
else if(*y<=150){
*uy=true;
}
}
/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
/* Upon destruction, tell the main thread to stop */
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
/* All other messages (a lot of them) are processed using default procedures */
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc; /* A properties struct of our window */
HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
MSG msg; /* A temporary location for all messages */
HWND hwnd1;
/* zero out the struct and set the stuff we want to modify */
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
//以上是基础,误删!
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Begin",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
500, /* width */
500, /* height */
NULL,NULL,hInstance,NULL);
MessageBox(hwnd, "请关掉Begin窗口","开始",
MB_ICONWARNING |MB_OK );
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
/*
This is the heart of our program where all input is processed and
sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
this loop will not produce unreasonably high CPU usage
*/
while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
TranslateMessage(&msg); /* Translate key codes to chars if present */
DispatchMessage(&msg); /* Send it to WndProc */
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","哈哈哈",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
150, /* width */
150, /* height */
NULL,NULL,hInstance,NULL);
hwnd1 = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","哈哈哈",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
150, /* width */
150, /* height */
NULL,NULL,hInstance,NULL);
int x1,y1,xk1,yk1;
bool ux1=true;
bool uy1=true;
bool uxk1=true;
bool uyk1=true;
int x2,y2,xk2,yk2;
bool ux2=true;
bool uy2=true;
bool uxk2=true;
bool uyk2=true;
x1=y1=150;
xk1=yk1=1;
x2=y2=150;
xk2=600;
yk2=30;
int k=5;
while(true){
MoveWindow(hwnd,xk1,yk1,x1,y1,false);
MoveWindow(hwnd1,xk2,yk2,x2,y2,false);
movew(&x1,&y1,&xk1,&yk1,&ux1,&uy1,&uxk1,&uyk1,k);
movew(&x2,&y2,&xk2,&yk2,&ux2,&uy2,&uxk2,&uyk2,k);
Sleep(1);
}
return msg.wParam;
}
各位大佬们,如果有懂得,麻烦看看那个问题,谢谢了