DLL custom window class

Source: Internet
Author: User

DLL custom window class

2011-1-9

The purpose is to simulate windows registering the window class in DLL and then calling it in other modules. The Code is as follows:

Call module:

# Include <windows. h> <br/> # pragma comment (Lib, "F: // myclass // debug // myclassdll. lib ") </P> <p> hwnd mycreateappswex (DWORD dwexstyle, <br/> lpctstr lpclassname, <br/> lpctstr lpwindowname, <br/> DWORD dwstyle, <br/> int X, <br/> int y, <br/> int nwidth, <br/> int nheight, <br/> hwnd hwndparent, <br/> hmenu, <br/> hinstance, <br/> lpvoid lpparam <br/>); </P> <p> int winmain (hinstance, <br /> Hinstance hprevinstance, <br/> lpstr lpcmdline, <br/> int ncmdshow <br/>) <br/>{</P> <p> hwnd = :: mycreateappswex (ws_ex_overlappedwindow, <br/> "myclass", <br/> "Custom window class", <br/> ws_visible | ws_tiledwindow, <br/> cw_usedefault, cw_usedefault, 400,200, // the same window is arranged in an ascending sequence <br/> null, <br/> null, <br/> hinstance, <br/> null ); </P> <p> If (hwnd = NULL) <br/> {<br/>: MessageBox (null, "An error occurred while creating the window! "," ", Mb_ OK); <br/> return 0; <br/>}</P> <p> MSG; <br/> while (getmessage (& MSG, null, 0, 0) <br/>{< br/>: translatemessage (& MSG); <br/>:: dispatchmessage (& MSG); <br/>}< br/> MessageBox (null, "", "", mb_ OK); </P> <p> return 0; <br/>}

DLL module:

# Include <windows. h> <br/> lresult callback windowproc (hwnd, <br/> uint umsg, <br/> wparam, <br/> lparam <br/>) <br/>{< br/> switch (umsg) <br/>{< br/> case wm_close: <br/>:: destroywindow (hwnd ); <br/>: postquitmessage (0); <br/> break; <br/>}</P> <p> return: defwindowproc (hwnd, umsg, wparam, lparam); <br/>}</P> <p> bool apientry dllmain (hmodule, <br/> DWORD ul_reason_for_call, <B R/> lpvoid lpreserved <br/>) <br/> {<br/> switch (ul_reason_for_call) <br/>{< br/> case dll_process_attach: <br/>{< br/> wndclassex WC; <br/> WC. cbsize = sizeof (WC); <br/> WC. style = cs_vredraw | cs_hredraw; // The value can be 0, but cannot be left blank! <Br/> WC. lpfnwndproc = windowproc; <br/> WC. cbclsextra = 0; <br/> WC. cbwndextra = 0; <br/> WC. hinstance =: getmodulehandle (null); <br/> WC. hicon =: loadicon (null, idi_application); <br/> WC. hcursor =: loadcursor (null, idc_arrow); <br/> WC. hbrbackground =: createsolidbrush (RGB (255,255,255); <br/> WC. lpszmenuname = NULL; <br/> WC. lpszclassname = "myclass"; <br/> WC. hiconsm = NULL; </P> <p >:: registerclassex (& WC); <br/>}</P> <p> case dll_thread_attach: <br/> case dll_process_detach: <br/> case dll_thread_detach: <br/> break; <br/>}</P> <p> return true; <br/>}</P> <p> hwnd mycreateappswex (DWORD dwexstyle, <br/> lpctstr lpclassname, <br/> lpctstr lpwindowname, <br/> DWORD dwstyle, <br/> int X, <br/> int y, <br/> int nwidth, <br/> int nheight, <br/> hwnd hwndparent, <br/> hmenu, <br/> hinstance, <br/> lpvoid lpparam <br/>) <br/>{< br/> return :: createmediawex (dwexstyle, <br/> lpclassname, <br/> lpwindowname, <br/> dwstyle, <br/> X, <br/> Y, <br/> nwidth, <br/> nheight, <br/> hwndparent, <br/> hmenu, <br/> hinstance, <br/> lpparam <br/> ); <br/>}< br/>

Def File

Library "myclassdll" <br/> exports <br/> mycreatedomainwex

 

Summary:

1.
The solution generates a debug folder (of course, the release folder is generated in the release mode), which is used to store the output of each project in a unified manner (DLL, Lib, or
EXE ).

2.
If the module does not call any function in the DLL, the DLL cannot be loaded. That is to say, deleting the DLL will not affect the normal execution of the program. Therefore, to load the DLL (to register a custom window class), you must call the function. In DLL, The createmediawex in user32.dll is encapsulated into mycreatemediawex, so that the calling module can load the DLL code at the same time of createwindow, so as to register the custom window class. I guess that when the user calls createwindow Wex in user32.dll, the system window class "button" has been registered in the initial code of user32.dll.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.