33,321
社区成员




#include <windows.h>
#include <iostream>
using namespace std;
HWND g_hWnd = NULL;
HINSTANCE g_hInst;
volatile bool bCanExit = false;
#pragma comment(linker, "/subsystem:\"console\" /entry:\"main\"")
LRESULT WndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam) {
switch(wMsg){
case WM_CLOSE:
bCanExit = true;
PostQuitMessage(0);
return (0);
default:
return ::DefWindowProcA(hWnd, wMsg, wParam, lParam);
}
}
void CreateWnd(void) {
WNDCLASS wc = {0};
wc.style = 0;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = g_hInst;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_GRAYTEXT);
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("SimpleWindow");
RegisterClass(&wc);
g_hWnd = CreateWindowEx(0,
TEXT("SimpleWindow"),
TEXT("SimpleWindow"),
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
g_hInst,
0);
}
DWORD MyThread(PVOID pArg) {
CreateWnd();
MSG msg;
while(GetMessage(&msg,NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
int main(){
g_hInst = GetModuleHandle(NULL);
HANDLE hThrd = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MyThread, NULL, 0, NULL);
CloseHandle(hThrd);
while(!bCanExit){
Sleep(10);
}
cout<<"windows has closed"<<endl;
getchar();
return 0;
}
#include <conio.h>
#include <windows.h>
int main() {
int k;
while (1) {
if (kbhit()) {
k=getch();
if (0==k || 0xE0==k) k=k<<8|getch();
if (27==k) break;//按Esc键退出
cprintf("\r\n%04X\r\n",k);
}
Sleep(200);
cprintf(".");
}
return 0;
}
#include <conio.h>
#include <windows.h>
int main() {
int k;
while (1) {
if (kbhit()) {
k=getch();
if (0==k || 0xE0==k) getch();
if (27==k) break;//按Esc键退出
cprintf("\r\n%04X\r\n",k);
}
Sleep(200);
cprintf(".");
}
return 0;
}
运行时按上下左右键试试看。