1. 程式人生 > >win32(002) 建立視窗和 子視窗 事件處理

win32(002) 建立視窗和 子視窗 事件處理

win32(002)  建立視窗和 子視窗 事件處理

不處理了自己上程式碼 我會上傳


// win3202.cpp : Defines the entry point for the application.
//


#include "stdafx.h"
#define    begbuttonid 0x100
#define    stopbuttonid 0x101
#define    actbuttonid  0x102
#define    aboutbuttonid 0x103
#include <malloc.h>
#include <crtdbg.h>
HWND hmainwindow;


LRESULT CALLBACK WindowProc(  HWND hwnd,      // handle to window
UINT uMsg,      // message identifier
WPARAM wParam,  // first message parameter
LPARAM lParam   // second message parameter
  )
{
switch (uMsg)
{
case WM_MOVE :{
_RPT1(_CRT_WARN, "WM_MOVE:%x\r\n",uMsg);
  }


break;


case WM_CREATE :
{

_RPT1(_CRT_WARN, "WM_CREATE:%x\r\n",uMsg);


CreateWindow(
"Button",
"開始記錄",
WS_VISIBLE|WS_CHILD,
50,
50,
100,
30,
hwnd,
(HMENU)begbuttonid,
NULL,
NULL
);
CreateWindow(
"Button",
"停止播放記錄",
WS_VISIBLE|WS_CHILD,
200,
50,
100,
30,
hwnd,
(HMENU)stopbuttonid,
NULL,
NULL
);
CreateWindow(
"Button",
"抖動視窗",
WS_VISIBLE|WS_CHILD,
50,
150,
100,
30,
hwnd,
(HMENU)actbuttonid,
NULL,
NULL
);
CreateWindow(
"Button",
"關於",
WS_VISIBLE|WS_CHILD,
200,
150,
100,
30,
hwnd,
(HMENU)aboutbuttonid,//一定要寫
NULL,
NULL
);
}
break;
case WM_COMMAND:
{

int j=0;
staticPOINT point[MAXBYTE]={0};
WORD wID = LOWORD(wParam);
switch(wID){

case begbuttonid:
{

SetWindowText(hwnd,"開始記錄");

for (int i=0;i<50;i++)

GetCursorPos(&point[j]);
Sleep(100);
_RPT2(_CRT_WARN, "begin:x座標: %d y座標: %d\r\n", point[j].x, point[j].y);
j++;
}


}
break;


case stopbuttonid:

{
SetWindowText(hwnd,"停止記錄並播放");
for (int i=0;i<50;i++)

SetCursorPos(point[j].x,point[j].y);
Sleep(100);
_RPT2(_CRT_WARN, "stop:x座標: %d y座標: %d \r\n", point[j].x, point[j].y);
j++;
}

}
break;
case actbuttonid:
{



for(int i=0;i<500;i++){
MoveWindow(hmainwindow,150,150,400,400,TRUE);

                    MoveWindow(hmainwindow,100,100,400,400,TRUE);

}


}
break;

case aboutbuttonid:
{

MessageBox(NULL,"關於程式","提示",MB_ICONERROR);

}
break;




}
}
break;


case WM_CLOSE:
{
/*PostQuitMessage(0);*/
PostMessage(hwnd,WM_QUIT,0,0);
_RPT1(_CRT_WARN, "WM_CLOSE:%x\r\n",uMsg);
}
break;
default: 
DefWindowProc(hwnd, uMsg, wParam, lParam);
}


return 1;






}




int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{








   //必須初始化
WNDCLASS wmainclass={0};
wmainclass.style=CS_HREDRAW|CS_VREDRAW;
wmainclass.lpfnWndProc=WindowProc;
wmainclass.hbrBackground=(HBRUSH)COLOR_BACKGROUND;
wmainclass.lpszClassName="wmainclass";
ATOM atom=RegisterClass(&wmainclass);
if (atom==0)
{


MessageBox(NULL,"註冊類失敗","提示",MB_ICONERROR);
}
 hmainwindow=CreateWindow(
"wmainclass",
"滑鼠記錄器",
       WS_MAXIMIZEBOX|WS_MINIMIZEBOX|WS_VISIBLE|WS_CAPTION|WS_BORDER|WS_SYSMENU,
100,
100,
400,
400,
NULL,
NULL,
NULL,
NULL


);




//ShowWindow(hmainwindow, SW_SHOWNORMAL);

MSG msg={0};
while (GetMessage(&msg,hmainwindow,0,0))
{


DispatchMessage(&msg);
}




return 0;
}