C + + MFC common functions (GO)

Source: Internet
Author: User
Tags class definition readfile win32

WinExec ()

ExitWindowsEx ()

GlobalMemoryStatus ()

GetSystemInfo ()

GetSystemDirectory ()

GetWindowsDirectory ()

Get taskbar window handle in Gettaskmanwindow ()//user32.dll

OpenProcessToken () Open an access token for a process

GetCurrentProcess () Gets the handle to this process

Lookupprivilegevalue () Modify Process permissions

Adjusttokenprovileges () Notify WindowsNT to modify the rights of this process

CreateRectRgn ()

CreateEllipticRgnIndirect ()

PtInRegion ()

Commandtoindex () ID number converted to index value

Menu class:

DrawMenuBar () Re-draw Menu

Setdefaultitem () Sets the default menu item

Checkmenuitem () Set menu item Check Status

CreatePopupMenu () Create pop-up menu

Window class:

MoveWindow ()

Invalidate () Invalidates window, window redraw when window is invalid

GetParent () Gets the parent window handle of the window

BringWindowToTop () Put the window to the top

SetWindowPos ()

DeferWindowPos ()

SetActiveWindow () Activates the top-level window

Set/getforegroundwindow () set/Get foreground window, return handle

Set/getwindowlong () changing window properties

Getnextwindow () Gets the next window handle

GetWindow () Gets the window handle

FromHandle () get pointer through handle

AfxGetInstanceHandle () Gets the program current instance handle

AfxRegisterWndClass ()

Set/getclasslong ()

Set/getwindowlong ()

GetWindowRect () Gets the rect of the window in the screen

dialog box:

GetDlgItem ()

Set/getdlgitemtext ()

Set/getdlgitemint ()

Set/getdlgitem ()

SendDlgItemMessage ()

SetFocus () Text editing box edit control to respond to a carriage return key message, you must set it to a multiline type

Getnextdlgtabitem () Gets the next control handle with the TabStop property

OnCtlColor () Wm_ctlcolor window Draw message response function

GetDlgCtrlID ()

SetButtonText () Sets the button text CreateRectRgn

GetStartPosition ()

Getnextpathname ()

SetHorizontalExtent ()

SHBrowseForFolder ()

Property sheet:

SetWizardMode ()

SetWizardButtons ()

OnSetActive ()

Tool bar:

RecalcLayout ()

Showcontrolbar ()

Status bar:

Commandtoindex () Get index value based on ID number

Setmessagetext ()

Getmessagebar ()

Getdescendantwindow ()

Graphics class

OnEraseBkgnd ()

CButton::D Rawitem ()

SetBkMode () Set text background

SetTextColor ()

SetBkColor ()

SetROP2 ()

Setcliprtn ()

DrawText ()

Beginpath ()

Endpath ()

BeginPaint ()

EndPaint ()

Gettextextend ()

Setworldtransform ()

GetDeviceCaps ()

Set/getviewportorg ()

Set/getwindoworg ()

Dptolp () Converts a device coordinate point to a logical coordinate point

LPTODP () Converts a logical coordinate point to a device coordinate point

Deletemetafile ()

Copymetafile ()

Getmetafile ()

GetEnhMetaFile ()

void Cview::onpaint ()

{

CPAINTDC DC (this);

OnPrepareDC (&DC);

OnDraw (&DC);

}

File operation:

EncryptFile ()

CreateFile ()

ReadFile ()

WriteFile ()

SetFilePointer ()

:: WriteProfileString () write related information to Win.ini

:: GetProfileString () Get related information from Win.ini

Cwinapp::writeprofilestring () write related information to the registry

Cwinapp::getprofilestring () Get relevant information from the registry

:: WritePrivateProfileString ()

RegCreateKeyEx () Create a registry key

RegCloseKey () Close the registry key

RegOpenKeyEx () to open the registry key

RegSetValueEx () Write registry key

RegQueryValueEx () Read registry key

RegEnumKeyEx ()

Regdeletekey ()

Document View:

Hook:

SetWindowsHookEx ()

CallNextHookEx ()

UnhookWindowsHookEx ()

GetCurrentThreadID ()

GetModuleHandle ()

Dynamic libraries:

#pragma data_seg ()

#pragma COMMENT (linker, "/SECTION:XXX,RWS") R readable W writable s share

Segments

XXX READ WRITE SHARED

Makeintresource ()

GetProcAddress () Get function address

LoadLibrary () Load DLL

FreeLibrary () Reduce the reference count of DLLs

Multithreading:

CreateThread ()

CloseHandle ()

Mutually exclusive:

CreateMutex ()

ReleaseMutex ()

WaitForSingleObject ()

Event:

CreateEvent ()

SetEvent () Set signal status

ResetEvent () Set no signal status

Critical section:

EnterCriticalSection ()

InitializeCriticalSection ()

LeaveCriticalSection ()

DeleteCriticalSection ()

Asynchronous sockets:

WSAAsyncSelect () Request a network message notification for a socket

Wsaenumprotocols () retrieving available network communication protocols

WSASocket ()

Clipboard:

OpenClipboard () to open the Clipboard

CloseClipboard () Close the Clipboard

EmptyClipboard () Empty the Clipboard

SetClipboardData ()

GetClipboardData ()

GlobalAlloc ()

GlobalLock ()

GlobalUnlock ()

Isclipboardformatavailable ()

Anonymous pipeline:

CreatePipe ()

CreateProcess ()

CreateFile ()

ReadFile ()

WriteFile ()

Named pipes:

CreateNamedPipe ()

Connectnamedpipe ()

Waitnamedpipe ()

CreateFile ()

ReadFile ()

WriteFile ()

Postal Slots:

Createmailslot ()

Multimedia:

Mcisendcommand () <mmsystem.h>

Conversion of a window, a pointer to a control, and a handle

1 pointer converted to handle

In an MFC application, you first get a pointer to the window and then convert it to a handle

cwnd* pWnd;

HANDLE hWnd = Pwnd->getsafehwnd ();

2 Handle converted to pointer

In an MFC application, you first get a handle to the dialog box control and then get its pointer

HANDLE hWnd;

GetDlgItem (Idc_xxx,&hwnd);

CWnd * pWnd = FromHandle (hWnd);

Ways to get the program window pointer

1 Get the main frame window pointer (can be used at any time, as long as it is in the MFC program)

cwnd* pWnd = AfxGetMainWnd ();

2 Getting the control pointer in the dialog box

cwnd* pWnd = GetDlgItem (idc_xxx);

3 getting a handle to a control in a dialog box

HANDLE GetDlgItem (M_hdlg,m_nid_dlgitem);

4 getting a handle to a GDI object

HANDLE m_hgdiobj = M_pgdiobj->getsafehanle ();

1. How do I get an instance handle of an application? AfxGetInstanceHandle ()

The instance handle of the application is saved in Cwinappim_hinstance, so you can call Afxgetinstancdhandle to get the handle.

Example:handle Hinstance=afxgetinstancehandle ();

2. How do I get pointers to the application's main window via code? AfxGetMainWnd GetSafeHwnd () afxgetappname () Afxgetthread

The pointer to the main window is saved in Cwinthread::m_pmainwnd, and the AfxGetMainWnd implementation is called.

"Example" AfxGetMainWnd ()->showwindow (sw_showmaxmized); Maximizes the program.

The main window of this example is a dialog box, and the following code shows the path in a few static text boxes (sub-windows) in the main dialog box (the main window) in the other Cfiletreectrl class (sub-window) of the related function implementation:

cwnd* m_pcwnd= AfxGetMainWnd (); Get the main window pointer to access other child window resources through the main pane pointer

Method One

M_pcwnd->setdlgitemtext (Idc_static_path, "cwnd*" +m_scurpath); Display a string in a child window (Id:idc_static_path) in the main window

M_pcwnd->setdlgitemtext (idc_static_who, "path display is done by the Filetreectrl class:");

Method Two

M_pcwnd->sendmessage (stn_clicked); Sends a message to the main window to show that the task is completed by the master window.

In the main window of the. cpp: On_message (stn_clicked, ONSTATICPATH3) Related description

Some functions must be accessed through the handle of the window, we can use the following method three

Cwnd::getsafehwnd

Returns the window handle for a window

HWND GetSafeHwnd () const;

HWND m_hwnd_tree =getsafehwnd ();//"Note" Here is just a handle to the current window (Filetree Class)

HWND m_hwnd = M_pcwnd->getsafehwnd ();//This is the handle to the main window (the handle to the main pane is obtained by the main window pointer)

BOOL SetWindowText (HWND hwnd, LPCTSTR lpstring)

:: SetWindowText (M_hwnd, "ok2222");//Modify Main Window Caption

:: Setdlgitemtext (M_hwnd,idc_static_path2, "hWnd:" +m_scurpath);

"Another" Afxgetthread

cwinthread* Afxgetthread ();

Return Value:pointer to the currently executing thread.

3. How do I get the icons for other programs in my program? AfxGetInstanceHandle ()

HInstance AfxGetInstanceHandle ();

Return Value

An hinstance to the current instance of the application. If called from within a DLL linked with the Usrdll version of MFC, an hinstance to the DLL is returned.

Remarks

This function allows retrieve the instance handle of the current application. AfxGetInstanceHandle always returns the hinstance of your executable file (. EXE) unless it is called from within a DLL linked with the Usrdll version of MFC. In this case, it returns a hinstance to the DLL.

Two methods:

(1) SDK function Shgetfileinfo or use ExtractIcon to get the handle (handle) of the icon resource,

(2) SDK function Shgetfileinfo Get a lot of information about the file, such as size icon, property, type, etc.

Example (1): Displays the Notepad icon in the upper-left corner of the program window.

void Csampleview:ondraw (CDC * PDC)

{

if (:: Shgetfileinfo (_t ("C:\\pwin95\\notepad.exe"), 0,

&stfileinfo,sizeof (Stfileinfo), Shgfi_icon))

{

PDC->drawicon (10,10,stfileinfo.hicon);

}

}

Example (2): same function, use ExtractIcon function

void Csampleview:: OnDraw (CDC *PDC)

{

Hicon hicon=:: ExtractIcon (AfxGetInstanceHandle (), _t

("NotePad.exe"), 0);

if (Hicon &&hicon!= (HICON)-1)

Pdc->drawicon (10,10,hicon);

}

"description" about how to get the correct path of the system files, such as the path of Win.ini System32.ini, the specific path in various systems is not the same. Such as:

The path of obtaining notepad.exe is normally obtained by using the GetWindowsDirectory function;

If the brush under Win95 is called, its path should be obtained by accessing the registry;

To make a more sophisticated procedure, consider the overall point.

Another

HInstance AfxGetResourceHandle ();

Return Value:an hinstance handle where the default resources of the application are loaded.

4. About getting the desktop handle GetDesktopWindow ()

An example from MSDN:

static function CWnd:: GetDesktopWindow Returns a pointer to the desktop window. The following example illustrates the MFC

void Cframewnd::beginmodalstate ()

{

First count all windows this need to be disabled

UINT ncount=0;

HWND hwnd=:: GetWindow (:: GetDesktopWindow (), gw_child);

while (Hwnd!=null)

{

if (:: IsWindowEnabled (hwnd) &&

Cwnd::fromhandlepermanent (hWnd)!=null &&

Afxisdescendant (Pparent->m_hwnd, hWnd) &&

:: SendMessage (hWnd, wm_disablemodal, 0, 0) = = 0)

{

++ncount;

}

Hwnd=:: GetWindow (HWnd, Gw_hwndnext);

}

}

User's problem: The following procedure does not take a handle to the same program, but GetModuleFileName returns the same as the result of wonder why

HWND chwnd;//child window Handle

HWND Hwdesktop=::getdesktopwindow ();//Get Desktop handle

Chwnd=::getwindow (hwdesktop,gw_child);//Get desktop clause handle

CString cstitle,csclass,cstm,mlookstring;

Char szbuffer[255];

while (Chwnd!=null)//The sibling handle of the loop take clause handle

{

if (:: IsWindowVisible (Chwnd))//Determine if the window is visible

{

:: GetWindowText (Chwnd,cstitle.getbuffer (255), 254);

:: GetClassName (Chwnd,csclass.getbuffer (255), 254);

Cstitle.releasebuffer ();//title

Csclass.releasebuffer ();//class name

Cstm.format ("%08x:", Chwnd);

if (cstitle== "")

{

Mlookstring=cstm+csclass;

}else

{

Mlookstring=cstm+cstitle;

}

The window handle here is not the same program? (the problem is!) But for what it turns out to be

HINSTANCE hinstance = (hinstance):: GetWindowLong (Chwnd,dwl_user);

:: GetModuleFileName (hinstance, Szbuffer, sizeof (szbuffer));

MessageBox (szbuffer,mlookstring);

}

Chwnd=::getwindow (Chwnd,gw_hwndnext);

}

Reply:

The problem is that Win32 GetWindowLong (Chwnd,dwl_user) always returns the HINSTANCE the current program is running, so you get a filename that is always one. So you get the program name by enumerating all of the "process's program names."

= = = The difference between a handle and a pointer

Many friends who start to learn VC, the most heard of the two words are pointers and handles.

But it is often unclear what the difference is between them.

First, the handle is a window flag, that is, all inherited from the CWnd class, there are many handles this member.

The only thing he can do is to represent a window on a desktop. The pointer is an address, if it points to an in-memory object, then it can be arbitrarily manipulated, of course, not limited to its own application, you can get a pointer to an object of another application, it is also possible to operate. However, if you want to get a pointer, first, you must find the handle to that window, and then use the function FromHandle to get his pointer.

= = = Question 1:

How do I send a string in a custom message? For example:

SendMessage (mywnd,wm_userdefined, 0,0)

How do I write a string buffer to wparam or lparam?

You can pass the address of the string to the past, because the address is exactly 32 bits. Such as:

Char s[256];

SendMessage (mywnd,wm_userdefined, (WPARAM) s,0)

The receiver only needs to assign wparam to a char*. However, this method can only be used to pass data within a process.

= = = Question 2:

1. In the VC application framework, how to add their own classes, how to define the object of this class, I want to mouse click on a menu item to generate this object, do you get it? (The constructor for this class is a parameter).

2. Message sending function:

PostMessage (HWND handle,wm_mymessage,

WPARAM Wparam,lparam LPARAM)

In

How is the first parameter obtained?

If my message was generated in my own application, would you like to send it to the application window to show some data (with the TextOut function)?

(It can also be a problem: build the application framework with AppWizard, how to get a handle to the window in the generated class, such as CView, and put it in the PostMessage function.) )

3.wparam,lparam, how do I use the message response function? VC is how to ensure that these two numbers into the function of? More problems, thank you!

Level: Just getting Started

Reply:

1, this question is affirmative. You can use ClassWizard to define classes, or you can enter them manually. If the class definition is already in a file, just use the project| Add files adds the file to the project. To define an object for a class, just do it in the appropriate event for your menu item. Such as:

{

...

MyClass myObject ("Hello");

Myobject.mymethod ();

...

}

2, in your own program to pass the message of course there is no problem, as long as you know to invoke the window class is inherited from CWnd, you can use the GetSafeHwnd function to get the window handle. However, in the same program in their own sometimes not willing to use the custom message, because it is too cumbersome. You can simply add a member function to the class you want to call, and simply call this member function if you want to display the data? Why use PostMessage? It is common to use custom messages only if they are called between programs. At this point, you can usually use FindWindow to get the window handle (QA000251 "How to use the FindWindow () function to find the program").

3. MFC will automatically map wparam and lparam to a more easy-to-use approach for messages that MFC has already defined for message processing functions. such as OnMouseMove (UINT nflags, CPoint point). For functions that MFC cannot automatically handle, such as when you use On_message to define message functions, MFC passes WPARAM and LPARAM to your function without any processing.

= = = An open Win32 API function: Gettaskmanwindow ()

The following example also uses: GetProcAddress GetParent (HWND)

Hmodule HUser32 = GetModuleHandle ("user32");

Download

Getaskmanwnd.cpp (Windows nt/2000)

//

Take advantage of an undisclosed WIN32 API function: Gettaskmanwindow,

Action on Windows taskbar (Show/Hide). This function returns the window handle that owns the taskbar button.

//

This example'll show how can obtain a handle to the

Windows taskbar window.

//

(c) 1999 Ashot Oganesyan K, Smartline, INC

Mailto:[email protected], http://www.protect-me.com, http://www.codepile.com

#i nclude <windows.h>

#i nclude <stdio.h>

user32! Gettaskmanwindow (NT specific!)

//

This function returns a handle to the window that ownes the taskbar buttons

//

HWND Gettaskmanwindow ()

//

typedef HWND (WINAPI *procgettaskmanwnd) (void);

Procgettaskmanwnd Gettaskmanwindow;

void Main (int argc, char* argv[])

{

if (argc<2)

{

printf ("Usage:\n\ngetaskmanwnd.exe s| H\n ");

Return

}

Hmodule HUser32 = GetModuleHandle ("user32");

if (!HUSER32)

Return

Gettaskmanwindow = (Procgettaskmanwnd) GetProcAddress (HUser32, "Gettaskmanwindow");

if (! Gettaskmanwindow)

Return

HWND hwnd = Gettaskmanwindow ();

if (!hwnd)

Return

if (*argv[1]== "h" | | *argv[1]== "h")

ShowWindow (GetParent (hWnd), sw_hide);

Else

ShowWindow (GetParent (hWnd), sw_show);

}

C + + MFC common functions (GO)

Related Article

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.