#ifndef _A_H #define _A_H #define EDIT_SCREEN_X (8*80) //640 #define EDIT_SCREEN_Y (16*24) //384 #define SCREEN_X (EDIT_SCREEN_X+2+16+2) //border(2)+scroll_bar(16)+space(2) #define SCREEN_Y (EDIT_SCREEN_Y+2+16+2) //border(2)+scroll_bar(16)+space(2) #define WINDOW_XSIZE (SCREEN_X+8) #define WINDOW_YSIZE (SCREEN_Y+8+38) BOOL Register(HINSTANCE hInst); BOOL Create(HINSTANCE hInst, int nCmdShow); LRESULT CALLBACK MainWndProc(HWND hMainwnd, UINT Message, WPARAM wParam, LPARAM lParam); LONG FAR PASCAL EditWndProc(HWND hwndEdit, UINT Message, WPARAM wParam, LPARAM lParam); #endif // a.cpp : Defines the entry point for the application. // #include #include "a.h" FARPROC g_lpfnMainWndProc; // 指向主窗口消息处理函数的指针 HWND g_hwndEdit; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. MSG msg; if (!Register(hInstance)) return FALSE; if (!Create(hInstance, nCmdShow)) return FALSE; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } // // Register Window // BOOL Register(HINSTANCE hInst) { WNDCLASSEX WndClass; WndClass.cbSize = sizeof(WNDCLASSEX); WndClass.style = CS_HREDRAW | CS_VREDRAW; WndClass.lpfnWndProc = MainWndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = hInst; WndClass.hIcon = LoadIcon(hInst,MAKEINTRESOURCE(IDI_INFORMATION)); WndClass.hCursor = LoadCursor(NULL,IDC_ARROW); WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); WndClass.lpszMenuName = NULL; WndClass.lpszClassName = "a414nu"; WndClass.hIconSm = NULL; return (RegisterClassEx(&WndClass) != 0); } // // Create the window and show it. // BOOL Create(HINSTANCE hInst, int nCmdShow) { HWND hMainWnd = CreateWindow("a414nu", "Main", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_XSIZE, WINDOW_YSIZE, NULL, NULL, hInst, NULL); if (hMainWnd == NULL) return FALSE; ShowWindow(hMainWnd, nCmdShow); UpdateWindow(hMainWnd); return TRUE; } LRESULT CALLBACK MainWndProc(HWND hMainwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_CREATE: // 创建控件窗口 g_hwndEdit = CreateWindow (TEXT("edit"), NULL, WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_BORDER|ES_LEFT|ES_MULTILINE|ES_READONLY|ES_AUTOVSCROLL, 0, 0, SCREEN_X, SCREEN_Y, hMainwnd, NULL, ((LPCREATESTRUCT)lParam)->hInstance, NULL); // 让控件的消息处理交给EditWndProc处理,其它消息不受影响 g_lpfnMainWndProc = (FARPROC)SetWindowLong(g_hwndEdit, GWL_WNDPROC, (DWORD)EditWndProc); return S_OK; case WM_DESTROY: SetWindowLong(g_hwndEdit, GWL_WNDPROC, (LONG)g_lpfnMainWndProc); PostQuitMessage(0); return S_OK; default: break; } return DefWindowProc(hMainwnd, Message, wParam, lParam); } LONG FAR PASCAL EditWndProc(HWND hwndEdit, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_CONTEXTMENU: // 成功得到了属于控件窗口的消息 MessageBox(hwndEdit, "success!", "a414nu", MB_ICONINFORMATION); return S_OK; default: break; } // 本函数中没有处理的控件消息交由MainWndProc处理 return CallWindowProc((WNDPROC)g_lpfnMainWndProc, hwndEdit, Message, wParam, lParam);