|
This question is a duplicate of this: http://www.codeproject.com/Messages/4336758/How-to-pan-client-area-of-a-window-using-Cplusplus.aspx[^]
My detailed answer to it: http://www.codeproject.com/Messages/4337474/Re-How-to-pan-a-window-using-Cplusplus-VCplusplus-.aspx[^]
I recommended this minimal program to modify to implement your panning: Using SetCapture() and ReleaseCapture() correctly (usually during a drag n' drop operation).[^]
And here is the modified version of the recommended source:
main.cpp:
#include <windows.h>
HINSTANCE g_hInstance = (HINSTANCE)GetModuleHandle(NULL);
HWND g_hMainWnd = NULL;
bool g_MovingMainWnd = false;
POINT g_OrigCursorPos;
POINT g_PanStartIconPos;
HICON g_Icon = LoadIcon(NULL, IDI_EXCLAMATION);
POINT g_IconPos = { 0, 0 };
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_LBUTTONDOWN:
if (GetCursorPos(&g_OrigCursorPos))
{
RECT rt;
GetWindowRect(hWnd, &rt);
g_PanStartIconPos.x = g_IconPos.x;
g_PanStartIconPos.y = g_IconPos.y;
g_MovingMainWnd = true;
SetCapture(hWnd);
SetCursor(LoadCursor(NULL, IDC_SIZEALL));
}
return 0;
case WM_LBUTTONUP:
ReleaseCapture();
return 0;
case WM_CAPTURECHANGED:
g_MovingMainWnd = (HWND)lParam == hWnd;
return 0;
case WM_MOUSEMOVE:
if (g_MovingMainWnd)
{
POINT pt;
if (GetCursorPos(&pt))
{
g_IconPos.x = g_PanStartIconPos.x + (pt.x - g_OrigCursorPos.x);
g_IconPos.y = g_PanStartIconPos.y + (pt.y - g_OrigCursorPos.y);
InvalidateRect(hWnd, NULL, TRUE);
}
}
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
if (HDC dc = BeginPaint(hWnd, &ps))
{
DrawIcon(dc, g_IconPos.x, g_IconPos.y, g_Icon);
EndPaint(hWnd, &ps);
}
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
bool CreateMainWnd()
{
static const char CLASS_NAME[] = "MainWndClass";
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = g_hInstance;
wc.lpfnWndProc = &MainWndProc;
wc.lpszClassName = CLASS_NAME;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
if (!RegisterClass(&wc))
return false;
g_hMainWnd = CreateWindowEx(
0,
CLASS_NAME,
"Main Window",
WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
NULL,
NULL,
g_hInstance,
NULL
);
return true;
}
int main()
{
if (!CreateMainWnd())
return -1;
ShowWindow(g_hMainWnd, SW_SHOW);
UpdateWindow(g_hMainWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
|
|
|
|
|
Hi,
I have tried the logic you have given it is not working as I need.
It will be a great help if you can provide me modified sample code to pan the client area of a window.
|
|
|
|
|
No one can help you if you don't know what you wanna achieve.
|
|
|
|
|
(VS2008, MFC, Feature-Pack, also on VS2012)
I'm trying to not display the description part of a tooltip in a MFC application.
The description part is, I assume, the 2nd line of the default tooltip.
This is what I tried to do (in a straighforward wizard generated MFC application)
InitTooltipManager();
CMFCToolTipInfo ttParams;
ttParams.m_bVislManagerTheme = TRUE;
ttParams.m_bDrawDescription = FALSE; CTooltipManager* tooltipManager = theApp.GetTooltipManager();
tooltipManager->SetTooltipParams(AFX_TOOLTIP_TYPE_ALL, RUNTIME_CLASS(CMFCToolTipCtrl), &ttParams);
I would assume that the m_bDrawDescription in the struct, and the call to SetTooltipParams would change the whole behavior of the tooltips of the application, but it does not, when those lines are added, nothing seems to changed.
When debugging, the field is (re)set to TRUE.
Am I missing something ?
Thanks.
Watched code never compiles.
|
|
|
|
|
I have an SDI application with view based on CFormView, where I layed a CListCtrl control ... on this control I try to catch OnKeyup event, but is available for mapping only OnKeydown event :
ON_NOTIFY(LVN_KEYDOWN, IDC_LIST1, OnKeydownList1)
void CTestListView::OnKeydownList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_KEYDOWN* pLVKeyDown = (LV_KEYDOWN*)pNMHDR;
*pResult = 0;
}
I try to modify LVN_KEYDOWN event in LVN_KEYUP, but there are not going ... my question is, how can I catch OnKeyup event on CListCtrl layered on CFormView ? It is not posible ?
Thank you.
modified 20-Aug-12 12:18pm.
|
|
|
|
|
If [this] still fails then maybe this?
WM_KEYUP Instead of LVN_KEYUP .
BEGIN_MESSAGE_MAP(CCeListCtrl, CListCtrl)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_KEYDOWN()
ON_WM_KEYUP()
END_MESSAGE_MAP()
void CCeListCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) {
switch (nChar) {
case VK_RETURN:
break;
}
CListCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CCeListCtrl::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) {
switch (nChar) {
case VK_RETURN:
break;
}
CListCtrl::OnKeyUp(nChar, nRepCnt, nFlags);
}
|
|
|
|
|
Hello Friends
creating a mFC project. Saving a image in bmp,for that I am using BITMAPINFOHEADER.
In Debug build,its working fine. But in release Build, BITMAPINFOHEADER fields are not accepting values that I am putting,it is still showing some garbage value.
Do I need to change in properties for release build ?
Thanks In Advance.
Regards
Yogesh
|
|
|
|
|
How do you check the values in Release? I hope not via debugger?
|
|
|
|
|
Hi
we cn debug in release build too.
|
|
|
|
|
Release builds usually optimize and this makes the debugger go wrong often.
If you want to see the values in Release it's probably better to use other means, writing them to a file or other external resource/tool.
|
|
|
|
|
Hey Guys
I found on net that whenever you found something working on debug but not in release build then first of all Disable optimization under Project properties ->c/c++ Tab,Under Optimization category - >under option (optimization).
I choose to Disable from maximize speed in release build then its working.
But,I have doubt over this. What it should be the optimization in release build and If it is other than Disable option then how this problem can be solved.
Any Views ??
But,Anyway mine BitmapInfoheader problem is Solved.
Regards
Yogesh
|
|
|
|
|
|
Is there any way of doing it? I am trying to write some simple class for showing modeless dialog box and just cannot understand what is wrong with it. While it is pretty easy with DialogBoxParam - there seems to be no way with CreateDialog . My goal is to pass somehow a pointer to a class to call my dlgProc. The way i am doing it with modal dlgbox:
INT_PTR DoModal(UINT nResourceId)
{
::InitCommonControls();
return ::DialogBoxParam(::GetModuleHandle(NULL),
MAKEINTRESOURCE(nResourceId),
NULL, DlgProcModal, reinterpret_cast<LPARAM>(this));
}
static INT_PTR __stdcall DlgProcModal(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
....
if( uMsg == WM_INITDIALOG )
{
if( !pWnd )
{
pWnd = reinterpret_cast<CBaseDlgWindow *>(lParam);
::SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)pWnd);
pWnd->m_hWnd = hwndDlg;
}
}
}
The way i am trying to do it with modeless and it is just not working:
INT_PTR DoModeless(UINT nResourceId)
{
::InitCommonControls();
if( m_hWnd = ::CreateDialog(::GetModuleHandle(NULL), MAKEINTRESOURCE(nResourceId), NULL, DlgProcModeless) )
{
m_bModeless = true;
::SetWindowLongPtr(m_hWnd, GWLP_USERDATA, (LONG_PTR)this);
MSG msg = { 0x00 };
while (::GetMessage(&msg, m_hWnd, 0, 0))
{
if (!::IsDialogMessage(m_hWnd, &msg))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
}
::DestroyWindow(m_hWnd);
return 0;
}
static INT_PTR __stdcall DlgProcModeless(
HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam
)
{
LONG_PTR lResult = ::GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
CBaseDlgWindow * pWnd = reinterpret_cast<CBaseDlgWindow *>(lResult);
...
}
What am i doing wrong?
Thanks in advance.
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
First, you probably have a main message loop somewhere that dispatches messages to every window (and dialog), not only to a specified window:
std::vector<HWND> g_Dialogs;
MSG msg;
while (::GetMessage(&msg, NULL, 0, 0))
{
std::vector<HWND> dialogs(g_Dialogs);
bool was_dialog_message = false;
for (size_t i=0,e=dialogs.size(); i<e; ++i)
{
if (::IsWindow(dialogs[i]) && ::IsDialogMessage(dialogs[i], &msg))
{
was_dialog_message = true;
break;
}
}
if (!was_dialog_message)
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
When you want a modeless dialog, you just create it with CreateDialog() , put its handle to g_Dialogs and thats it. You don't have to run a local message loop like with modal messageboxes, especially not with a specified single window handle (m_hWnd). A modeless dialog works almost like a normal window that you created with CreateWindowEx() , you just have to handle it a bit specially with IsDialogMessage() . In a modeless multi-windowed gui program its always enough one message loop - the main message loop pumps the messages for all modeless windows.
EDIT: warning: while you are iterating over the g_Dialogs vector, the contents of the vector might change because you dispatch messages to dialogs that can respond to those messages by creating/deleting dialogs! To avoid bugs caused by this you either use a custom container that can be modified during iteration or solve it somehow else. One good solution can be copying the vector before iteration and iterating on the copied vector, still some HWNDs might become invalid during iteration so before IsDialogMessage() it might be wise to call IsWindow() on the handles.
modified 19-Aug-12 19:12pm.
|
|
|
|
|
Hey, thanks for your reply. My problem was that i couldn't find a way to put the whole thing into nice C++ class of my own. But, problem was solved by using CreateDialogParam instead. So i can pass a pointer to the dialog class to a main message loop so i can call my own virtual methods inside children classes.
Cheers.
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
I though that your problem is the messagebox because your loop was inside the DoModeless() method. Anyway, your previous hooking is also fine, you just miss a few messages that come before the return of your CreateDialog() call and you have to handle uninitialized GWLP_USERDATA cases as well. Even if you initialize userdata from WM_INITDIALOG it might be better to handle uninitialized cases, who knows what the future brings?
|
|
|
|
|
I'm a Chinese , want to find a friend who use C/C++
,and i want to comunicate with him ,anything
my email and MSN is mqy_bs@Hotmail.com,E-mail me if you want
|
|
|
|
|
You should really read this set of guidelines[^] for using these forums. If you have trouble with English then the General Chinese Forum[^] has members who can help you.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
The following is a fragment of code:
sockaddr_in from,a;
WSADATA wsdata;
BOOL optval = TRUE;
u_short uPort = m_nPort;
if(WSAStartup(0x0202,&wsdata) != 0)
{
CString strErr;
strErr.Format(_T("WSAStartup Init failed! Error code:%d"),WSAGetLastError());
AddNote(strErr);
_endthreadex(0);
return FALSE;
}
a.sin_family = AF_INET;
a.sin_addr.s_addr = 0;
a.sin_port = htons(uPort);
from.sin_family = AF_INET;
from.sin_addr.s_addr = INADDR_BROADCAST;
from.sin_port = htons(uPort);
int fromlength=sizeof(SOCKADDR);
pThread->m_socket = socket(AF_INET,SOCK_DGRAM,0);
setsockopt(pThread->m_socket,SOL_SOCKET,SO_BROADCAST,(char FAR *)&optval,sizeof(optval));
bind(pThread->m_socket,(sockaddr *)&a,sizeof(sockaddr_in));
char buf[BUF_MAX_LEN] = {0x00};
int nLen = 0;
char *pszIPClient = NULL;
u_short uPortClient = 0;
THREADBOARD *pThreadBoard = NULL;
CString strInit = _T("");
strInit.Format(_T("Port: %d Now waiting for clients..."),uPort);
AddNote(strInit);
while(true)
{
nLen = recvfrom(pThread->m_socket,buf,BUF_MAX_LEN,0,(struct sockaddr FAR *)&from,(int FAR *)&fromlength);
if(nLen > 0)
{
}
}
Thanks for your reply !
|
|
|
|
|
You can do this by using raw sockets but I was raw-socketing only on linux, dont know how it works on windows (if it works at all...). I searched for an example and found this discussion: http://bytes.com/topic/c/answers/900770-how-determine-origin-incoming-message-uincast-multicast-broadcast[^]
One of the answers contains source code, read along that thread. Note: I havent tested their code.
EDIT: The trick is simple: the sendto address is in the header of the datagram you received, but your recvfrom returns just the payload from that and the address of the sender. You need a method to reach other fields of the received UDP packet header, one way is using raw sockets.
modified 18-Aug-12 11:22am.
|
|
|
|
|
First [this].
Then, Packet Filtering , found [here].
Also [this].
|
|
|
|
|
|
Programs under windows need administrator privilege must get user's permission when they start. And they can also run with administrator privilege by right click the EXE and select "Run as Administrator". But, before the EXE start, a message box will show up. It's really disgusting.
Now I want my application to run as the Administrator, and I don't want the popup message box when user click the EXE file.
Now I wonder whether AdjustTokenPrivileges can help me achieve this.
Any one can help me?
modified 18-Aug-12 10:13am.
|
|
|
|
|
This has nothing to do with C++/MFC, please choose a forum more appropriate to the subject. You may find that the most appropriate forum is Google itself.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
So you want your program to run as an admin without notifying the user, or getting the admin's permission?
Sounds like malware to me.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|