VC小技巧20个
VC小技巧20个 1. 打开CD-ROM mciSendString(Set cdAudio door openwait,NULL,0,NULL); 2. 关闭CD_ROM mciSendString(Set cdAudio door closedwait,NULL,0,NULL); 3. 关闭计算机 OSVERSIONINFO OsVersionInfo; // 包含操作系统版本信息的数据结构 OsVersionIn
VC小技巧20个
1. 打开CD-ROM
mciSendString("Set cdAudio door openwait",NULL,0,NULL);
2. 关闭CD_ROM
mciSendString("Set cdAudio door closedwait",NULL,0,NULL);
3. 关闭计算机
OSVERSIONINFO OsVersionInfo; //包含操作系统版本信息的数据结构
OsVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&OsVersionInfo); //获取操作系统版本信息
if(OsVersionInfo.dwPlatformId ==VER_PLATFORM_WIN32_WINDOWS)
{
//Windows98,调用ExitWindowsEx()函数重新启动计算机
DWORD dwReserved;
ExitWindowsEx(EWX_REBOOT,dwReserved);//可以改变第一个参数,实现注销用户、
//关机、关闭电源等操作
// 退出前的一些处理程序
}
4. 重启计算机
typedef int (CALLBACK *SHUTDOWNDLG)(int); //显示关机对话框函数的指针
HINSTANCE hInst = LoadLibrary("shell32.dll");//装入shell32.dll
SHUTDOWNDLG ShutDownDialog; //指向shell32.dll库中显示关机对话框函数的指针
if(hInst != NULL)
{
//获得函数的地址并调用之
ShutDownDialog =(SHUTDOWNDLG)GetProcAddress(hInst,(LPSTR)60);
(*ShutDownDialog)(0);
}
5. 枚举所有字体
LOGFONT lf;
lf.lfCharSet = DEFAULT_CHARSET; // Initialize the LOGFONTstructure
strcpy(lf.lfFaceName,"");
CClientDC dc (this);
//Enumerate the font families
::EnumFontFamiliesEx((HDC) dc,&lf,
(FONTENUMPROC) EnumFontFamProc,(LPARAM) this,0);
//枚举函数
int CALLBACK EnumFontFamProc(LPENUMLOGFONTlpelf,LPNEWTEXTMETRIC
lpntm,DWORD nFontType,long lparam)
{
// Create a pointer to the dialogwindow
CDay7Dlg* pWnd = (CDay7Dlg*) lparam;
// add the font name to the list box
pWnd->m_ctlFontList.AddString(lpelf ->elfLogFont.lfFaceName);
// Return 1 to continue fontenumeration
return 1;
}
其中m_ctlFontList是一个列表控件变量
6. 一次只运行一个程序实例,如果已运行则退出
if( FindWindow(NULL,"程序标题")) exit(0);
7. 得到当前鼠标所在位置
CPoint pt;
GetCursorPos(&pt); //得到位置
8. 上下文菜单事件触发事件:
OnContextMenu事件
9. 显示和隐藏程序菜单
CWnd *pWnd=AfxGetMainWnd();
if(b_m) //隐藏菜单
{
pWnd->SetMenu(NULL);
pWnd->DrawMenuBar();
b_m=false;
}
else
{
CMenu menu;
menu.LoadMenu(IDR_MAINFRAME); ////显示菜单也可改变菜单项
pWnd->SetMenu(&menu);
pWnd->DrawMenuBar();
b_m=true;
menu.Detach();
}
10. 获取可执行文件的图标
HICON
hIcon=::ExtractIcon(AfxGetInstanceHandle(),_T("NotePad.exe"),0);
if (hIcon &&hIcon!=(HICON)-1)
{
pDC->DrawIcon(10,10,hIcon);
}
DestroyIcon(hIcon);
11. 窗口自动靠边程序演示
BOOL AdjustPos(CRect* lpRect)
{
//自动靠边
intiSX=GetSystemMetrics(SM_CXFULLSCREEN);
intiSY=GetSystemMetrics(SM_CYFULLSCREEN);
RECT rWorkArea;
BOOL bResult =SystemParametersInfo(SPI_GETWORKAREA,
sizeof(RECT), &rWorkArea, 0);
CRect rcWA;
if(!bResult)
{
//如果调用不成功就利用GetSystemMetrics获取屏幕面积
rcWA=CRect(0,0,iSX,iSY);
}
else
rcWA=rWorkArea;
int iX=lpRect->left;
int iY=lpRect->top;
if(iX
{
//调整左
//pWnd->SetWindowPos(NULL,rcWA.left,iY,0,0,SWP_NOSIZE);
lpRect->OffsetRect(rcWA.left-iX,0);
AdjustPos(lpRect);
return TRUE;
}
if(iY
{
//调整上
//pWnd->SetWindowPos(NULL,iX,rcWA.top,0,0,SWP_NOSIZE);
lpRect->OffsetRect(0,rcWA.top-iY);
AdjustPos(lpRect);
return TRUE;
}
if(iX + lpRect->Width() >rcWA.right - DETASTEP && iX
!=rcWA.right-lpRect->Width())
{
//调整右
//pWnd->SetWindowPos(NULL
,rcWA.right-rcW.Width(),iY,0,0,SWP_NOSIZE);
lpRect->OffsetRect(rcWA.right-lpRect->right,0);
AdjustPos(lpRect);
return TRUE;
}
if(iY + lpRect->Height() >rcWA.bottom - DETASTEP && iY
!=rcWA.bottom-lpRect->Height())
{
//调整下
//pWnd->SetWindowPos(NULL
,iX,rcWA.bottom-rcW.Height(),0,0,SWP_NOSIZE);
lpRect->OffsetRect(0,rcWA.bottom-lpRect->bottom);
return TRUE;
}
return FALSE;
}
//然后在ONMOVEING事件中使用所下过程调用
CRect r=*pRect;
AdjustPos(&r);
*pRect=(RECT)r;
12. 给系统菜单添加一个菜单项
给系统菜单添加一个菜单项需要进行下述三个步骤:
首先,使用Resource Symbols对话(在View菜单中选择Resource Symbols ...可以显示该对话)定义菜单项ID,该ID应大于0x0F而小于0xF000;
其次,调用CWnd::GetSystemMenu获取系统菜单的指针并调用CWnd::Appendmenu将菜单项添加到菜单中。下例给系统菜单添加两个新的菜单项。
int CMainFrame:: OnCreate (LPCREATESTRUCT lpCreateStruct)
{
…
//Make sure system menu item is in theright range.
ASSERT(IDM_MYSYSITEM
//Get pointer to system menu.
CMenu* pSysMenu=GetSystemMenu(FALSE);
ASSERT_VALID(pSysMenu);
//Add a separator and our menu item tosystem menu.
CString StrMenuItem(_T ("New menuitem"));
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_MYSYSITEM, StrMenuItem);
…
}
13. 运行其它程序
//运行EMAIL或网址
char szMailAddress[80];
strcpy(szMailAddress,"");
ShellExecute(NULL, "open", szMailAddress, NULL,NULL,
SW_SHOWNORMAL);
//2、运行可执行程序
WinExec("notepad.exe",SW_SHOW); //运行记事本
14. 动态增加或删除菜单
(1) 增加菜单
//添加
CMenu *mainmenu;
mainmenu=AfxGetMainWnd()->GetMenu(); //得到主菜单
(mainmenu->GetSubMenu (0))->AppendMenu(MF_SEPARATOR);//添加分隔符
(mainmenu->GetSubMenu
(0))->AppendMenu(MF_STRING,ID_APP_ABOUT,_T("Alwayson &Top"));
//添加新的菜单项
DrawMenuBar(); //重画菜单
(2) 删除菜单
//删除
CMenu *mainmenu;
mainmenu=AfxGetMainWnd()->GetMenu(); //得到主菜单
CString str ;
for(int i=(mainmenu->GetSubMenu (0))->GetMenuItemCount()-1;i>=0;i--)
//取得菜单的项数。
{
(mainmenu->GetSubMenu(0))->GetMenuString(i,str,MF_BYPOSITION);
//MF_BYPOSITION的解释见上。
if(str=="Always on&Top") //如果是刚才我们增加的菜单项,则删除。
{
(mainmenu->GetSubMenu (0))->DeleteMenu(i,MF_BYPOSITION);
break;
}
}
15. 测试ALT键是否按下:
GetKeyState(VK_MENU);
GetAlt();
16. 检查是否按下鼠标左键
if((nFlags&MK_LBUTTON)==MK_LBUTTON)
17. 检查键盘输入
在OnKeyDown中的参数nChar是一个数值,当显示的时候,需要转换成字符,使用如下的命令:
char lsChar;
lsChar=char(nChar);
if(lsChar=='A');
{
.......
}
18. 调用另一个函数::GetKeyState(),用一个特定的键代码来确定法键是否被按下。如果::GetKeyState函数的返回值是负的,表示该键被按下。如果返回值是非负的,表示该留未被按下。例如:如果要确定shift键是否被按下,可以使用下面的代码:
if(::GetKeyState(VK_SHIFT)
{
AfxMessageBox("shift ispressed");
}
19. 如何在编程的过程中随时结束应用程序(常规)
1)需要向窗口发送 WM_CLOSE/WM_QUIT消息,
调用 CWnd::OnClose成员函数并允许对用户提示是否保存修改过的数据.
AfxGetMainWnd()->SendMessage(WM_CLOSE); //别忘了先得到当前窗口的指针
2)使用函数: void PostQuitMessage( int nExitCode // exit code );
3)使用标准函数:void exit( int status ); //尽量不要在MFC中使用
20. 得到屏幕的尺寸大小
HWND hWnd;
CRect Rect;
hWnd = ::GetDesktopWindow();
::GetClientRect(hWnd, &Rect);
//---------------------------------------------------------
如何查询和设置系统参数
在Windows 3.1SDK中介绍过SDK函数SystemParametersInfo,调用该函数可以查询和设置系统参数,诸如按键的重复速率设置、鼠标双击延迟时间、图标字体以及桌面覆盖位图等等。
//Create a font that is used for icon titles.
LOGFONT stFont; :: SystemParametersInfo(SPIF_GETICONTITLELOGFONT,
sizeof (LOGFONT), &stFont,SPIF_SENDWININICHANGE);
m_font.CreateFontIndirect (&stFont); //Change thewallpaper to leaves.bmp.
:: SystemParametersInfo (SPI_SETDESKWALLPAPER, 0,
_T("forest.bmp"),SPIF_UPDATEINIFILE);
//---------------------------------------------------------
如何使用一个预定义的Windows光标?调用CWinApp:: LoadStandardCursor并传送光标标识符。
BOOL CSampleDialog:: OnSetCursor(CWnd* pWnd, UINT nHitTest,
UINT message) { //Display wait cursor if busy.
if (m_bBusy) {
SetCursor (AfxGetApp () ->LoadStandardCursor(IDC_WAIT));
return TRUE; }
return CDialog:: OnSetCursor (pWnd. nHitTest,message); }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Douyin is a popular short video social application. Users can not only watch and share various types of short video content, but also provide a series of mini games for you to play. So where can I get into Douyin mini-games? Where is the entrance to Douyin mini game? Let’s take a look at the detailed tutorial below. Method 1: Open the mini program 1. Click the My option. After entering the homepage of Douyin, click the My option to enter. 2. Click the three horizontal lines. After entering the My interface, click the three horizontal lines button above. 3. Click on the mini program. After opening the three horizontal lines option, click on the mini program inside. 4. Click on the Douyin Mini Game. After entering the mini program interface, click on the Douyin Mini Game option to open it. Method 2: Search and open 1. Click the magnifying glass to enter

Win11 Tips Sharing: One trick to skip Microsoft account login Windows 11 is the latest operating system launched by Microsoft, with a new design style and many practical functions. However, for some users, having to log in to their Microsoft account every time they boot up the system can be a bit annoying. If you are one of them, you might as well try the following tips, which will allow you to skip logging in with a Microsoft account and enter the desktop interface directly. First, we need to create a local account in the system to log in instead of a Microsoft account. The advantage of doing this is

In C language, it represents a pointer, which stores the address of other variables; & represents the address operator, which returns the memory address of a variable. Tips for using pointers include defining pointers, dereferencing pointers, and ensuring that pointers point to valid addresses; tips for using address operators & include obtaining variable addresses, and returning the address of the first element of the array when obtaining the address of an array element. A practical example demonstrating the use of pointer and address operators to reverse a string.

We often create and edit tables in excel, but as a novice who has just come into contact with the software, how to use excel to create tables is not as easy as it is for us. Below, we will conduct some drills on some steps of table creation that novices, that is, beginners, need to master. We hope it will be helpful to those in need. A sample form for beginners is shown below: Let’s see how to complete it! 1. There are two methods to create a new excel document. You can right-click the mouse on a blank location on the [Desktop] - [New] - [xls] file. You can also [Start]-[All Programs]-[Microsoft Office]-[Microsoft Excel 20**] 2. Double-click our new ex

How to open Minesweeper? Minesweeper is a classic single-player game in which the player's goal is to uncover numbers within squares in order to determine which squares contain mines. If the revealed square contains no mines, the game will continue; if the revealed square contains mines, the game will end immediately. Next, we will introduce in detail how to enter the Minesweeper game. Step 1: Turn on the computer First, you need to turn on the computer. Minesweeper is a computer game that can only be played on a computer. If you haven't turned on the computer yet, press and hold the power button until the computer turns on and displays the desktop. Step 2: Find the Minesweeper game. Look for the Minesweeper game icon on the desktop. If Minesweeper is already installed, it should appear on the desktop. If not, search for Minesweeper game in your computer's start menu

VSCode (Visual Studio Code) is an open source code editor developed by Microsoft. It has powerful functions and rich plug-in support, making it one of the preferred tools for developers. This article will provide an introductory guide for beginners to help them quickly master the skills of using VSCode. In this article, we will introduce how to install VSCode, basic editing operations, shortcut keys, plug-in installation, etc., and provide readers with specific code examples. 1. Install VSCode first, we need

Win11 tricks revealed: How to bypass Microsoft account login Recently, Microsoft launched a new operating system Windows11, which has attracted widespread attention. Compared with previous versions, Windows 11 has made many new adjustments in terms of interface design and functional improvements, but it has also caused some controversy. The most eye-catching point is that it forces users to log in to the system with a Microsoft account. For some users, they may be more accustomed to logging in with a local account and are unwilling to bind their personal information to a Microsoft account.

Title: PHP Programming Tips: How to Jump to a Web Page within 3 Seconds In web development, we often encounter situations where we need to automatically jump to another page within a certain period of time. This article will introduce how to use PHP to implement programming techniques to jump to a page within 3 seconds, and provide specific code examples. First of all, the basic principle of page jump is realized through the Location field in the HTTP response header. By setting this field, the browser can automatically jump to the specified page. Below is a simple example demonstrating how to use P
