69,371
社区成员
发帖
与我相关
我的任务
分享
case WM_COMMAND:{...}
后面我忘记加break;了,导致程序执行完按键的命令胡自动执行了后面的case WM_DESTROY:\
PostQuitMessage(0);
return 0;
#include <windows.h>
#include <stdlib.h>
#include "resource.h"
#include "resrc1.h"
#include <stdio.h>
HDC hDC;
HFONT hfont,ohfont;
PAINTSTRUCT paint;
RECT rect;
COLORREF oc;
HMENU hMenuShort;
int PopupShortcutMenu(HWND hWnd)
{
POINT point;
GetCursorPos(&point);
TrackPopupMenu(GetSubMenu(hMenuShort,0),TPM_CENTERALIGN,point.x,point.y,0,(HWND)hWnd,NULL );
return 1;
}
LRESULT WINAPI WinProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
switch(Msg)
{
case WM_RBUTTONUP:
PopupShortcutMenu(hWnd);
break;
case WM_PAINT:
BeginPaint(hWnd, &paint);
hDC = paint.hdc; // the device context to draw in
GetClientRect(hWnd, &rect); // Obtain the window's client rectangle
hfont = CreateFont(200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "宋体");
ohfont=(HFONT)SelectObject(hDC,hfont);
oc=SetTextColor(hDC,0x00C080FF);
SetBkMode(hDC, TRANSPARENT);
TextOut(hDC,rect.left+rect.right/2-600, rect.top+rect.bottom/2-100,"最短画图程序",12);
SelectObject(hDC,ohfont);
SetTextColor(hDC,oc);
DeleteObject(hfont);
EndPaint(hWnd, &paint);
break;
case WM_COMMAND:
switch(0x0000ffff&wParam)
{case 40001:
MessageBox(NULL,"4001","WinMain",MB_OK);
break;
case 40002:
MessageBox(NULL,"4002","WinMain",MB_OK);
break;
case 40003:
MessageBox(NULL,"4003","WinMain",MB_OK);
break;
}
case WM_DESTROY:\
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,Msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
char *cName = "myWindow";
WNDCLASSEX wc;
HWND hWnd;
MSG Msg;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(hInstance,MAKEINTRESOURCE(IDC_1));
wc.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
wc.hIconSm = NULL;
wc.hInstance = hInstance;
wc.lpfnWndProc = WinProc;
wc.lpszClassName =(LPSTR)cName;
wc.lpszMenuName = NULL;//
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx(&wc);//
hMenuShort = LoadMenu(hInstance,MAKEINTRESOURCE(IDM_SHORTCUT));
hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,cName,"贪吃蛇",WS_POPUP|WS_VISIBLE,
0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),NULL,NULL,hInstance,NULL);
if(hWnd == NULL)
{//容错处理
MessageBox(NULL,"There's an Error","Error Title",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
ShowWindow(hWnd,nShowCmd);//显示窗口
UpdateWindow(hWnd);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);//翻译消息
DispatchMessage(&Msg);//分派消息
}
return Msg.message;
}
#include <windows.h>
#include <stdlib.h>
#include "resource.h"
HDC hDC;
HFONT hfont,ohfont;
PAINTSTRUCT paint;
RECT rect;//RECT用来存储窗口信息的结构,只要是窗口的坐标、宽度和高度。
COLORREF oc;
HMENU hMenuShort;//同上
//定义弹出快捷菜单函数
int PopupShortcutMenu(HWND hWnd)
{
POINT point;
GetCursorPos(&point);//获取鼠标的位置
//弹出菜单
TrackPopupMenu(GetSubMenu(hMenuShort,0),TPM_CENTERALIGN,point.x,point.y,0,(HWND)hWnd,NULL );
return 1;
}
//回调函数
LRESULT WINAPI WinProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
switch(Msg)//处理消息过程,什么是消息,下节再讲
{
case WM_LBUTTONUP://鼠标左键松开时
//清除文字,出现菜单
break;
case WM_RBUTTONUP://鼠标右键松开时
PopupShortcutMenu(hWnd);
break;
case WM_PAINT:
BeginPaint(hWnd, &paint);
hDC = paint.hdc; // the device context to draw in
GetClientRect(hWnd, &rect); // Obtain the window's client rectangle
hfont = CreateFont(200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "宋体");
ohfont=(HFONT)SelectObject(hDC,hfont);
oc=SetTextColor(hDC,0x00C080FF);
SetBkMode(hDC, TRANSPARENT);
TextOut(hDC,rect.left+rect.right/2-600, rect.top+rect.bottom/2-100,"最短画图程序",12);
SelectObject(hDC,ohfont);
SetTextColor(hDC,oc);
DeleteObject(hfont);
EndPaint(hWnd, &paint);
break;
case WM_COMMAND:
switch(0x0000ffff&wParam)
{case IDR_HELP:
//添加新菜单项
AppendMenu(GetSubMenu(hMenuShort,0),MF_CHECKED,0X4403,"新添加的菜单项");
break;
case IDR_RANK:
//删除菜单项
DeleteMenu(GetSubMenu(hMenuShort,0),0x4403,MF_BYCOMMAND);
break;
case IDR_EXIT:
exit(1);
}
case WM_DESTROY://响应鼠标单击关闭按钮事件
PostQuitMessage(0);//退出消息队列,至于什么是消息队列,下节说
return 0;//退出函数
}
return DefWindowProc(hWnd,Msg,wParam,lParam);
}
//主函数
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
char *cName = "myWindow";
WNDCLASSEX wc;
HWND hWnd;
MSG Msg;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//通过函数来设置一个白色的背景
wc.hCursor = LoadCursor(hInstance,MAKEINTRESOURCE(IDC_1));//载入光标资源
wc.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
wc.hIconSm = NULL;//不设置
wc.hInstance = hInstance;//当前程序的句柄,hInstance是有系统给传递的
wc.lpfnWndProc = WinProc;//窗口处理过程的回调函数。
wc.lpszClassName =(LPSTR)cName;//窗口类的名字。
wc.lpszMenuName = NULL;//目录名,不设置
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx(&wc);//在系统中注册
hMenuShort = LoadMenu(hInstance,MAKEINTRESOURCE(IDM_SHORTCUT));
hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,cName,"贪吃蛇",WS_POPUP|WS_VISIBLE,
0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),NULL,NULL,hInstance,NULL);//创建窗口,窗口标题为"我的窗口我喜欢"
if(hWnd == NULL)
{//容错处理
MessageBox(NULL,"There's an Error","Error Title",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
ShowWindow(hWnd,nShowCmd);//显示窗口
UpdateWindow(hWnd);
//下面是对消息的循环处理,大家先不必管这些,下节课我会细说的
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);//翻译消息
DispatchMessage(&Msg);//分派消息
}
return Msg.message;
}