•
Due
: in Three Weeks
•
DoubleWnd.c Creates Two Windows with The Same Window Class.
•
One Window Is Closed, The Other Is Closed TOO.
•
Requirement
: Modify the doublewnd.c to import the folowing
Functions:
•
Can Close The Two Windows Respectively.
•
The Two Windows Have Different Actions To The Same Window
Message.
The Two Windows Have Different Actions To The Same Window
Message.
my answer:
//Doublewnd.c; Define the entry point of the application.
#include
// Global variable: static tchar szappname [] = text ("doublewnd"); // application name; hwnd hwnd1; // window one handle; hwnd hwnd2; // window two handle; uint check = 0; / / This unsigned integer is used to determine whether a window is turned off, it should end the program;
// Pre-declaration of the function included in this code module: LRESULT CALLBACK WNDPROC (HWND, UINT, WPARAM, LPARAM); Atom MyRegisterClass (Hinstance Hinstance);
Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance, Pstr Szcmdline, ICMDSHOW) {MSG MSG;
// Window initialization; if (! Myregisterclass (hinstance)) {
MessageBox (NULL, TEXT ("This Program Is Error!"), Szappname, MB_ICONERROR); RETURN 0;} // established the first window; hWnd1 = CreateWindow (Szappname, // WINDOW Class Name Text ("THE FIRST WINDOW Zhang Lei "), // window capenow, // window style 40, // initial x position 40, // initial y position 300, // initial x size 300, // initial y size null, // parent window handle null , // Window Menu Handle Hinstance, // Program Instance Handle Null; // CREATION Parameters // Established Second Window; HWND2 = CREATEWI NDOW (Szappname, // WINDOW CLASS NAME TEXT ("The Second Window - Zhang Lei"
), // window Caption WS_OVERLAPPEDWINDOW | WS_Visible, // Window Style 400, // Initial X Position 40, // Initial Y Position 300, // Initial x size 300, // initial y size null, // parent window handle null , //, //program instance handle null; // creeation parameters // Displays two windows and makes the window one in an activation state; showwindow (hwnd2, icmdshow); UpdateWindow (hwnd2); showwindow (hwnd1, ICMDSHOW); UpdateWindow (HWND1); While (GetMessage (& MSG, NULL, 0, 0)) {TranslateMessage (& MSG); DispatchMessage (& MSG);} return msg.wparam; }
// Function MyRegisterClass (Hinstance Hinstance); // Purpose: Initialization and Register Window; Atom MyRegisterClass (Hinstance Hinstance) {WNDCLASS WNDCLASS;
wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor (NULL , IDC_ARROW); WNDCLASS.HBRBACKGROUND = (Hbrush) getStockObject (white_brush); WNDCLASS.LPSZMENUNUNAME = NULL; WNDCLASS.LPSZCLASSNAME = szappname;
Return RegisterClass (& WNDCLASS);
// Function: WndProc (HWND, Unsigned, Word, Long); // Destination: Handling the message of the main window; // wm_lbuttondown - Responding to the left mouse button; // wm_paint - Draw the main window; // WM_DESTROY - Close the current window or exit and return message transmission; LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {HDC hdc; PAINTSTRUCT ps; RECT rect; switch (message) {case WM_LBUTTONDOWN: PlaySound (TEXT ( "hellowin.wav") , NULL, SND_FILENAME | SND_ASYNC); Return 0; Case WM_CREATE: RETURN 0;
Case WM_Paint: HDC = Beginpaint (HWND, & PS); getClientRect (hWnd, & Re); if (hwnd == hwnd1) {DrawText (HDC, Text ("Hello, THE FIRST WINDOW!"), -1, & Rect, DT_SingLine | DT_Center | DT_VCENTER);} else {DrawText (HDC, TEXT ("Hello, Thext, DT_SINGLINE | DT_CENTER | DT_VCENTER); Endpaint (HWND, & PS); Return 0; Case WM_DESTROY: IF (Check == 0) {DestroyWindow (HWND); Check = Check 1;} else {postquitmessage (0); check = check 1;} return 0;}
Return DefWindowProc (hwnd, message, wparam, lparam);